--- psmandup.orig Wed Oct 1 20:30:31 2003 +++ psmandup.inkjet Wed Oct 1 20:35:56 2003 @@ -22,6 +22,8 @@ # Author: Akim Demaille +# Inkjet page flipping (-i) by + # Get the name of the program program=`echo $0 | sed 's#.*/##g'` @@ -31,8 +33,12 @@ debug= file= front=: # Print the front side pages. -output=- # Default is stdout +output= # Default is output to printer +flip= # flip sides, suitable on inkjet printers fixps=${FIXPS:-fixps} +fixpsoption= # option for fixps +destination= # printer name to print to +jobname='' message= psselect=${PSSELECT:-psselect} psset=${PSSET:-psset} @@ -59,8 +65,12 @@ -h, --help display this help and exit -v, --version display version information and exit -q, --quiet don't print informational messages + -d, --destination specify printer name (default printer if not specified) + -t, --tag job name -o, --output=FILE save result in FILE. If FILE is \`-', send to stdout -n, --no-fix don't call fixps to fix PS problems in FILE + -w, --fullrewrite call fixps to full rewrite the file with ghostscript + -i, --flip flip sides (suitable for inkjet printers) -f, --front output only the front pages (recto) on the regular tray -b, --back output only the back pages (verso) on the manual @@ -136,13 +146,17 @@ -q | -s | --s* | --q*) verbose=:; quiet=echo;; # Delay debugging so that options parsing does not appear -D | --deb*) debug=-D ;; + -d | --des*) shift ; destination="-d $1" ;; -o | --out*) shift ; output=$1 ;; + -t | --tag*) shift ; jobname=$1 ;; -b | --bac*) front= ; back=: ;; -f | --fro*) front=: ; back= ;; + -i | --fli*) flip=: ;; -) # We are working with stdin ;; set dummy "$@" "$1" shift ;; + -w | --ful*) fixpsoption=-f ;; -n | --no*) fixps= ;; --) # What remains are not options. @@ -187,8 +201,16 @@ mkdir $tmpdir +if test -z "$jobname"; then + if test "$file" = '-'; then + jobname="(stdin)" + else + jobname=$file + fi +fi + # If printing from stdin, save into a tmp file -if test $file = '-'; then +if test "$file" = '-'; then file=$tmpdir/stdin.ps cat > $file fi @@ -196,76 +218,147 @@ # Fix the file beforehand, so that we can really expect to find the # page numbers. if test -n "$fixps"; then - $fixps $file -o $tmpdir/fixed.ps `$quiet -q` + $fixps $fixpsoption $file -o $tmpdir/fixed.ps `$quiet -q` file=$tmpdir/fixed.ps fi # Get the number of pages of the document -pagenum=`sed -ne '/^%%Pages:/{ -s/%%Pages: \\([0-9]*\\).*$/\\1/p -q -}' $file` - -# Make the string to get the front pages (even, increasing) if needed -if test -n "$front"; then - evens="1" - i=3 - # Build the string to give to psselect - while test $i -le $pagenum; - do - evens="$evens,$i" - i=`expr $i + 2` - done -fi - -# Make the string to get the back pages (odd, decreasing) if needed -if test -n "$back"; then - # If the number of pages is odd, we need to insert a blank sheet - case "$pagenum" in - *[13579]) - odds="_" - i=`expr $pagenum - 1 || exit 0` - ;; - *) # Odd, and not 0, so at least >= 2 - odds=$pagenum - i=`expr $pagenum - 2 || exit 0` - ;; - esac +pagenum=`grep '^%%Pages: [0-9]\+$' $file | sort -r | head -n 1 | cut -d: -f2` + +if test -n "$flip"; then - # Make the string to get the second half (odd, decreasing) - while test $i != 0; - do - odds="$odds,$i" - i=`expr $i - 2 || exit 0` - done -fi - -# If there are both odds and evens to print, the separator is `,' -test -n "$odds" && test -n "$evens" && separator=, - -# Reorder the pages -$psselect -q $evens$separator$odds $file > $tmpdir/ordered.ps - -# If needed, insert the manual feed request -if test -n "$back"; then - # The option of psset to ask the manual feed. If not set, - # psset just does nothing. - pssetmanfeed=-m - # Compute the address, depending on front pages are printed or not. + # Make the string to get the front pages (odd, decreasing) if needed if test -n "$front"; then - address=`expr '(' $pagenum + 1 ')' / 2 + 1` - message="\ + case "$pagenum" in + *[13579]) + i=`expr $pagenum - 0 || exit 0` + ;; + *) # Even, do not print last page so make it odd + i=`expr $pagenum - 1 || exit 0` + ;; + esac + + odds="$i" + i=`expr $i - 2` + + # Build the string to give to psselect + while test $i -ge 1; + do + odds="$odds,$i" + i=`expr $i - 2` + done + fi + + # Make the string to get the back pages (even, increasing) if needed + if test -n "$back"; then + case "$pagenum" in + *[02468]) + maxi=`expr $pagenum - 0 || exit 0` + ;; + *) # Odd, do not print the last page so make it even + maxi=`expr $pagenum - 1 || exit 0` + ;; + esac + + evens="2" + i=`expr 4` + + # Make the string to get the second half (odd, decreasing) + while test $i -le $maxi; + do + evens="$evens,$i" + i=`expr $i + 2 || exit 0` + done + + # If the number of pages is odd, we need to insert a blank sheet + case "$pagenum" in + *[13579]) + evens="$evens,_" + ;; + *) # Odd, and not 0, so at least >= 2 + evens="$evens" + ;; + esac + fi +else + # Make the string to get the front pages (even, increasing) if needed + if test -n "$front"; then + evens="1" + i=3 + # Build the string to give to psselect + while test $i -le $pagenum; + do + evens="$evens,$i" + i=`expr $i + 2` + done + fi + + # Make the string to get the back pages (odd, decreasing) if needed + if test -n "$back"; then + # If the number of pages is odd, we need to insert a blank sheet + case "$pagenum" in + *[13579]) + odds="_" + i=`expr $pagenum - 1 || exit 0` + ;; + *) # Odd, and not 0, so at least >= 2 + odds=$pagenum + i=`expr $pagenum - 2 || exit 0` + ;; + esac + + # Make the string to get the second half (odd, decreasing) + while test $i != 0; + do + odds="$odds,$i" + i=`expr $i - 2 || exit 0` + done + fi +fi + +# if there is no output specified, spool it to printer +if test -n "$output"; then + # If there are both odds and evens to print, the separator is `,' + test -n "$odds" && test -n "$evens" && separator=, + + # Reorder the pages + $psselect -q $odds$separator$evens $file > $tmpdir/ordered.ps + + # If needed, insert the manual feed request + if test -n "$back"; then + # The option of psset to ask the manual feed. If not set, + # psset just does nothing. + pssetmanfeed=-m + # Compute the address, depending on front pages are printed or not. + if test -n "$front"; then + address=`expr '(' $pagenum + 1 ')' / 2 + 1` + message="\ Once the first half of the file printed, insert the sheets stack into the manual feed tray to print the second half. Be aware the time out if usually short. If it expired, use option -b to proceed." - else - address=0 - message="Insert the front pages stack into the manual feed tray." + else + address=0 + message="Insert the front pages stack into the manual feed tray." + fi fi -fi -# Insert the manual feed request if needed -$psset -n $pssetmanfeed -a $address -o$output $tmpdir/ordered.ps $debug + # Insert the manual feed request if needed + $psset -n $pssetmanfeed -a $address -o$output $tmpdir/ordered.ps $debug +else + # spool it as two separate jobs, first is started immediately, + # while the other job will be held until it is released manually + # from printer spool system (e.g. cups, http://localhost:631/printers + $psselect -q $odds $file | lp $destination -s -t "$jobname [front]" + $psselect -q $evens $file | lp $destination -s -t "$jobname [back]" -H hold + message="\ +After the first half [front] print job is done, +second half [back] print job is held and waits indefinitely. +Take the sheet stack and rotate it by 180 degrees. +(sheet on top of the rotated stack should still be on top). +Insert the sheet stack again into the printer. +Release job manually from the printer spooler system. +For cups, click (Release Job) at http://localhost:631/jobs" +fi test -n "$message" && $verbose "$message" 1>&2 exit 0