#!/usr/bin/perl

#
# avery - print Avery 5267 numbered tape labels via PostScript laser printer
#
#   This software is Public Domain. Please maintain version history.
#   Report all bugs to Greg Ercolano (erco@3dsite.com).
#
#       VERS    DATE     AUTHOR                 COMMENTS
#       1.00    07/13/96 erco@3dsite.com        Initial implementation
#

# MAIN
{
    local($x, $y);
    local($progname) = "avery-5267";
    local($prefix)   = $ARGV[0];
    local($count)    = $ARGV[1];
    local($command)  = join(" ", $0, @ARGV);
    local(%cursor);

    # COMPUTE X RANGE OF POSITIONS
    $cursor{"x"}{"start"} = .50;        # indent from left edge
    $cursor{"x"}{"end"}   = 8.0;
    $cursor{"x"}{"inc"}   = (8.0/4);    # 4 labels wide

    # COMPUTE Y RANGE OF POSITIONS
    $cursor{"y"}{"start"} = 11 - .90;   # offset from top .80
    $cursor{"y"}{"end"}   = .5;
    $cursor{"y"}{"inc"}   = -.5;        # step rate .5 inch per label

    # HELP
    if ( $#ARGV != 1 )
    {
        print STDERR "  usage: $progname <prefix> <startval>\n"; 
        print STDERR "example: $progname 'ID# ' 100\n";
        exit(1);
    }

    # POSTSCRIPT HEADER
    #    Setup font, dimension, then scale page so values represent inches
    #
    print <<"EOF";
%!PS-Adobe-1.0
%%DocumentFonts: Helvetica-Bold
%%Dimensions: 8.5 x 11 (in)
%%Margins: (l,t,r,b) $cursor{"x"}{"start"}, $cursor{"y"}{"start"}, $cursor{"x"}{"end"}, $cursor{"y"}{"end"}
%%Title: $command
%%Pages: (atend)
%%EndComments

%%EndProlog
%%Page 1 1
save
gsave
% -- CONVERT TO INCHES
72 72 scale save

%% FONT SIZE/COLOR
% -- When scaled to inches, '1.0 scalefont' makes 
% -- Helvetica-Bold letters .75 inch high
% /Helvetica-Narrow-Bold findfont
/Helvetica-Bold findfont
0.250000 scalefont setfont 0 setgray
EOF

    # LOOP THRU ENTRIES VERTICALLY DOWN THE PAGE
    for ( $y =  $cursor{"y"}{"start"};
          $y >= $cursor{"y"}{"end"};
          $y += $cursor{"y"}{"inc"})
    {
	# LOOP THRU ENTRIES ACROSS THE RIGHT OF THE PAGE
        for ( $x =  $cursor{"x"}{"start"};
              $x <= $cursor{"x"}{"end"};
              $x += $cursor{"x"}{"inc"})
        {
	    # POSITION AND PRINT PREFIX FOLLOWED BY COUNT
            printf("%.2f %.2f moveto (%s%d) show\n", $x, $y, $prefix, $count);
            $count++;
        }
    }

    # POSTSCRIPT TRAILER
    print <<"EOF";
showpage restore
grestore
restore
%%Trailer
%%Pages: 1
EOF
}
