#! /bin/sh
 
#    SEP 07/93 - F.Majaess (enforce using Victoria node batch line)
#    FEB 25/93 - F.Majaess (add script switch)
#    FEB 08/93 - F.Majaess (enable submitting the transferred file)
#    MAR 02/92 - F.Majaess
 
#id  fget    - Used to transfer file from ccrn server to current machine.
 
#    AUTHOR  - F.Majaess
 
#hd  PURPOSE - "fget" script is used to  transfer "fln" file from ccrn sun 
#hd            main server to the current machine. The user have the choice 
#hd            of submitting the transfered file to the back/front end as
#hd            well as not saving the transfered file at all on the current
#hd            machine (front-end).
#hd            NOTE : If "fln" is a "CRAWORK" file to be submitted, then
#hd                   "fln" must be saved on the front-end (i.e. nosvaf=no).
 
#pr  PARAMETERS:
#pr
#pr    POSITIONAL     
#pr
#pr      fln    = ccrn sun filename to be transfered.
#pr               (the same name is used to store the file on the local
#pr                machine if "nosvaf"='no').
#pr
#pr    PRIMARY/SECONDARY
#pr
#pr      nosvaf = switch controlling saving accessed file on the front-end
#pr               (=no/yes).
#pr      ppath  = path of subdirectory containing output from submitted job 
#pr               (=$OUTPUTQ/$PRNTQ).
#pr      sjtfe  = switch controls job submission to the front-end instead 
#pr               of the back-end (=no/yes).
#pr      subaf  = switch accomodating user request to submit an accessed
#pr               file from another machine (=no/yes).
#pr      script = switch to generate a copy of the pre-processed job
#pr               script (i.e the next job in the string) without sending
#pr               the job to NQS (diagnostic jobs only)

#pr
#pr    PRIMARY
#pr
#pr      fepath = path to user's favourite directory on front-end 
#pr               account (=$FEPATH).
#pr      sunpath= path to user's favourite directory on ccrn sun's 
#pr               account (=$SUNPATH).
#pr      sv     = ccrn sun server net address (=$RMTSV)
#pr
 
#ex  EXAMPLE: 
#ex
#ex  fget fln=myjob fepath=$HOME/work sunpath='\\\$HOME/back-end-jobs' subaf
#ex  fget fln=myjob sunpath='\\\$HOME/back-end-jobs' subaf nosvaf
#ex  fget fln=myjob sunpath='\\\$HOME/back-end-jobs' subaf nosvaf sjtfe ppath
 
#   * 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
 
#   * make sure arguments list variable "prmtrl" is initialized...
 
prmtrl=${prmtrl=}
  
#   * 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;;
      *) 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 ccrn sun filename > \\c"
  read fln
done
 
if [ "$nosvaf" = "yes" -a "$subaf" = "no" ] ; then
  eval "echo '$0: ' "
  eval "echo illegal to access a remote file without saving it "
  eval "echo nor submitting it ! "
  exit
fi
 
#   ****   Task of the script...   ****
 
#   * setup needed parameters...
# 
 
if [ "$sv" = "$VICSV01" ] ; then
  sv='ftp1.cccma.ec.gc.ca'
fi
 
Tpid=$$
 
if [ "$subaf" = "yes" ] ; then
 if [ "$sjtfe" = "yes" ] ; then
  Site=" " 
 else
  Site=" sx3 " 
 fi
 if [ "$nosvaf" = "no" ] ; then
  Sbfil=" $fepath/$fln "
 else
  Sbfil=" $ppath/$fln.$Tpid "
 fi
 Tsubcmd=" submit3 $Sbfil $Site $script "
else
 Sbfil=" $fepath/$fln "
 Tsubcmd=": "
fi
#
#   * Abort if "$Sbfil" is an existing subdirectory... 
# 
if  [ -d "$Sbfil" ] ; then
  eval "echo '$0: $Sbfil is a subdirectory ! ' "
  exit 1
fi
#
#   * Check if "$Sbfil" file already exists ... 
#   *          (get user's authorization to replace it)
# 
if  [ -s "$Sbfil" ] ; then
  eval "echo '$0: Existing $Sbfil contents will be lost ' "
  echo ' if that is ok, enter <CR> '
  echo ' to abort, type ^c or ... '
  read prmtr
  ( \rm -rf $Sbfil || : ) 
fi
 
# tupdir1="rcp -p $sv:'$sunpath/$fln' $fepath/$fln && "
 
nohupout="$ppath/fget""_""$fln""_""$Tpid""_out"
nohuperr="$ppath/fget""_""$fln""_""$Tpid""_err"
 
# set -x
# set -v
# cat << FGETwoof | more
( cat << FGETwoof ) | ( eval "nohup sh - 1>>$nohupout 2>>$nohuperr & " )
#! /bin/sh
## ( /usr/ucb/ftp -i -v $sv << woof ) &&
## del $Sbfil
## binary
## get $sunpath/$fln $Sbfil 
## woof
scp -o NoneSwitch=yes "$sv:./$sunpath/$fln" $Sbfil &&
ls -al $Sbfil &&
if [ -s $Sbfil ] ; then
 :
else
 exit 2
fi &&
$Tsubcmd &&
(\rm -rf $ppath/$fln.$Tpid || : ) &&
( cat << FDELwoof ) | ( eval "nohup sh - 1>>/dev/null 2>> /dev/null & " ) &&
sleep 45
\rm $nohupout $nohuperr
FDELwoof
exit 0 ||
exit 3 
FGETwoof
exit
