#! /bin/sh
# -*- ksh -*-

# psmandup --- produce a version of a PS file to print in manual Duplex.

# Copyright (c) 1988, 89, 90, 91, 92, 93 Miguel Santana
# Copyright (c) 1995, 96, 97, 98 Akim Demaille, Miguel Santana

# $Id: psmandup.in,v 1.12 1998/07/07 16:05:27 demaille Exp $

# 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, 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, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.

# Commentary:

# Author: Akim Demaille <demaille@inf.enst.fr>

# In the interest of general portability, some common bourne shell
# constructs were avoided because they weren't guaranteed to be available
# in some earlier implementations.  I've tried to make this program as
# portable as possible.  Welcome to unix, where the lowest common
# denominator is rapidly diminishing.
#
# Among the more interesting lossages I noticed with some bourne shells
# are:
#     * No shell functions.
#     * No `unset' builtin.
#     * `shift' cannot take a numeric argument, and signals an error if
#       there are no arguments to shift.

# Code:

# Minimal path.  It must be able to see the psutils.
PATH=$PATH:/usr/local/bin
export PATH

# Get the name of the program
program=`echo $0 | sed 's#.*/##g'`

# Local vars
debug=
file=
output=
fixps=${FIXPS:-fixps}
tmpdir=/tmp/$program.$$
verbose=echo

# The version/usage strings
version="psmandup 1.0 (a2ps 4.10.4)

Copyright (c) 1988, 89, 90, 91, 92, 93 Miguel Santana
Copyright (c) 1995, 96, 97, 98 Akim Demaille, Miguel Santana
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Written by <Akim.Demaille@inf.enst.fr> and <Miguel.Santana@st.com>
News, updates and documentation: visit http://www.inf.enst.fr/a2ps/"

usage="\
Usage: $program FILE
Tries to produce a version of the PostScript FILE to print in manual
Duplex.

Options:
 -h, --help           display this help and exit
 -v, --version        display version information and exit
 -q, --quiet          don't print informational messages
 -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

Produced output is meant for PS level 2 printers which don't support
Duplex printing, but support Manual Feed.  Once the first set of pages
is printed (odd pages), manual feed is asked: introduce the odd pages
to print the even pages on the other side.

Report bugs to <a2ps-bugs@inf.enst.fr>"

help="Try \`$program --help' for more information."

# The PostScript line to ask manual feed
# We go and pick up setpagedevice because the psutils (here psselect)
# define an ineffective setpagedevice.
# I hope setpagedevice is always in systemdict...
psmanualfeed='% Pagedevice definitions:
countdictstack [{
%%BeginFeature: *ManualFeed True
  (<<) cvx exec /ManualFeed true (>>) cvx exec
  systemdict /setpagedevice get exec
%%EndFeature
} stopped
cleartomark
countdictstack exch sub dup 0 gt
{
  { end } repeat
}{
  pop
} ifelse'

# Don't leave really new lines in it
sedmanualfeed="`echo "$psmanualfeed" | tr '\n' ''`"

# Parse command line arguments.
option_without_arguments='vhsqDnf'
while test $# -gt 0; do
  
  # Handle --option=value by splitting apart and putting back on argv.
  case "$1" in
    --*=*)
      opt=`echo "$1" | sed -e 's/=.*//'`
      val=`echo "$1" | sed -e 's/[^=]*=//'`
      shift
      set - "$opt" "$val" "$@"
      ;;

    -[$option_without_arguments]?*)
      opt=`echo "$1" | sed -e 's/-\\(.\\).*/-\1/'`
      rest=`echo "$1" | sed -e 's/-.\\(.*\\)/-\1/'`
      shift
      set - "$opt" "$rest" "$@"
      ;;

    # This case needs to be protected so that the case `-??*' does
    # not split long options without arguments
    --*)
      ;;

    # This is an option non without argument.  It can be either an error,
    # or a option with argument.  In the first case, the error will still
    # be catched though the `argument' has been separated.
    -??*)
      opt=`echo "$1" | sed -e 's/-\\(.\\).*/-\1/'`
      arg=`echo "$1" | sed -e 's/-.\\(.*\\)/\1/'`
      shift
      set - "$opt" "$arg" "$@"
      ;;
  esac
  
  # This recognizes --heltch as --help.  So what.
  case "$1" in
    --h*|-h)  echo "$usage";   exit 0  ;;
    --v*|-v)  echo "$version"; exit 0  ;;
    -s|-q|--q*|--s*) verbose=: ;;
    -D|--debug) debug=: ;;
    
    --o*|-o)
      shift
      output=$1
      ;;
    
    -n|-f|--no-fix) fixps= ;;

    -) # We are working with stdin
       nonopt="$nonopt $1" ;;

    -*)
      exec 1>&2
      echo "$program: Unknown or ambiguous option \`$1'."
      echo "$program: Try \`--help' for more information."
      exit 1
      ;;
    
    *) nonopt="$nonopt $1"  ;;
  esac
  shift
done

case `echo "$nonopt" | wc -w | sed -e 's/[\t ]//g'` in
  0)  file=-;;
  1)  file=$nonopt;;
  *)  echo "$program: too many arguments" 1>&2
      echo "$help"  1>&2
      exit 1;;
esac

# Set -x now if debugging
test $debug && set -x

# Temp dir.  Get ready not to leave junk (if not debugging)
test $debug || trap "/bin/rm -rf $tmpdir" 0 1 2 3 13 15
mkdir $tmpdir

# If printing from stdin, save into a tmp file
if test $file = '-'; then
  file=$tmpdir/stdin.ps
  cat > $file
fi

# Fix the file
if test -n "$fixps"; then
  $fixps $file -o $tmpdir/fixed.ps `test $verbose = ":" && echo "-q"` \
    || exit 1
  file=$tmpdir/fixed.ps
fi

# Get the number of pages of the document
pagenum=`grep '^%%Pages: ' $file | sed -ne '1s/%%Pages: \\([0-9]*\\).*$/\\1/gp'`

# Make the string to get the first half (even, increasing)
psselectarg="1"
i=3
# Build the string to give to psselect
while test $i -le $pagenum;
do
  psselectarg="$psselectarg,$i"
  i=`echo "$i + 2" | bc`
done

# If the number of pages is odd, we need to insert a blank sheet
if echo "$pagenum" | grep '[13579]$' > /dev/null 2>&1; then
  psselectarg="$psselectarg,_"
  i=`echo "$pagenum - 1" | bc`
else
  i=$pagenum
fi

# Make the string to get the second half (odd, decreasing)
while test $i != 0;
do
  psselectarg="$psselectarg,$i"
  i=`echo "$i - 2" | bc`
done

# Reorder the pages
psselect -q $psselectarg $file > $tmpdir/ordered.ps || exit 1

# Compute the number of the first sheet of the second half
midpagenum=`echo "(($pagenum + 1) / 2) + 1" | bc`

# Insert the manual feed request at the first page of the second half
# This page has as page num, $MIDPAGENUM.  
# Make sure to skip human readable page number.
if test "$output" && test "$output" != "-"; then
  sed -e "s#^%%Page:\(.* $midpagenum[ \t]*\)\$#%%Page:\1$sedmanualfeed#g" \
     $tmpdir/ordered.ps | tr '' '\n' > $output || exit 1
else
  sed -e "s#^%%Page:\(.* $midpagenum[ \t]*\)\$#%%Page:\1$sedmanualfeed#g" \
     $tmpdir/ordered.ps | tr '' '\n' || exit 1
fi

# Don't remove files if debugging
test $debug || /bin/rm -rf $tmpdir

$verbose "Once the first half of the file printed, insert the sheets" 1>&2
$verbose "stack into the manual feed tray to print the second half." 1>&2
exit 0

