#!/bin/sh
#################################################
## dplx v0.5                                   ##
## Emulates duplex printing for dedicated HP   ##
## Deskjet printer on a workstation            ##
## Calling sequence:                           ##
##  dplx [-opt "<lp common parameter list>"]   ##
##       [<print-file>|<->]                    ##
##                                             ##
## Copyright (c) 21 Jun 2003 Joseph J. Pollock ##
##   JPmicrosystems - josephj at main.nc.us    ##
##                                             ##
## 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              ##
#################################################

function max() {
  # max - return the maximum of an arbitrary list of
  #       unsigned 8-bit numeric arguments
  #       nonnumerics and nulls treated as 0
  local i rc
  typeset -i i rc

  rc=0
  for i in $*
  do
    if [ "$i" -gt "$rc" ]
    then
      rc=$i
    fi
  done

  return $rc
}

name=$(basename $0) # path stripped name of this script
usage="$name [-opt \"<lp common parameter list>\"] [<print-file>|<->]"

namlen=${#name}  # string length of name
nl=$(( $namlen+18 ))  # Length of script name
                      # + room for infobox title line
uselen=${#usage} # string length of usage
dsptime=20000    # Miliseconds to display error messages
xdopt="--title $name --auto-placement"  # Xdialog common options
if [ "$1" = "--help" ]  # if first argument is --help
then                      # then echo a usage message
  echo "$name: usage is $usage"  # and exit
  exit 0
fi

e=""            # Null string so duplex is called
duplex="duplex" # duplex printing script - production version
# e="echo "     #For debugging - disable call to duplex
# duplex="/home/bigbird/pgm/duplex.0.5"   # Use the test version

# put your lp options for printing each side here
pass1="-o page-set=odd -o outputorder=reverse"
pass2="-o page-set=even -o outputorder=normal"

if [ "$1" = "-opt" ]  # check for optional common parameters flag
then
  shift

  if [ -z "$1" ]  # if flag was found, then parameters 
  then            # are mandatory
    Xdialog $xdopt --backtitle "Usage Error" --infobox "Usage is\n\n
$usage\n\n
Aborting..." 12 $uselen $dsptime

    exit 1
  else  # common parameters found
#    pass1="$pass1 $1"
#    pass2="$pass2 $1"
    pass1="$1 $pass1"
    pass2="$1 $pass2"
    echo "pass1 [$pass1]"  # for debugging
    echo "pass2 [$pass2]"
    shift
  fi
fi

if [ -z "$1" ] || [ "$1" = "-" ]  # If no file argument, 
then                              #  use "-" for stdin
  f="-"
else            # otherwise, use file argument
  f=$1
  if [ ! -e "$f" ]  # if the file does not exist
  then
    fl=$(( ${#f}+13 ))    # Length of file name + message text
    max $fl $nl 
    len=$?

    Xdialog $xdopt --infobox "[$f] Not found\n\n Aborting..."\
 8 $len $dsptime
  
    exit 1
  fi
fi

# if e is null, call duplex
# otherwise e="echo" - echo the calling sequence for debugging
$e $duplex "$pass1" "$pass2" "$f"  
                                            
exit $?  # return the status returned by duplex
