#!/bin/sh

#   Dec 03/08 - F.Majaess (Revised for sa/saiph, ib/dorval-ib, af/alef)
#   Oct 16/06 - F.Majaess (Revised for ma/maia, ns/naos)
#   Jun 19/06 - F.Majaess (Added support for "ps2pdf" conversion)
#   Mar 01/06 - F.Majaess (Revised for rg/rigel)
#   Oct 26/04 - F.Majaess (Add "plfile" to allow specifying the final filename
#                          to be used).
#   Jul 16/03 - F.Majaess (Revise for azur)
#   May 19/98 - F.Majaess (Disable handling of "aaasovl")
#   May 04/98 - F.Majaess (Removed o2000-2)
#   Mar 27/98 - F.Majaess (Added o2000-2)
#   Apr 16/97 - F.Majaess (Make UQAM option on pollux consistent with sx3r)
#   Oct 01/96 - F.Majaess (Added "CCRDCP/NCCRDCP/DOW/NEWDOW/HCDOW/HCNDOW" options)
#   Mar 18/96 - F.Majaess (revise for SX-4)
#   Dec 08/95 - F.Majaess (Replace 'hostname' calls by "$HOSTID")
#   Oct 23/95 - F.Majaess (Added "YORK" option for 'York' site)
#   Jan 17/95 - F.Majaess (Force changing 'QMS' to 'CCRDHP')
#   Dec 22/94 - F.Majaess (Added "CCRDHP/NCCRDHP" options)
#   Nov 08/94 - F.Majaess (Modify for sx3r)
#   Sep 27/94 - F.Majaess (Adjusted for $CCRNTMP .ne. $RUNPATH)
#   May 26/94 - F.Majaess (Added "HCVIC/HCNVIC/HCSUN/HCNSUN/" options)
#   Mar 14/94 - F.Majaess (Added "plunit=UTOR" option) 
#   Oct 25/93 - F.Majaess (Added "plunit=NEWSUN" option) 
#   Oct 12/93 - F.Majaess (Adjust for new "metacod" path)
#   Sep 22/93 - F.Majaess (Added "plunit=NEWVIC" option) 
#   May 20/93 - F.Majaess (Added "plunit=VIC" option) 
#   Mar 30/93 - F.Majaess (Process only if files exist)
#   Feb 17/93 - F.Majaess (Make sure files transfered before deleting)
#   Feb 11/93 - F.Majaess (Remove acrnall and its password occurences)
#   Nov 05/92 - T.Jang    (Added "plunit=UQAM" option)
#   Nov 03/92 - T.Jang    (Added "plunit=no option" for printout only, ie.,
#                          ignore plots)
#   Oct 29/92 - T.Jang    (Replaced NFS copys with ftp gets)
#   Sep 10/92 - T.Jang

#id plottr  - transfer the metafile from the backend machine to the frontend
#id           machine and start either the "todow", "tovic", "tondow", 
#id           "tonvic"  or "toplotr" scripts on the frontend machine.

#   AUTHOR  - T.Jang

#hd PURPOSE - "Plottr" takes the metafile that was generated by a plot program
#hd           on the back-end machine and uses "ftp" to transfer it to the
#hd           front-end machine.  Then on the front-end machine either the
#hd           "todow", "tovic", "tondow", "tonvic" or "toplotr" script is 
#hd           invoked based on whether the metafile is sent to disk or to 
#hd           the laser printer.

#pr PARAMETERS:
#pr
#pr
#pr   PRIMARY
#pr
#pr     plunit    = plot unit to direct the metafile (=CCRDHP)
#pr               = "QMS"   , small size plots (8.5"X11") on QMS LG2200 plotter.(disabled) 
#pr               = "QMSBIG", large size plots (11" X17") on QMS LG2200 plotter.(disabled)
#pr               = "QMS8"  , small size plots (8.5"X11") on QMS LG2000 plotter.(disabled)
#pr               = "DOW"   , metacode to be send to Downsview site.
#pr               = "HCDOW" , metacode to be send to Downsview site, hardcopy generated on "lw".
#pr               = "HCCLMT", metacode to be send to Downsview site, hardcopy generated on "lj4".
#pr               = "CCRDHP", metacode to be send to Downsview site, hardcopy generated on "lj4".
#pr               = "CCRDCP", metocode to be send to Downsview color printer,
#pr                           hardcopy generated on "tek".
#pr               = "NEWDOW", plotting subdirectory to be send to Downsview site.
#pr               = "HCNDOW", plotting subdirectory to be send to Downsview site,
#pr                           hardcopy generated on "lw".
#pr               ="NCCRDHP", plotting subdirectory to be send to Downsview site,
#pr                           hardcopy generated on "lj4".
#pr               ="NCCRDCP", plotting subdirectory to be send to Downsview site,
#pr                           hardcopy generated on color printer "tek".
#pr               = "VIC"   , metacode to be send to Victoria node filesystem.
#pr               = "HCVIC" , metacode to be send to Victoria node filesystem,
#pr                           hardcopy generated.
#pr               = "NEWVIC", plotting subdirectory to be send to Victoria node.
#pr               = "HCNVIC", plotting subdirectory to be send to Victoria node,
#pr                           hardcopy generated.
#pr               = "UTOR"  , metacode to be send to University of Toronto filesystem.
#pr               = "YORK"  , metacode to be generated at York University site.
#pr
#pr     pldir     = Optional final destination "plots" subdirectory full path.
#pr     plpfn     = prefix to use in transit naming of the metacode file (=$jobname)
#pr     plfile    = final filename to use for the transfered file once its saved into 
#pr                 its final destination.

#pr

#ex EXAMPLE:
#ex
#ex     plottr plunit=VIC
#ex
#ex     The above example sends the metafile to Victoria site and place it
#ex     in the user's "plots" sub-directory.

#  * Reset field separators (otherwise those defined in the parent process
#  * will be used and these may cause problems if they include special
#  * characters used in this script).

IFS=' '
export IFS
  set -x
#  * process the input parameters.

arg_list=$@

for arg in $arg_list
do
  case $arg in
      plunit=*) eval "$arg"     ;;
       pldir=*) eval "$arg"     ;;
       plpfn=*) eval "$arg"     ;;
      plfile=*) eval "$arg"     ;;
  esac
done

if [ -z "$plunit" ] ; then
 echo "Abort from PLOTTR: You must specify a value for plunit!" 
 echo "Abort from PLOTTR: You must specify a value for plunit!"  >> haltit
 exit 1
fi

#  * Set the defaults.

if [ -n "$plpfn" ] ; then
  plpfn="${plpfn}_${jobname}"
else
  plpfn="$jobname"
fi
# plpfn=${plpfn:=$jobname}
plpfn=`echo $plpfn | tr '[A-Z]' '[a-z]'`
USER=`echo $USER | tr '[A-Z]' '[a-z]'`
user=`echo $USER | tr '[a-z]' '[A-Z]'`
TmStmp=`date '+%Y%j%H%M%S'`"_$$"
if [ "$SITE_ID" = 'DrvlSC' ] ; then
 # UiD=`echo "$user" | cut -c 1-3`
 # uid=`echo "$USER" | cut -c 1-3`
 # UiD=`accmap | tr '[A-Z]' '[a-z]' | sed 's/^....//'`
 # uid=`accmap | tr '[a-z]' '[A-Z]' | sed 's/^....//'`
 USER=`accmap | tr '[A-Z]' '[a-z]'`
 user=`echo $USER | tr '[a-z]' '[A-Z]'`
 UiD=`echo "$user" | sed 's/^....//'`
 uid=`echo "$USER" | sed 's/^....//'`
elif [ "$SITE_ID" = 'Victoria' ] ; then
# plunit=${plunit:=VIC}
# UiD=`expr "$user" : '.*\(...\)'`
# uid=`expr "$USER" : '.*\(...\)'`
#  UiD=`echo "$user" | sed -e 's/.*\(...\)/\1/'`
 UiD=`echo "$user" | sed 's/^....//'`
 uid=`echo "$USER" | sed 's/^....//'`
elif [ "$SITE_ID" = 'York' ] ; then
# plunit=${plunit:=YORK}
 UiD="$user"
 uid="$USER"
elif [ "$SITE_ID" = 'Downsview' -a "$OS" = 'Linux' ] ; then
 plunit=${plunit:='CLMT'}
 # UiD="$user"
 # uid="$USER"
 UiD=`echo "$user" | sed 's/^....//'`
 uid=`echo "$USER" | sed 's/^....//'`
elif [ "$SITE_ID" = 'UOFT' ] ; then
 plunit=${plunit:='UOFT'}
 UiD="$user"
 uid="$USER"
elif [ "$SITE_ID" = 'ORNS' ] ; then
 plunit=${plunit:='ORNS'}
 UiD="$user"
 uid="$USER"
elif [ "$SITE_ID" = 'Utor1' ] ; then
# plunit=${plunit:=UTOR1}
 UiD="$user"
 uid="$USER"
else
# plunit=${plunit:=CCRDHP}
 UiD=`expr "$user" : 'ACRN\(...\)'`
 uid=`expr "$USER" : 'acrn\(...\)'`
 UiD=${UiD:-`echo "$user" | sed 's/^....//'`}
 uid=${uid:-`echo "$USER" | sed 's/^....//'`}
fi
plunit=`echo $plunit | tr '[a-z]' '[A-Z]'`
case $plunit in
  QMS|QMSBIG|QMS8) plunit='CCRDHP' ;;
  SUN)             plunit='DOW' ;;
  NEWSUN)          plunit='NEWDOW' ;;
  HCSUN)           plunit='HCDOW' ;;
  HCNSUN)          plunit='HCNDOW' ;;
  * )              break ;;
esac

#
if [ \( "$SITE_ID" = 'Dorval' -a \( "$HOSTHW" = 'SX-6' -o "$OS" = 'AIX' -o "$OS" = 'Linux' \) \) -o "$SITE_ID" = 'DrvlSC' ] ; then
 if [ -d "$SITESTORE_ROOT/../ccrn_tmp/metacod/." ] ; then
  LMETACOD="$SITESTORE_ROOT/../ccrn_tmp/metacod"
 else
  LMETACOD="$CCRNTMP/metacod"
 fi
elif [ "$SITE_ID" = 'Downsview' -a "$OS" = 'Linux' ] ; then
 if [ -n "$pldir" ] ; then 
   LMETACOD="$pldir" 
 else 
   LMETACOD="$HOME/plots" 
 fi
elif [ "$SITE_ID" = 'Victoria' -o "$SITE_ID" = 'Downsview' ] ; then
 if [ -n "$pldir" ] ; then 
   LMETACOD="$pldir" 
 else
   LMETACOD="/plots/u${uid}"
 fi
elif [ "$SITE_ID" = 'Downsview' ] ; then
 plunit='DOW'
 if [ -n "$pldir" ] ; then 
   LMETACOD="$pldir" 
 else
   LMETACOD="$HOME/plots"
 fi
elif [ "$SITE_ID" = 'York' ] ; then
 if [ -n "$pldir" ] ; then 
   LMETACOD="$pldir" 
 else
   LMETACOD="$HOME/plots"
 fi
elif [ "$SITE_ID" = 'UOFT' ] ; then
 if [ -n "$pldir" ] ; then 
   LMETACOD="$pldir" 
 else
   LMETACOD="$HOME/plots"
 fi
elif [ "$SITE_ID" = 'ORNS' ] ; then
 if [ -n "$pldir" ] ; then 
   LMETACOD="$pldir" 
 else
   LMETACOD="$HOME/plots"
 fi
elif [ "$SITE_ID" = 'Utor1' ] ; then
 if [ -n "$pldir" ] ; then 
   LMETACOD="$pldir" 
 else
   LMETACOD="$HOME/plots"
 fi
else
 if [ "$plunit" = 'UQAM' ] ; then
  LMETACOD="$HOME/.queue"
 else
  LMETACOD="$RUNPATH/metacod"
 fi
fi

if [ "$plunit" = 'HCDOW' -o "$plunit" = 'HCVIC' \
  -o "$plunit" = 'CCRDHP' -o "$plunit" = 'NCCRDHP' \
  -o "$plunit" = 'CCRDCP' -o "$plunit" = 'NCCRDCP' \
  -o "$plunit" = 'HCCLMT' \
  -o "$plunit" = 'HCNDOW' -o "$plunit" = 'HCNVIC' ] ; then
  print='yes'
else
  print='no'
fi
if [ "$plunit" = 'HCDOW' -o "$plunit" = 'HCNDOW' \
  -o "$plunit" = 'DOW'   -o "$plunit" = 'NEWDOW' ] ; then
 lpid='lw'
fi
# if [ "$plunit" = 'CCRDHP' -o "$plunit" = 'NCCRDHP' ] ; then
if [ "$plunit" = 'CCRDHP' -o "$plunit" = 'HCCLMT' -o "$plunit" = 'NCCRDHP' ] ; then
 lpid='lj4'
fi
if [ "$plunit" = 'CCRDCP' -o "$plunit" = 'NCCRDCP' ] ; then
 lpid='tek'
fi

if [ "$plunit" = 'DOW'   -o "$plunit" = 'VIC'   -o "$plunit" = 'QMS'  \
  -o "$plunit" = 'HCDOW' -o "$plunit" = 'HCVIC' -o "$plunit" = 'UTOR' \
  -o "$plunit" = 'QMS8'  -o "$plunit" = 'UQAM'   -o "$plunit" = 'CCRDHP' \
  -o "$plunit" = 'ORNS'  -o "$plunit" = 'CLMT'  -o "$plunit" = 'HCCLMT' \
  -o "$plunit" = 'UOFT'  -o "$plunit" = 'CCRDCP' -o "$plunit" = 'YORK' ] ; then

#  * create the metafile and overlay file names 

mfile="u${uid}_${plpfn}${TmStmp}"
ofile="u${uid}_${plpfn}${TmStmp}.AAASOVL"


#  * determine the metacod sub-directory 

if [ "$plunit" = 'UQAM' ] ; then
  METACOD=".queue"
else
  if [ \( "$SITE_ID" = 'Dorval' -o "$SITE_ID" = 'DrvlSC' \) -a "$OS" = 'Linux' ] ; then
   METACOD="$LMETACOD"
  else
   METACOD="\$RUNPATH/metacod"
  fi
fi

#  * move the temporary files to the back-end cash data directory
# set -x
if [ -s aaasput ] ; then
 # pltextn=`file aaasput`
 pltextn=`file aaasput 2>/dev/null | egrep -i 'PostScript'`
 # pltextn=`echo $pltextn | sed -n -e 's/^.* PostScript .*$/ps/gp'`
 # pltextn=`echo $pltextn | sed -n -e 's/^.* PostScript .*$/ps/p'`
 # pltextn=${pltextn:-'ncgm'}
 if [ -n "$pltextn" ] ; then
  pltextn='ps'
  if [ -n "$PDFOUTPUT" ] ; then
   PDFOUTPUT=`echo $PDFOUTPUT | tr '[A-Z]' '[a-z]' `
  fi
  if [ "$PDFOUTPUT" = 'yes' -o "$PDFOUTPUT" = 'on' ] ; then
   ps2pdf aaasput aaasput.pdf
   mv aaasput.pdf aaasput
   pltextn='pdf'
  fi
 else
  pltextn='ncgm'
 fi
 Ofile=${plfile:-"u${uid}_${plpfn}${TmStmp}"}
 Ofile="${Ofile}.${pltextn}" 
 # if [ \( "$SITE_ID" != 'Dorval' -a "$SITE_ID" != 'DrvlSC' \)  -o "$plunit" = 'UQAM' ] ; then
 if [ "$SITE_ID" != 'Dorval' -o "$plunit" = 'UQAM' ] ; then
  mfile="$Ofile"
 fi
 (\rm -f $LMETACOD/$mfile || : )
 if [ "$SITE_ID" = 'DrvlSC' ] ; then
  \cp -p aaasput $LMETACOD/$mfile && \rm -f aaasput || : 
 else
  mv aaasput $LMETACOD/$mfile
 fi
 # chmod 660 $LMETACOD/$mfile
 chmod 640 $LMETACOD/$mfile
else
 echo ' PLOTTR: Routing skipped, metacode file is empty!'
 exit
fi

# Victoria/York/UOFT/ORNS/Utor1

if [ "$SITE_ID" = 'Victoria' -o "$SITE_ID" = 'York'  -o "$SITE_ID" = 'UOFT' -o "$SITE_ID" = 'ORNS' -o "$SITE_ID" = 'Utor1' -o "$SITE_ID" = 'Downsview' ] ; then
 if [ "$SITE_ID" = 'Victoria' ] ; then
  if [ "$print" = 'yes' ] ; then
# qsub -q cidsv08 -eo -o $OUTPUTQ/plottr$$ -r plottr <<..eof
  qsub -q vicsmall -eo -o /dev/null -r plottr <<..eof
#!/bin/sh
   set -x
   cd $LMETACOD
   if [ "$plunit" = "HCVIC" -o "$plunit" = "VIC" ] ; then
    tovic ifile=$mfile ofile=$Ofile pldir=$pldir SUBD=U$UiD subpath=$LMETACOD print=$print del=yes
   fi
..eof
  fi
 fi
 ##  if [ "$SITE_ID" = 'Downsview' ] ; then
 ##   cat <<..eof  > $CCRNTMP/tmp_plottr_job$$
 ## #!/bin/sh
 ## # set -x
 ##    cd $LMETACOD
 ##    todow ifile=$mfile ofile=$Ofile SUBD=U$UiD subpath=$LMETACOD print=$print lpid=$lpid del=yes
 ##    rm -f $CCRNTMP/tmp_plottr_job$$
 ## ..eof
 ##   chmod u+x $CCRNTMP/tmp_plottr_job$$
 ## # nohup $CCRNTMP/tmp_plottr_job$$ > $OUTPUTQ/plottr_$$ 2>$OUTPUTQ/plottr_$$ &
 ##   nohup $CCRNTMP/tmp_plottr_job$$ > /dev/null 2> /dev/null &
 ##  fi
 exit
fi

# Disable handling of "aaasovl" file...

(\rm -f aaasovl aaasovl2 || : ) > /dev/null 2>/dev/null
# if [ -s aaasovl ] ; then
#  tail +2 aaasovl > aaasovl2 2>/dev/null
# fi
if [ -s aaasovl2 ] ; then
 if [ "$SITE_ID" = 'DrvlSC' ] ; then
  \cp -p aaasovl2 $LMETACOD/$ofile && \rm -f aaasovl2 || : 
 else
  mv aaasovl2 $LMETACOD/$ofile
 fi
 chmod 660 $LMETACOD/$ofile
 if [ "$SITE_ID" = 'Dorval' -a "$OS" != 'Linux' ] ; then
  (chown acrnsrc $LMETACOD/$ofile || : )
 fi
 getofile="get $ofile"
 chmdofil="chmod 660 $ofile"
 chonofil="chown acrnsrc $ofile"
 delofile="delete $ofile"
else
 getofile='ascii'
 chmdofil='# '
 chonofil='# '
 delofile='ascii'
fi
if [ "$SITE_ID" = 'DrvlSC' ] ; then
 if [ "$plunit" = "VIC" -o "$plunit" = 'HCVIC' ] ; then
   (\rm -f $LMETACOD/${mfile}_tovic_cmd || : )
   # echo "tovic ifile=$mfile ofile=$Ofile pldir=$pldir SUBD=U$UiD subpath=$METACOD print=$print del=yes" > $LMETACOD/${mfile}_tovic_cmd
   # echo "tovic ifile=$mfile ofile=$Ofile pldir=$pldir SUBD=U$UiD subpath=\`pwd\` print=$print del=yes accmap=`accmap`" > $LMETACOD/${mfile}_tovic_cmd
   echo "tovic ifile=$mfile ofile=$Ofile pldir=$pldir SUBD=U$UiD subpath=/fs/cetus3/fs2/crb/metacod print=$print del=yes accmap=`accmap`" > $LMETACOD/${mfile}_tovic_cmd
     chmod 644 $LMETACOD/${mfile}_tovic_cmd
   # (chmod 664 $LMETACOD/${mfile}_tovic_cmd $LMETACOD/${mfile} || : )
 else
   (\rm -f $LMETACOD/${mfile}_todow_cmd || : )
   # echo "todow ifile=$mfile ofile=$Ofile pldir=$pldir SUBD=U$UiD subpath=$METACOD print=$print lpid=$lpid del=yes" > $LMETACOD/${mfile}_todow_cmd
   # echo "todow ifile=$mfile ofile=$Ofile pldir=$pldir SUBD=U$UiD subpath=\`pwd\` print=$print lpid=$lpid del=yes accmap=`accmap`" > $LMETACOD/${mfile}_todow_cmd
   echo "todow ifile=$mfile ofile=$Ofile pldir=$pldir SUBD=U$UiD subpath=/fs/cetus3/fs2/crb/metacod print=$print lpid=$lpid del=yes accmap=`accmap`" > $LMETACOD/${mfile}_todow_cmd
   chmod 644 $LMETACOD/${mfile}_todow_cmd
   # (chmod 664 $LMETACOD/${mfile}_todow_cmd $LMETACOD/${mfile} || : )
 fi
 exit
fi
# mdest=`hostname`
## if [ "$SITE_ID" = 'Dorval' -a \( "$HOSTHW" = 'SX-6' -o "$OS" = 'AIX' \) ] ; then
if [ "$SITE_ID" = 'Dorval' -a "$OS" = 'Linux' ] ; then
 HOSTID=${HOSTID-`hostname | cut -d'.' -f1`}
 HOSTID=`echo $HOSTID | sed -e 's/hpcr4-in/ppp4/' -e 's/hpcr3-in/ppp3/' `
 case $HOSTID in
 # ale*) mdest='alef'      ;;
 ib3*) mdest='pollux' ;;
 ib4*) mdest='castor' ;;
 ib8*) mdest='mez' ;;
  * ) mdest='pollux'      ;;
 esac
# elif [ "$SITE_ID" = 'Dorval' -a \( "$OS" = 'AIX' -o "$OS" = 'Linux' \) ] ; then
elif [ "$SITE_ID" = 'Dorval' -a \( "$OS" = 'AIX' \) ] ; then
 ## if [ "$HOSTHW" = 'SX-6' ] ; then
 ##  mdest='yata'
 ## else
  HOSTID=${HOSTID-`hostname | cut -d'.' -f1`}
  HOSTIDf=`echo $HOSTID | cut -c 1-3`
  case $HOSTIDf in
   c1f*|c1h*|c1r*|c1s* ) mdest='spica' ; CMCFEDEST='pollux'  ;;
   c2f*|c2h*|c2r*|c2s* ) mdest='hadar' ; CMCFEDEST='pollux'  ;;
  #c3f* ) mdest='rigel' ;;
   c4f* ) mdest='maia'  ;;
   c6f* ) mdest='saiph'  ;;
   c7f* ) mdest='zeta'  ;;
   c8f* ) mdest='algol'  ;;
     * ) mdest='azur'  ;;
  esac
 ## fi
else
 mdest=${HOSTID-`hostname | cut -d'.' -f1`}
fi
#  * qsub a job to the front-end to copy the files from the back-end
CMCFEDEST=${CMCFEDEST:='pollux'}
if [ "$SITE_ID" = 'Dorval' ] ; then
 Tqueue='pollux'
 if [ "$OS" = 'Linux' ] ; then
  # Tqueue='ccrnftp' 
  HOSTID=${HOSTID-`hostname | cut -d'.' -f1`}
  case $HOSTID in
  # ale*) Tqueue='alef'      ;;
  ib3*) Tqueue='pollux' ;;
  ib4*) Tqueue='castor' ;;
  ib8*) Tqueue='mez' ;;
    *) Tqueue='pollux' ;;
  esac
 elif [ "$OS" = 'AIX' ] ; then
  if [ "$morigin" = 'pollux' ] ; then 
    Tqueue='pollux' 
  elif [ "$morigin" = 'castor' ] ; then 
    Tqueue='castor' 
  elif [ "$morigin" = 'mez' ] ; then 
    Tqueue='mez' 
  # elif [ "$morigin" = 'alef' ] ; then 
  #  #Tqueue='alef' 
  #   morigin='pollux'
  #   Tqueue='pollux'
  # elif [ "$CMCFEDEST" = 'alef' ] ; then
  # #Tqueue='alef'
  #  CMCFEDEST='pollux'
  #  Tqueue='pollux'
 #elif [ "$CMCFEDEST" = 'pollux' ] ; then
 # Tqueue='pollux'
  fi
 fi
else
 Tqueue=${morigin:=$CMCFEDEST}
fi
PLwd=`pwd`
cd $HOME/tmp
# (cat <<..eof) | qsub -q $Tqueue ${E_NOBODY} -eo -o /dev/null -r plottr 
# qsub -q $Tqueue ${E_NOBODY} -eo -o $OUTPUTQ/plottr_$HOSTID_$$ -r plottr <<..eof
qsub -q $Tqueue ${E_NOBODY} -eo -o /dev/null -r plottr <<..eof
#!/bin/sh
# set -xv
cd $METACOD
if [ "$mdest" != 'pollux' -a "$mdest" != 'castor' -a "$mdest" != 'mez' ] ; then
# /usr/ucb/ftp -i $mdest <<..endftp
# if [ "$SITE_ID" = 'Dorval' ] ; then
# if [ "$SITE_ID" = 'Dorval' -a "$METACOD" != "${LMETACOD}" ] ; then
if [ "$METACOD" != "${LMETACOD}" ] ; then
 scp -p -o NoneSwitch=yes "${mdest}:${LMETACOD}/${mfile}" $METACOD/${mfile} ; chmod u+w $METACOD/${mfile}
 if [ "$getofile" = "get $ofile" ] ; then
  scp -p -o NoneSwitch=yes "${mdest}:${LMETACOD}/${ofile}" $METACOD/${ofile} ; chmod u+w $METACOD/${ofile}
 fi
 if [ -s $METACOD/$mfile ] ; then
  if [ "$delofile" = "delete $ofile" ] ; then
   ssh ${mdest} "cd ${LMETACOD} ; rm -r -f ${mfile} ${ofile}"
  else
   ssh ${mdest} "cd ${LMETACOD} ; rm -r -f ${mfile}"
  fi
 fi
 chmod 660 $METACOD/$mfile
 $chmdofil
 ( chown acrnsrc $METACOD/$mfile || : )
 $chonofil || :
## else
##  ftp -i $mdest <<..endftp
## debug
## cd $LMETACOD
## binary
## get $mfile
## ascii
## $getofile
## quit
## ..endftp
##  chmod 660 $mfile
##  $chmdofil
##  ( chown acrnsrc $mfile || : )
##  $chonofil || :
##  if [ -s $mfile ] ; then
## # /usr/ucb/ftp -i $mdest <<..endftp
##   ftp -i $mdest <<..endftp
## cd $LMETACOD
## ascii
## delete $mfile
## $delofile
## quit
## ..endftp
## fi
fi
fi
if [ "$plunit" = 'DOW'   -o "$plunit" = 'VIC'   -o "$plunit" = 'UTOR' \
  -o "$plunit" = 'HCDOW' -o "$plunit" = 'HCVIC' -o "$plunit" = 'CCRDHP' \
  -o "$plunit" = 'ORNS'  -o "$plunit" = 'CLMT' -o "$plunit" = 'HCCLMT' \
  -o "$plunit" = 'UOFT'  -o "$plunit" = 'CCRDCP' -o "$plunit" = 'YORK' ] ; then
 if [ "$plunit" = "YORK" ] ; then
   echo " plottr: Sorry, YORK option is valid only at York site"
   echo " plottr: Sorry, YORK option is valid only at York site" >> haltit
   exit 1
 elif [ "$plunit" = "CLMT" ] ; then
   echo " plottr: Sorry, CLMT option is valid only in Downsview site"
   echo " plottr: Sorry, CLMT option is valid only in Downsview site" >> haltit
   exit 1
 elif [ "$plunit" = "UOFT" ] ; then
   echo " plottr: Sorry, UOFT option is valid only at UofT site"
   echo " plottr: Sorry, UOFT option is valid only at UofT site" >> haltit
   exit 1
 elif [ "$plunit" = "ORNS" ] ; then
   echo " plottr: Sorry, ORNS option is valid only at UofT site"
   echo " plottr: Sorry, ORNS option is valid only at UofT site" >> haltit
   exit 1
 elif [ "$plunit" = "UTOR1" ] ; then
   echo " plottr: Sorry, UTOR1 option is valid only at Utor1 site"
   echo " plottr: Sorry, UTOR1 option is valid only at Utor1 site" >> haltit
   exit 1
 elif [ "$plunit" = "UTOR" ] ; then
   toutor ifile=$mfile ofile=$mfile pldir=$pldir SUBD=U$UiD subpath=$METACOD del=yes
 elif [ "$plunit" = "VIC" -o "$plunit" = 'HCVIC' ] ; then
   tovic ifile=$mfile ofile=$Ofile pldir=$pldir SUBD=U$UiD subpath=$METACOD print=$print del=yes
 else
   todow ifile=$mfile ofile=$Ofile pldir=$pldir SUBD=U$UiD subpath=$METACOD print=$print lpid=$lpid del=yes
 fi
elif [ "$plunit" = 'QMS' -o "$plunit" = 'QMS8' ] ; then
  toplotr ifile=$mfile plunit=$plunit subpath=$METACOD del=yes
fi
..eof
cd $PLwd

elif [ "$plunit" = 'NEWVIC' -o "$plunit" = 'NEWDOW' -o "$plunit" = 'HCNVIC' \
       -o "$plunit" = 'HCNDOW' -o "$plunit" = 'NCCRDHP' \
                               -o "$plunit" = 'NCCRDCp' ] ; then
 if [ "$SITE_ID" = 'Downsview' ] ; then
  exit
 fi
#set -x

#plotdir="d""$UiD""$TmStmp"
 plotdir="d${uid}_${plpfn}${TmStmp}"
 CMCFEDEST=${CMCFEDEST:='pollux'}
 if [ "$SITE_ID" = 'Dorval' ] ; then
  Tqueue='pollux'
  # if [ "$morigin" = 'dorval-ib' ] ; then Tqueue='dorval-ib' ; fi
  # if [ "$morigin" = 'alef'      ] ; then Tqueue='alef'      ; fi
  if [ "$morigin" = 'pollux'      ] ; then Tqueue='pollux'      ; fi
  if [ "$morigin" = 'castor'      ] ; then Tqueue='castor'      ; fi
  if [ "$morigin" = 'mez'      ] ; then Tqueue='mez'      ; fi
 #if [ "$morigin" = 'alef' -o "$morigin" = 'dorval-ib' ] ; then Tqueue='alef'      ; fi
  if [ "$OS" = 'Linux' ] ; then Tqueue='ccrnftp' ; fi
 else
  Tqueue=${morigin:=$CMCFEDEST}
 fi
 
 if [ -d plotdir ] ; then
# ls -al plotdir
  chmod g+rwx plotdir  ; chmod o+rx plotdir

# Victoria

  if [ "$SITE_ID" = 'Victoria' ] ; then
    \mv plotdir $LMETACOD/$plotdir &&
    tonvic plotdir=$plotdir SUBD=U$UiD subpath=$LMETACOD print=$print && exit || exit 1
  elif [ "$SITE_ID" = 'Downsview' ] ; then
    \mv plotdir $LMETACOD/$plotdir &&
    tondow plotdir=$plotdir SUBD=U$UiD subpath=$LMETACOD print=$print del=yes lpid=$lpid && exit || exit 1
  elif [ "$OS" = 'AIX' -a "$SITE_ID" != 'Dorval' ] ; then
   \mv plotdir $DATAPATH/metacod/$plotdir &&
   tonvic plotdir=$plotdir SUBD=U$UiD print=$print || exit 1
  else
   if [ "$plunit" = 'NEWDOW' -o "$plunit" = 'HCNDOW' -o "$plunit" = 'NCCRDHP' \
                                               -o "$plunit" = 'NCCRDCP' ] ; then
     Tocmd='tondow'
     Tlpid="lpid=$lpid"
   else
     Tocmd='tonvic'
     Tlpid=' '
   fi
   (chmod g+rw plotdir/* || : ) 
   # if [ "$mdest" != 'pollux' ] ; then
   if [ "$mdest" != 'pollux' -a "$mdest" != 'castor' -a "$mdest" != 'mez' ] ; then
#    \mv plotdir $RUNPATH/metacod/$plotdir 
    set -x
    \mv plotdir $CCRNTMP/metacod/$plotdir  &&
#   rcp -rp $CCRNTMP/metacod/$plotdir $Tqueue:/data/ccrn/gen/metacod &&
    scp -o NoneSwitch=yes -rp $CCRNTMP/metacod/$plotdir $Tqueue:/data/ccrn/gen/metacod &&
    \rm -rf $CCRNTMP/metacod/$plotdir &&
    PLwd=`pwd` &&
    cd $HOME/tmp &&
    qsub -q $Tqueue ${E_NOBODY} -eo -o /dev/null -r plottr << ..eof &&
 #! /bin/sh
# set -x
 $Tocmd plotdir=$plotdir SUBD=U$UiD subpath=/data/ccrn/gen/metacod print=$print del=yes $Tlpid
..eof
    cd $PLwd
   else
    \mkdir -m 755 $LMETACOD/$plotdir &&
    \cp plotdir/* $LMETACOD/$plotdir &&
    \rm -rf plotdir  &&
#    set -x &&
     $Tocmd plotdir=$plotdir SUBD=U$UiD subpath=/data/ccrn/gen/metacod print=$print del=yes $Tlpid
   fi
  fi
 fi
 exit 

fi 
