#!/bin/sh
#################################################
## duplex v0.5                                 ##
## Emulates duplex printing for a non-duplex   ##
## dedicated printer on a workstation          ##
## Calling sequence:                           ##
## duplex "<pass one lp parameters>"           ##
##        "<pass two lp parameters>"           ##
##        <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 \"<pass one lp parameters>\" \"<pass two lp parameters>\" <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
e=""         # Null string so lp is called
# e="echo "  # for debugging - just echo the actual call to lp

if [ "$1" = "--help" ]   # if first argument is --help
then                     # then echo a usage message
  echo "$name: Usage is $usage"  # and exit
  exit 0
fi

# echo "arg1 [$1]"  # for debugging
# echo "arg2 [$2]"
# echo "arg3 [$3]"

if [ -z "$3" ] # File name must be non-null
then
  Xdialog $xdopt --infobox "Usage is\n\n
$usage\n\n
Aborting..." 10 $uselen $dsptime
 
  exit 1
fi

if [ "$3" = "-" ]  # if input is from stdin
then
  tp="/tmp/"  # temp file path
  TMPFILE=$(/bin/mktemp -q $tp.XXXXXX)  # create temp file
  if [ $? -ne 0 ]    # if create tempfile failed
  then
    tl=$(( ${#tp}+22+10 ))   # Length of temp file path
                             # + message text + margins 
    max $nl $tl           #size dialog box width
    len=$?

    Xdialog $xdopt --backtitle "Can't create temp file"\
 --infobox "Check permissions for $tp\n\n
Aborting..." 10 $len $dsptime
    exit 1
  fi

  cat > $TMPFILE  # copy stdin to temp file
  fn="stdin"  # file name for messages
  f=$TMPFILE  # file name for commands
else          # not using stdin
  fn="$3"     # file name for messages
  f="$3"      # file name for commands
fi

if [ "$f" != "-" ] && [ ! -e "$f" ]   # If the file is not stdin,
then                                  #   it must exist
  fl=$(( ${#f}+13 ))    # Length of file name + message text
  max $fl $nl           # Find the widest line
  len=$?

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

if [ -s "$f" ]  # if file contains data
then
  $e lp $1 "$f"  # print pass 1
  if [ $? -ne 0 ] # if lp failed
  then
    fl=$(( ${#f}+15 ))  # string length of file line
    max $fl $nl         # Find the widest line
    len=$?
    Xdialog $xdopt --infobox "Error printing $fn\n\n
Aborting..." 8 $len $dsptime
    if [ -e $TMPFILE ]  # if temp file was created
    then
      rm -f $TMPFILE    # delete it
    fi 

    exit 1
  fi
else  #  file was empty
  fl=$(( ${#f}+13 ))    # Length of file name + message text
  max $fl $nl           # Find the widest line
  len=$?

  Xdialog $xdopt --infobox "Empty file [$fn]\n\n Aborting..."\
 8 $len $dsptime
  
  if test -e $TMPFILE  # if temp file was created
  then
    rm -f $TMPFILE     # delete it
  fi 

  exit 1
fi

fl=$(( $flen+23 ))  # string length of file line
max $fl 40      # Find the longest line
len=$?

Xdialog $xdopt --backtitle "Printing $fn - Please Wait" --yesno\
 "When finished, either\n\n
Rotate and reinsert the pages\n
Then, Click Yes to Print the other sides\n\n
or\n\n
Click No to Abort" 16 $len && \
$e lp $2 "$f"  # if user clicks "yes" or presses "Enter"
                # then print pass 2
if [ -e $TMPFILE ]  # if temp file was created
then
  rm -f $TMPFILE    # delete it
fi

exit 0
