#!/usr/local/bin/ksh
#
#  queue files for printing on vax
#  Skip Montanaro, GE Corporate Research & Development
#  montanaro@sprite.steinmetz.ge.com
#

# where ftp gets login information
netrc=${HOME}/.netrc

# defaults
dhost=${REMOTEVAX:=iagvax}
user=${user:-$LOGNAME}
password=${password}
machine=$dhost
queue=sys\$print:
num=1
class=`hostname`
fortran=cat
files=
number=
lpp="-l60"
savelogin=TRUE
args="$*"
netrcentry=FALSE

help () {
  echo ''
  echo "usage:"
  echo "   $0 [ options ] file1 file2 ..."
  echo ""
  echo "prints file1, file2, ... using the options specified. Some attempt"
  echo "has been made to use options similar to those of pr and lpr,"
  echo "however, this is not always desirable or possible. Notice that"
  echo "wherever an option takes an argument, the option and the argument"
  echo "are always separated by white space. Note also that all arguments"
  echo "come before all files and that all options set are valid for all"
  echo "files."
  echo ""
  echo "options (defaults are shown in parens):"
  echo "  -P machine  machine on which to print file		($dhost)"
  echo "  -Q queue    copy file into queue on machine		($queue)"
  echo "  -# num      print num copies of each file		($num)"
  echo "  -C class    print class on burst page as host		(`hostname`)"
  echo "  -f          files contain FORTRAN carriage control"
  echo "  -n          number the lines of each file"
  echo "  -N          do not save login information in ~/.netrc"
  echo "  -l lpp      use lpp as the number of lines per page	(60)"
  echo "  -[hH]       print this message and exit"
  echo ""
}

# Bourne shell function to print out a colon, banner-style, since the SysV
# banner command doesn't do it right.

colon () {
echo "   # "
echo "  ###"
echo "   # "
echo "     "
echo "   # "
echo "  ###"
echo "   # "
}

# determine if we are running background - is this a hack?
tty 2> /dev/null > /dev/null
if [ $? -eq 0 ] ; then
  background=FALSE
else
  background=TRUE
fi

# make sure .netrc file exists
if [ ! -f ${netrc} -a $savelogin = TRUE ] ; then
  cat /dev/null > ${netrc}
  chmod 600 ${netrc}
fi

grep $machine ${netrc} >/dev/null 2>/dev/null
netrcentry=$?
if [ $netrcentry -eq 0 ] ; then
  netrcentry=TRUE
else
  netrcentry=FALSE
fi

while [ $# -gt 0 ] ; do

  case $1 in
	-P) machine=$2; shift		;;
	-Q) queue=$2; shift		;;
	-#) num=$2; shift		;;
	-C) class=$2; shift		;;
	-f) fortran=/usr/ucb/fpr	;;
	-n) number=-n10			;;
	-l) lpp="-l$2"; shift		;;
	-N) savelogin=FALSE;		;;
	-[hH]) help; exit 0		;; 
	*)  files="$files $1"		;;
  esac

  shift

done

if [ $netrcentry = FALSE -a $savelogin = TRUE -a $background = FALSE ] ; then

  echo -n "Name ($machine:${user}): "
  read user
  if [ "x$user" = "x" ] ; then
    user=$LOGNAME
  fi

  echo -n "Password ($machine:$user): "
  stty -echo
  read password
  stty echo
  echo ""

  echo "machine" $machine "login" $user "password" $password >> ${netrc}

fi

# spin off to background if:
#   not already running in background *and*
#   ftp won't need to prompt for username and password
if [ $background = FALSE -a \( $savelogin = TRUE -o $netrcentry = TRUE \) ]
then
  (user=$user password=$password $0 $args &) &
  exit 0
fi

# for each file, set up a useful banner page for the operators to use to
# identify your printout, and concatenate the file with the banner using one
# of several optional filters.

rm -f /tmp/tzip$$

set $files
if [ $# -gt 0 ] ; then

  for file in $files ; do

    if [ -f $file ] ; then

      echo -n 'XXXXXXXXXX    000000000000000000000000000000'	>>/tmp/tzip$$
      echo -n '00000000000000000000000000000000000000000000'	>>/tmp/tzip$$
      echo    '000000000000000000000000000000    XXXXXXXXXX'	>>/tmp/tzip$$
      echo -n 'XXXXXXXXXX    000000000000000000000000000000'	>>/tmp/tzip$$
      echo -n '00000000000000000000000000000000000000000000'	>>/tmp/tzip$$
      echo    '000000000000000000000000000000    XXXXXXXXXX'	>>/tmp/tzip$$
      echo -n 'XXXXXXXXXX    000000000000000000000000000000'	>>/tmp/tzip$$
      echo -n '00000000000000000000000000000000000000000000'	>>/tmp/tzip$$
      echo    '000000000000000000000000000000    XXXXXXXXXX'	>>/tmp/tzip$$
      /usr/5bin/banner ""					>>/tmp/tzip$$
      /usr/5bin/banner "USER"					> /tmp/trash1$$
      /usr/5bin/banner " $user"					> /tmp/trash2$$
      colon | paste /tmp/trash1$$ - /tmp/trash2$$		>>/tmp/tzip$$
      /usr/5bin/banner "FILE"					> /tmp/trash1$$
      /usr/5bin/banner " `basename $file`"			> /tmp/trash2$$
      colon | paste /tmp/trash1$$ - /tmp/trash2$$		>>/tmp/tzip$$
      /usr/5bin/banner "HOST"					> /tmp/trash1$$
      /usr/5bin/banner " $class"				> /tmp/trash2$$
      colon | paste /tmp/trash1$$ - /tmp/trash2$$		>>/tmp/tzip$$
      if [ $num -gt 1 ] ; then
	/usr/5bin/banner "COPIES"				> /tmp/trash1$$
	/usr/5bin/banner " $num"				> /tmp/trash2$$
	colon | paste /tmp/trash1$$ - /tmp/trash2$$		>>/tmp/tzip$$
      else
	/usr/5bin/banner ""					>>/tmp/tzip$$
      fi
      /usr/5bin/banner ""					>>/tmp/tzip$$
      echo "File: `hostname`:$file"				>>/tmp/tzip$$
      echo "`ls -ld $file | \
		awk '{printf \"Last saved: %3s %2s %5s\", $5, $6, $7}'`" \
								>>/tmp/tzip$$
      echo ""							>>/tmp/tzip$$
      echo -n 'XXXXXXXXXX    000000000000000000000000000000'	>>/tmp/tzip$$
      echo -n '00000000000000000000000000000000000000000000'	>>/tmp/tzip$$
      echo    '000000000000000000000000000000    XXXXXXXXXX'	>>/tmp/tzip$$
      echo -n 'XXXXXXXXXX    000000000000000000000000000000'	>>/tmp/tzip$$
      echo -n '00000000000000000000000000000000000000000000'	>>/tmp/tzip$$
      echo    '000000000000000000000000000000    XXXXXXXXXX'	>>/tmp/tzip$$
      echo -n 'XXXXXXXXXX    000000000000000000000000000000'	>>/tmp/tzip$$
      echo -n '00000000000000000000000000000000000000000000'	>>/tmp/tzip$$
      echo    '000000000000000000000000000000    XXXXXXXXXX'	>>/tmp/tzip$$
      echo ""							>>/tmp/tzip$$

      $fortran < $file | /usr/5bin/pr $lpp -f -e -t  $number	> /tmp/tfile$$
      echo ""							>>/tmp/tfile$$

      i=$num
      while [ $i -gt 0 ] ; do
	cat /tmp/tfile$$					>>/tmp/tzip$$
	i=`expr $i - 1`
      done

    fi

  done

  #
  #  all files prepared for printing. ship to vax.
  #
  (echo "lcd /tmp"; echo "put tzip$$ $queue" ; echo "quit" ) | \
	ftp $machine						> /dev/null

fi

#
#  clean up intermediate files
#

rm -f /tmp/tfile$$ /tmp/tzip$$ /tmp/trash[12]$$
