#!/usr/bin/perl

# Peter Nermander <m8130@abc.se>, May 2004

# Script to add collation bar and signature number
# to imposed sheets

while($_=$ARGV[0], /^-/) {
    shift;
    /^-px/ && do {$pmarginx=shift or die "Missing x margin\n"; next;};
    /^-py/ && do {$pmarginy=shift or die "Missing y margin\n"; next;};



    /^-help/ && do {print <<EOHELP;exit;};
Usage: sign -px <xmargin> -py <ymargin> <input file> [<imposition>]
where
 xmargin     are printer margins, defined to make
 ymargin     sure the number ends up in printed space

 imposition  name of the impositions used. If not specified the
             script will parse the input file for the imposition.
EOHELP
}

($pmarginx & $pmarginy) or die "Printer margin not specified\n";

$infile=shift or die "No input file specified\n";
$outfile= "signed_" . $infile;

# If imposition is not specified
$imposition=shift or do {

# Parse input file for imposition
    open INFILE, $infile or die "Input file not found\n";
    while(<INFILE>) {
	/(%#Imposition:\s*)(.*$)/ && do {
	    $imposition=$2;
	};
    };
};

die "Could not figure out imposition\n" unless $imposition;

# We got a name, check if it exists
$imposition.=".imp";
-f $imposition or die "Imposition file $imposition not found\n";

# Let's read it
do $imposition;

open INFILE, $infile;
    while(<INFILE>) {
       /(^%%BoundingBox: )(.*)$/ && do {
           ($llx,$lly,$sw,$sh) = split /\s+/, $2;};
};

# Here comes the work

# Cell size
$ch=int($sh/$rows); $cw=int($sw/$cols);

# Cell location
($cy,$cx)=split /,/, $sig_number{'cell'};


# Cell lower left corner
$sigx=($cx-1)*$cw; $sigy=($cy-1)*$ch;

# Position of the desired corner
# This can be done easier?

$sig_number{'loc'} =~ /[lL][lL]/ && do {
    $sigx+=$pmarginx; 
    $sigy+=$pmarginy;  
}; 
$sig_number{'loc'} =~ /[lL][rR]/ && do {
    $sigx+=($cw-$pmarginx); 
    $sigy+=$pmarginy;  
}; 
$sig_number{'loc'} =~ /[uU][lL]/ && do {
    $sigx+=$pmarginx; 
    $sigy+=($ch-$pmarginy);  
}; 
$sig_number{'loc'} =~ /[uU][rR]/ && do {
    $sigx+=($cw-$pmarginx); 
    $sigy+=($ch-$pmarginy);  
}; 


$sigangle=0; # Safety fallback to not mess up postscript
# Set rotation angle and
# move start point "back" 2 times text size 
# I bet this can be done easier too?
$sig_number{'rot'} =~ /[rR]/ && do {$sigangle=270;$sigy+=12;};
$sig_number{'rot'} =~ /[lL]/ && do {$sigangle=90;$sigy-=12;};
$sig_number{'rot'} =~ /[uU]/ && do {$sigangle=180;$sigx+=12;};
$sig_number{'rot'} =~ /[nN]/ && do {$sigangle=0; $sigx-=12;};


# Start and end points for spine collation bar
($startx,$starty)=split /,/ , $coll_bar{'start'};
($stopx,$stopy)=split /,/ , $coll_bar{'end'};

$startx*=$cw;
$starty*=$ch;
$stopx*=$cw;
$stopy*=$ch;


# Bar made modulo 6
$sizex=int(($stopx-$startx)/6);
$sizey=int(($stopy-$starty)/6);

print "Signature location $sigx,$sigy and rotate $sigangle\n";
print "Collation bar start at $startx,$starty and end at $stopx,$stopy\n";


print "Printing signature and collation mark\n";

# Open temporary file for input
open INFILE, $infile or die("Could not find file for input!\n");
open OUTFILE, ">", $outfile or die("Could not create output file (write protected?)\n");
# Print signature on every modulo page

$modulo=$sheets*2 or $modulo=2;
$page = 0;
$signature=1;
while (<INFILE>) {

# Look for page boundaries by DSC comments
    if (/^%%Page:/) {
# Unless wrong page (depends on modulo)
	$page++;
	unless (($page-1) % $modulo) {
	    print OUTFILE;
# Save state so we don't mess up the file
	    print OUTFILE "gsave\n";

# Draw bar
	    print OUTFILE "6 setlinewidth";
	    print OUTFILE "\n", ($startx + (($signature-1) % 6) * $sizex), " " , ($starty + (($signature-1) % 6) * $sizey), " moveto";
	    print OUTFILE "\n", ($startx + (($signature-1) % 6) * $sizex + $sizex), " " , ($starty + (($signature-1) % 6) * $sizey + $sizey), " lineto \n";
	    print OUTFILE "stroke\n";

# Print signature number
	    print OUTFILE "/Helvetica findfont\n";
	    print OUTFILE "6 scalefont\n";
	    print OUTFILE "setfont\n";
	    print OUTFILE "newpath\n";
	    print OUTFILE "$sigx $sigy moveto\n";
	    print OUTFILE "$sigangle rotate\n";
	    print OUTFILE "(";
	    print OUTFILE $signature++ ;
	    print OUTFILE ") show\n";
	    print OUTFILE "grestore\n";

	    $odd = 0;

	} else {

# This page shall not have signature
	    print OUTFILE; 

	} # Check page end
    } else {

# Lines not needing special attention just get copied to output
	print OUTFILE; 
	
    } # if %%Page end

} # While loop end





