#!/usr/local/bin/perl

######################################################################
#  Perl font split impose
#
#  Copyright (C) 2003 Dov Grobgeld <dov@imagic.weizmann.ac.il>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# 
#  For more details see the file COPYING.
######################################################################

my $VERSION = "0.2";

# Get options
while($_ = $ARGV[0], /^-/) {
    shift;
    /^-lastbbox/ and do { $lastbbox="-lastbbox"; next; };
    /^-lpi/      and do { $lpi = shift; next; };
    /^-impose_options/ and do { $impose_options = shift; next; };
    /^-max(_pages)?/ and do { $max_pages_per_signature = shift; next; };
    /^-help/ and print<<HELP and exit;
psbl -- A program for automatic booklet formatting of postscript files

SYNTAX:
    psbl [-lastbbox] [-lpi] [-impose_options io] [-max_pages mp]
         postscript.file

SEEALSO:
    impose 
HELP
    die "Unknown option $_!\n";
}

$lpi = 80 unless $lpi;

# Defaults
$max_pages_per_signature = 60 unless $max_pages_per_signature;

# Get the name of the orginial file
$psname = shift || die "Need name of ps file!\n";

# Get number of pages in file
$pages = get_num_pages($psname);

# Split it nicely
$num_signatures = int($pages/$max_pages_per_signature + 0.9999999);

$q_per_signature = int($pages/4/$num_signatures+0.999);
print "num_signatures = $num_signatures\n";
print "q_per_signature = $q_per_signature\n";

for $i (0..$num_signatures-1) {
    my $start = $i * $q_per_signature * 4 + 1;
    my $end = ($i+1) * $q_per_signature * 4;
    if ($end > $pages) {
	$end = $pages;
    }
    $cmd = "psselect $start-$end $psname | fixtd -tumble | psbook > $psname.$start-$end";
    print "$cmd\n";
    system $cmd;

    $lastbbox = "-lastbbox" if $i > 0;
    $cmd = "impose $impose_options $lastbbox $psname.$start-$end";
    print "$cmd\n";
    system $cmd;

    unlink "$psname.$start-$end";
}
print "Done!\n";

sub get_num_pages {
    my $fn = shift;
    my $pages;
    open(PS, $fn) || die "Couldn't open $fn: $!\n";
    while(<PS>) {
	if ($. == 1) {
	    die "Not a Postscript file!\n" unless /^%!/;
	}
	$pages = $_ and last if /^%%Pages:/;
    }
    die "Couldn't find %%Pages!\n" unless $pages;

    if ($pages =~ /atend/) {
	seek(PS, -2000, 2);
	read(PS, $_, 2000);
	/^%%Pages:.*$/m or die "Couldn't find %%Pages at end of file!\n";
	$pages = $&;
    }

    $pages=~ /(\d+)\S*(\d*)/;
    return $1;
}
