#!/bin/sh
# -*- perl -*-
# This code allows us to start perl from our path or an environment variable
# rather than hardcoding a path into the #! line.  It works from sh or csh.
(exit $?0) && eval 'exec ${QPERLQ-perl} -x $0 ${1+"$@"}'
if (! $?QPERLQ) setenv QPERLQ perl
exec $QPERLQ -x $0 $argv:q

#!/usr/local/bin/perl -w
#
# $Id: make-labels,v 1.2 1998/05/10 14:29:51 ejb Exp $
# $Source: /home/ejb/scripts/RCS/make-labels,v $
# $Author: ejb $
#

{
    local(@whoami) = split('/', $0);
    $whoami = pop(@whoami);
};

$prolog = "/usr/local/share/label-prolog.ps";

open(PROLOG, "<$prolog") ||
    die "$whoami: can't open $prolog: $!\n";

chop($date = `date`);

print <<EOF;
%!
%
% This file was automatically generated on $date
% from label information and from the file 
% $prolog.
EOF

    # Get the indentation right again...
    ;

while (<PROLOG>)
{
    m/%!/ && next;
    m/INSERT DATA HERE/ && last;
    print;
}

close(PROLOG);

@lines = ();
$maxrows = 10;
$maxcols = 3;
$row = $col = 1;
while (<>)
{
    chop;
    s/^\s*//;
    s/\s*$//;
    if (m/^$/)
    {
	if (scalar(@lines) > 0)
	{
	    printf "%02d %02d [ ", $row, $col;
	    $first = 1;
	    foreach $line (@lines)
	    {
		print " " x 8 unless $first;
		$first = 0;
		$line =~ s/\(/\\\(/g;
		$line =~ s/\)/\\\)/g;
		print "($line)\n";
	    }
	    print "      ] draw-label\n\n";
	    @lines = ();
	    $col++;
	    ($col > $maxcols) && do { $col = 1; $row++ };
	    ($row > $maxrows) && do { $row = 1; &showpage };
	}
    }
    else
    {
	push (@lines, ($_));
    }
}

&showpage unless (($row == 1) && ($col == 1));

sub showpage {
    print "show-label-outlines { draw-label-outlines } if\n";
    print "showpage\n\n";
}
