#!/bin/sh
#
# 2up - reformat postscript output at about 1/2 normal size, 2 per page
#
# 2up [-r] [-l[logofile]] [-c[notice] [-poutputfile] [file...]
#		[-P[printer] lpr options]
#
# 2up is a filter that takes conformant postscript input from standard input
# or from "file(s)" and resets it 1/2 width and 1/2 height, squeezing two per
# page.  The page also contains lines for notes down the right side and an
# optional logo at the bottom right.  If the input contains an odd number of
# pages, the last page contains lines for notes across the entire bottom half.
#
# Options are:
#  -r: reverse the page order of the output file.  (Some printers need this
#	and other don't).
#  -l: print a logo on bottom of each page.  If no file is specified
# 	use the standard (ACE) logo which is about 1 inch square.  If
#	a file name follows contiguously after the -l, the named file
# 	must contain a postscript function named 2up_logo that will paint the
# 	desired logo.  2up_logo is called with current point set to where
# 	center of the logo goes.  Note: The area for logo will already be
#	painted with lines for notes, so 2up_logo should paint background
#	white to cover lines if desired.
#  -c: print a copyright notice at the bottom of each page.  If no copyright
#	notice follows contiguously, use the default (ACE) copyright.
#  -p: send output to a file instead of standard output.  A file name must
#	follow contiguously after the -p.  This option and the -P option are
#	mutually exclusive.
#  file: arguments not beginning with "-" are taken to be names of files
# 	used as input to 2up.  If multiple files are specified, 2up assumes
# 	they each contain postscript for a single page of output and do NOT
# 	contain standard headers or trailers (i.e., it assumes they are hand-
#	written postscript programs, NOT the output from troff).
#  -P: pass output to postscript printer using the Unix "lpr" command instead
#	of writing it to standard output.  If a printer name is specified, 2up
#	passes the entire argument "-Pprinter" as the first argument to lpr.
# 	Anything on the command line following "-P" will be passed to the lpr
#	command as arguments.
# 	NOTE: -P AND PRINTER OPTIONS MUST FOLLOW ALL OTHER ARGUMENTS.
#
# Authors:
#	Douglas Comer and Yongguang Zhang
#	Computer Science Department
#	Purdue University
#	West Lafayette, IN 47907
#	(comer@purdue.edu)
#	
# Version 4.0 of 11/18/89
#
PATH=/usr/local/bin:/bin:/usr/bin:/usr/ucb
export PATH
TMPF=/tmp/2up$$.ps
TMPI=/tmp/2up$$i.ps
trap 'rm -f $TMPF $TMPI; exit' 1 2 3 13 15
# UNIX commands invoked (possibly nonstandard - check local system)
REVCMD=psrev				# postscript page reverse
LPRCMD=lpr				# filter that prints postscript
REVFLAG=
INFILE=
LPROPTS=
OUTFILE=
PRINT=
MERGE=false
PSCONV=false
LOGOFILE=
PRINTLOGO=false
PRINTCOPY=false
COPYRIGHT="Copyright (c) 1989 by Advanced Computing Environments. All Rights Reserved."
AWKPROG='BEGIN { oddpage = 0 }
	/^%%Pages:/ {if ($2=="(atend)") print;
			else print "%%Pages: " int(($2+1)/2);
		     next;}
	/^%%Page:/  {if (($3)%2 == 1)
			{print "%%Page: " $2 " " int(($3+1)/2); oddpage=1}
		     else {oddpage = 0}
		     next;}
	/^%%Trailer/{if (oddpage==1)
			print "2up_oddpgout % eject half-empty page"}
		    {print}'
# Process arguments
PROG=$0
while [ $# -gt  0 ] ; do
    case $1 in
	-r) REVFLAG=1;;
	-p*)OUTFILE=`expr "$1" : '-p\(.*\)'`;;
	-c*)PRINTCOPY=true
	    if [ "x$1" != 'x-c' ]; then
		COPYRIGHT=`expr "$1" : '-c\(.*\)'`
	    fi;;
	-l*)PRINTLOGO=true
	    LOGOFILE=`expr "$1" : '-l\(.*\)'`;;
	-P*)if [ "x$OUTFILE" != x ]; then
		echo "$PROG: Options -p and -P cannot both be specified."
		exit
	    fi
	    PRINT=true
	    if [ "x$1" = "x-P" ]; then		# -P alone
		shift
	    fi;
	    LPROPTS="$LPROPTS $*"
	    break;;
	-*) # illegal option
	    echo 'use: 2up [-r] [-l[logofile]] [-poutfile] [file...] [-P[printer] [lpr options]]'
	    exit;;
	*)  if [ "x$INFILE" != x ]; then
		MERGE=true
	    fi;
	    INFILE="$INFILE $1";;
    esac
    shift
done

if $MERGE; then
    echo '%!PS-Adobe-1.0' > $TMPI
    echo "%%Creator: $0 " >>$TMPI
    echo "%%CreationDate: `date`" >>$TMPI
    echo '%%Pages: (atend)' >>$TMPI
    echo '%%EndProlog' >>$TMPI
    page=0
    for file in $INFILE ; do
        page=`expr $page + 1`
        echo "%%Page: $page $page" >> $TMPI
        cat $file >> $TMPI
    done
    echo '%%Trailer' >>$TMPI
    echo "%%Pages: $page" >>$TMPI
    INFILE=$TMPI
    PSCONV=true
    echo "%!PS-Adobe-1.0" > $TMPF
else
    if [ "x$INFILE" = x ]; then
        cat > $TMPI
        INFILE=$TMPI
    fi
    magic=`head -1 $INFILE`
    echo "$magic" > $TMPF
    if [ `expr "$magic" : '%!PS-Adobe.*'` != 0 ]; then
        PSCONV=true
    fi
fi
if $PRINTLOGO; then
    if [ "x$LOGOFILE" != "x" ]; then
	cat $LOGOFILE >> $TMPF
    else
	cat << \DEFAULT_LOGO >> $TMPF
/2up_logo {					% default logo
gsave currentpoint translate
newpath -36 -36 moveto				% paint white 1 inch square
0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath
1 setgray fill
-12 -18 moveto					% move to lower circle
18 18 rlineto 0 18 rlineto -18 0 rlineto 0 -18 rlineto 18 0 rlineto
0 setgray stroke
-12 -18 moveto					% move back to lower circle
currentpoint 5.4 0 360 arc fill
6 0 moveto					% move to rotated box
gsave
  currentpoint translate 45 rotate newpath -5 -5 moveto
  0 10 rlineto 10 0 rlineto 0 -10 rlineto closepath fill
grestore
0 18 rmoveto					% move to top circle
gsave
  currentpoint 5.4 0 360 arc fill
grestore
-18 0 rmoveto				% move to top box
gsave
  -5 -5 rmoveto currentpoint
  newpath moveto 0 10 rlineto 10 0 rlineto 0 -10 rlineto closepath fill
grestore
0 -18 rmoveto				% move to triangle
gsave
  currentpoint newpath moveto
  -5 5 rmoveto 10 0 rlineto -5 -10 rlineto closepath fill
grestore
} bind def
DEFAULT_LOGO
    fi
else
    echo '/2up_logo { } bind def' >> $TMPF	# no logo at all
fi
if $PRINTCOPY; then
    echo "/2up_cpy { ($COPYRIGHT) show } bind def" >> $TMPF
else
    echo '/2up_cpy { } bind def' >> $TMPF
fi
cat << \END_2UP_PRELUDE >> $TMPF
/2up_font /Helvetica findfont 20 scalefont store
/2up_cfont /Helvetica findfont 6 scalefont store
/2up_outlinepg {gsave initgraphics currentlinewidth 2 mul setlinewidth
53 18  moveto 0 375 rlineto 272 0 rlineto 0 -375 rlineto -272 0 rlineto
53 398 moveto 0 375 rlineto 272 0 rlineto 0 -375 rlineto -272 0 rlineto
stroke currentlinewidth 3 div setlinewidth
340 700 moveto 1 1 24 { 225 0 rlineto -225 -30 rmoveto } for stroke
418 740 moveto 2up_font setfont (NOTES) show
52    9 moveto 2up_cfont setfont 2up_cpy
556  40 moveto 2up_logo grestore
} bind def
/2up_oddpgout  {gsave initgraphics currentlinewidth 2 mul setlinewidth
53 398 moveto 0 375 rlineto 272 0 rlineto 0 -375 rlineto -272 0 rlineto
stroke currentlinewidth 3 div setlinewidth
340 700 moveto 1 1 11 { 225 0 rlineto -225 -30 rmoveto } for
-285 0 rmoveto 1 1 13 { 513 0 rlineto -513 -29 rmoveto } for stroke
418 740 moveto 2up_font setfont (NOTES) show
52    9 moveto 2up_cfont setfont 2up_cpy
556  40 moveto 2up_logo grestore
systemdict /showpage get exec [ .47 0 0 .49 45 392 ] concat
/2up_even_page 2up_even_page not store
} bind def
/showpage { 2up_even_page
{2up_outlinepg systemdict /showpage get exec [ .47 0 0 .49 45 392 ] concat}
{ initgraphics [ .47 0 0 .49 45 12 ] concat } ifelse
/2up_even_page 2up_even_page not store
} bind def
/2up_even_page false store
[ .47 0 0 .49 45 392 ] concat
END_2UP_PRELUDE
if $PSCONV; then
    if [ "$REVFLAG" != "" ]; then
	awk "$AWKPROG" $INFILE | $REVCMD >> $TMPF
    else
	awk "$AWKPROG" $INFILE >> $TMPF
    fi
else
    cat $INFILE >> $TMPF
    cat << \END_2UP_POSTLUDE >> $TMPF
%
2up_even_page { 2up_oddpgout } if
END_2UP_POSTLUDE
fi
if [ $OUTFILE ]; then
    cp $TMPF $OUTFILE
elif [ "$PRINT" != "" ]; then
    cat $TMPF | $LPRCMD $LPROPTS
else
    cat $TMPF
fi
rm -f $TMPF $TMPI
