#! /bin/sh
 
#    OCT 28/92 - F.Majaess ( Replace LSH by LI )
#    FEB 21/92 - F.Majaess
 
#id  qgdest  - Handles output file disposition for "QG".
 
#    AUTHOR  - F.Majaess
 
#hd  PURPOSE - "qgdest" script is used to handle (job) output disposition
#hd            for "qg" of "fln" file in "ppath" subdirectory.
#hd            Note: a job is assembled and submitted directly to the 
#hd                  mainframe controlling the output printer in the 
#hd                  back-ground using the network connection (TCP/IP...).
 
#pr  PARAMETERS:
#pr
#pr    POSITIONAL     
#pr
#pr      fln    = (job) output local filename to be handled by "qgdest".
#pr      destin = disposition option
#pr               (E-evict, K-move to $PRNTQ, P-print on "un", 
#pr                T-transfer to $RMTSV($OUTPUTQ)).
#pr      lilo   = parameter holding file printing script name
#pr               (only valid if (destin=P); valid values LI/LO).
#pr
#pr    PRIMARY/SECONDARY
#pr
#pr      keep   = switch controls keeping "fln" file (=no/yes).
#pr               (ignored if destin=E).
#pr      ppath  = path of subdirectory containing "fln" (=$OUTPUTQ/$PRNTQ).
#pr
#pr    PRIMARY
#pr
#pr      ncopies= number of copies requested (=1)
#pr      time   = back-end CPU time in seconds (=1)
#pr      mfl    = back-end MFL      in words   (=100000)
#pr      box    = box to dispose printout if desired (=$BOX).
#pr      un     = usernumber for printout (=$DESTIN). 
#pr      una    = user's account  (=$USER).
#pr      sv     = CCRN sun server net address (=$CCRN).
 
#ex  EXAMPLE: 
#ex
#ex    qgdest fln=output_queue_fln destin=p lilo=LI keep un=ccrn 
  
#   * code used to set switches and deal with parameter=value arguments 
#   * as well as setting parameters to their primary/secondary defaults.
#   * The list of other arguments (if any) is returned in "prmtrl" var...
 
. $SUBPROC/check_set_swtches_prmtrs
 
cd $ppath

#   * make sure arguments list variable "prmtrl" is initialized...
 
prmtrl=${prmtrl=}
  
#   * code used to set hard coded primary defaults...
 
eval "ncopies=${ncopies='1'}" 
eval "time=${time='1'}" 
eval "mfl=${mfl='100000'}"
 
#   * The following code deals with checking/setting positional 
#   * parameters ...
#   * Note: script will abort if too many arguments are specified.
 
if [ -n "$prmtrl" ] ; then
  i=1
  for prmtr in $prmtrl
  do
    case $i in
      1) fln=$prmtr;;
      2) destin=`echo $prmtr | tr 'epkt' 'EPKT'` ;;
      3) if [ "$destin" = "P" ] ; then
	   lilo=`echo $prmtr | tr 'lio' 'LIO'` 
         else
           eval "echo '$0 : destin=$destin, too many parameters !' " 
	   exit 
	 fi  ;;
      *) eval "echo '$0 : too many parameters !' " ; exit ;;
    esac
    i=`expr $i + 1`
  done
fi
 
#   * Prompt for a positional parameter value(s) if none was/were
#   * specified...
 
while [ -z "$fln" ] ; do
  echo "please enter output filename  > \\c"
  read fln
done
 
while [ "$destin" != "E" -a "$destin" != "P"  \
     -a "$destin" != "K" -a "$destin" != "T" ] ; do
  echo "please enter disposition option: "
  echo "E-evict, T-transfer to $RMTSV (\$OUTPUTQ),"
  echo "P-print on $un, K-move to $PRNTQ > \\c"
  read destin 
  destin=`echo $destin | tr 'epkt' 'EPKT'`
done
 
if [ "$destin" = "P" ] ; then
  if [ "$lilo" != "LI" -a "$lilo" != "LO" ] ; then 
    echo "please enter file printing script utility name; valid values: "
    echo "LO for   shifted file for printer carriage control character"
    echo "LI for unshifted file (default) > \\c "
    read lilo 
    lilo=`echo $lilo | tr 'lio' 'LIO'` 
    if [ "$lilo" != "LI" -a "$lilo" != "LO" ] ; then 
       lilo='LI'  
    fi
  fi
fi
 
i=1
prmtrl=$fln
for prmtr in $prmtrl
do
  case $i in
    1) fln=$prmtr;;
    *) break ;;
  esac
  i=`expr $i + 1`
done

#   ****   Task of the script...   ****
 
#   * check if a valid filename is specified, and process as requested ...
  
if [ -f $ppath/$fln -a -s $ppath/$fln -a -r $ppath/$fln ] ; then
  if [ "$keep" = "no" ] ; then
    rmv='yes'
  else
    rmv='no'
  fi
  case $destin in
  E ) \rm $ppath/$fln  ;
      eval "echo '$0 : $ppath/$fln file removed as requested ' " ;;
 #P ) eval "$SUBPROC/rprint fln=$ppath/$fln Tcaller=$lilo  \\
  P ) eval "rprint fln=$ppath/$fln Tcaller=$lilo  \\
	rmv=$rmv ncopies=$ncopies time=$time mfl=$mfl \\
	box=$box un=$un una=$una " ;
      eval "echo ' $ppath/$fln file is forwarded to $un ' " ;;
K|T ) \mv $ppath/$fln $PRNTQ/$fln  ;
      eval "echo '$0 : $fln file moved to $PRNTQ subdirectory' " ; 
      if [ "$destin" = "T" ] ; then
       #sunpath=`rsh $sv 'echo $OUTPUTQ'`
        sunpath=`ssh $sv 'echo $OUTPUTQ'`
        eval "echo ' and it will be tranferred to $sv ' " 
        eval "echo ' $sunpath subdirectory in the background' " 
        eval "$CCRNSTD/fput fln=$fln rmv=$rmv ppath=$PRNTQ \\
	      fepath=$PRNTQ sunpath=$sunpath sv=$sv" 
      fi ;;
  * ) eval "echo '$0 : illegal value destin=$destin !' " ;;
  esac
  exit
else
  eval "echo $ppath/$fln file is not valid !"
  exit 1
fi
