#! /bin/sh
 
#    JUN 17/08 - F.Majaess
 
#id  be2lnx - Transfer data from back-end to Linux front-end.
 
#    AUTHOR  - F.Majaess
 
#hd  PURPOSE - "be2lnx" script is used to transfer "target" from back-end
#hd            unix "tarpath" directory to front-end server "despath" 
#hd            directory.
#hd            NOTE : Removal of "target" is controlled by "rmv" parameter. 
 
#pr  PARAMETERS:
#pr
#pr    POSITIONAL     
#pr
#pr      target = name of target file/subdirectory to transfer. 
#pr               (same name is used on destination machine) 
#pr
#pr    PRIMARY/SECONDARY
#pr
#pr      subdir = switch indicating type of "target"; 'no' for file (default)
#pr               and 'yes' for subdirectory (=no/yes).
#pr      metafl = switch indicating "target" file(s) is(are) metacode 
#pr               file(s) (=no/yes). 
#pr      rmv    = switch controls deletion of "target" once the transfer
#pr               is complete (=no/yes). 
#pr      noprint= switch controls output generation; "yes" (or "on") no output
#pr               (=no/yes).
#pr
#pr    PRIMARY
#pr
#pr      Note: One of 'ha' or 'sp' is to be specified.
#pr
#pr          ha = to target job to IBM (hadar).
#pr
#pr          sp = to target job to IBM (spica).
#pr
#pr  fil_grpname= User has the option of specifying one of
#pr               'ccrn_shr/ccrn_stf/ccrn_net/ccrn_rcm/ccrn_mam/ccrn_gst'
#pr               group ownership for officially saved transfered 
#pr               files.
#pr               (=$DEFFGRP).
#pr
#pr      tarpath= subdirectory in which "target" data is residing 
#pr               on back-end machine (=$BERUNPATH).
#pr               (=$BERUNPATH/metacod if metafl=yes)
#pr      despath= subdirectory in which the transfered data get stored
#pr               on front-end destination machine (=$RUNPATH).
#pr               (=$RUNPATH/metacod if metafl=yes)
#pr      time   = back-end CPU time in seconds (=600)
#pr      mfl    = back-end MFL                 (=250MB)
#pr      vmode  = access code on "target"      (=750)
#pr               (=770 if metafl=yes)
 
#ex  EXAMPLE: 
#ex
#ex      be2lnx fil_grpname='ccrn_shr' mxx99zz.123 ha
#ex
#ex    The above command, executed on the front-end, requests 
#ex    tansferring "mxx99zz.123" dataset from HADAR back-end 
#ex    and officially "save" it on the front-end with "ccrn_shr" 
#ex    group ownership.
#ex
#ex      be2lnx ha subdir=yes rmv=yes tarpath=/fs/dev/crb/had01/data mydirectory 
#ex
#ex    The above command requests tansferring and removing subdirectory 
#ex    "mydirectory" residing under /fs/dev/crb/had01/data from HADAR.
#ex
#ex      be2lnx sa rmv=yes tarpath=/fs/dev/crb/spd01/data metafl 'URXX*' 
#ex
#ex    The above command requests tansfering and removing all the files 
#ex    starting with 'URXX' from metacod subdirectory under 
#ex    /fs/dev/crb/spd01/data on SPICA to metacod subdirectory on the 
#ex    front-end. 
#ex    Note: "wild-card" style expansion is limited only to this case.
#ex
#ex
#ex      be2lnx filexx.001 tarpath='/fs/dev/crb/had02/data' \
#ex                        despath='/fs/dev/crb/spd02/data' ha
#ex
#ex    The above command, executed on SPICA back-end, requests transfer
#ex    of /fs/dev/crb/had02/data/filexx.001 file on HADAR back-end
#ex    into /fs/dev/crb/spd02/data subdirectory on SPICA back-end.

OS=${OS:-`uname -s`}
HOSTID=${HOSTID-`hostname | cut -d'.' -f1`}
HOSTIDf=`echo $HOSTID | cut -c 1-3`
# if [ "$OS" != 'Linux' ] ; then
# if [ "$OS" != 'Linux'  -o \( "$OS" = 'Linux' -a "$HOSTIDf" = 'erg' \) ] ; then
if [ "$OS" != 'AIX' -a \( "$OS" != 'Linux'  -o \( "$OS" = 'Linux' -a \( "$HOSTIDf" = 'erg' -o "$HOSTIDf" = 'jou' \) \) \) ] ; then
#tty -s && InTrCtv='true' || InTrCtv='false'
#if [ "$InTrCtv" = 'true' ] ; then
# exec ssh mez "be2lnx $*"
  exec ssh pollux "be2lnx $*"
#else
# echo "Sorry; be2lnx must be invoked from Linux front-end"
# exit 1
#fi
fi 

#   * 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...
 
#  *  check if switches/parameters specified on script call
 
if [ $# -gt 0 ] ; then
  
#  * put argument list in "prmtrl" ...
   
  prmtrl=$@
# prmtrl=`echo $prmtrl | tr '[A-Z]' '[a-z]'`
  
#  * for each argument specified do...
    
  for prmtr in $prmtrl
  do
    case $prmtr in
 
#   * check for standard shell switches and set them accordingly
 
     --|-|[-+][aefhkntuvx]) set `echo $prmtr ` ;;
 
#   * case where a parameter is to be assigned a value
 
    *=*   ) eval $prmtr ;;
 
#   * destinations

       ha) mdest=${mdest:='hadar'}       ;;
       sp) mdest=${mdest:='spica'}       ;;

#   * anything else case skip...
 
    *  )  tprmtr="$tprmtr $prmtr";;
 
    esac
  done
  prmtrl=$tprmtr
  tprmtr=
 
fi
prmtrl=${prmtrl=}

#   * Check/set parameters to the secondary defaults...
 
if [ -n "$prmtrl" ] ; then
  
  tprmtr=
 
#  * for each argument undelt with yet do...
  
  for prmtr in $prmtrl
  do
      
    case $prmtr in
 
#   * check for keyword(s) then 
#   *     if the corresponding parameter is already set then
#   *        store "keyword" in "tprmtr" so that it is treated 
#   *        later on as a value to a positional parameter
#   *     otherwise set parameter whose name is "keyword" to 
#   *     its secondary default.
 
 subdir ) if [ -n "$subdir" ] ; then
            tprmtr="$tprmtr $prmtr"
          else
            subdir='yes' 
          fi ;;
 
  metafl) if [ -n "$metafl" ] ; then
            tprmtr="$tprmtr $prmtr"
          else
            metafl='yes' 
          fi ;;
 
    rmv ) if [ -n "$rmv" ] ; then
            tprmtr="$tprmtr $prmtr"
          else
            rmv='yes' 
          fi ;;

  noprint) noprint='yes' ;;
 
#   * anything else case accumulate in "tprmtr" temporary var...
 
    *  )  tprmtr="$tprmtr $prmtr";;
 
    esac
  done
  prmtrl=$tprmtr
  tprmtr=''
fi

 
#   * 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) target=$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 "$target" ] ; do
  echo "please enter file\/subdirectory name ? > \\c"
  read target 
done
 

#  * Prompt for a destination if none was specified.

while [ -z "$mdest" ]
do
  echo "please enter a destination; ha/sp: > \\c"
  read tmdest
  case $tmdest in
      ha) mdest='hadar'                 ;;
      sp) mdest='spica'                 ;;
       *) echo "illegal destination ! "      ;;
  esac
done
if [ "$mdest" = 'hadar' ] ; then
  mext='ha'
elif [ "$mdest" = 'spica' ] ; then
  mext='sp'
else
  mext='sss'
fi

#   * code used to set hard coded primary defaults...
 

eval "metafl=${metafl='no'}"
noprint=${noprint='no'}
if [ "$noprint" = 'on' ] ; then
 noprint='yes'
elif [ "$noprint" = 'off' ] ; then
 noprint='no'
fi
eval "rmv=${rmv='no'}"
eval "time=${time='600'}"
eval "mfl=${mfl='250MB'}"
eval "subdir=${subdir='no'}"
if [ -z "$tarpath" -a "$mdest" = 'spica' ] ; then
 if [ "$OS" = 'AIX' ] ; then BERUNPATH=${BERUNPATH:=$RUNPATH} ; fi
 BERUNPATH=`echo $BERUNPATH | sed -e 's/\/fs\/dev\/crb\/[mh]ad/\/fs\/dev\/crb\/spd/'`
elif [ -z "$tarpath" -a "$mdest" = 'hadar' ] ; then
 if [ "$OS" = 'AIX' ] ; then BERUNPATH=${BERUNPATH:=$RUNPATH} ; fi
 BERUNPATH=`echo $BERUNPATH | sed -e 's/\/fs\/dev\/crb\/[ms][ap]d/\/fs\/dev\/crb\/had/'`
fi
if [ "$metafl" = "yes" ] ; then
  vmode='770'
  eval "tarpath=${tarpath=$BERUNPATH/metacod}"
  eval "despath=${despath=$RUNPATH/metacod}"
else
  if [ "$subdir" = 'yes' ] ; then
   eval "vmode=${vmode='750'}"
  else
   eval "vmode=${vmode='640'}"
  fi
  eval "tarpath=${tarpath=$BERUNPATH}"
  eval "despath=${despath=$RUNPATH}"
fi
# fil_grpname=${fil_grpname:='ccrn_shr'}
DEFFGRP=${DEFFGRP:='ccrn_shr'}
fil_grpname=${fil_grpname:=$DEFFGRP}
if [ -n "$fil_grpname" ] ; then
 Tfil_grpname="fil_grpname=$fil_grpname && export fil_grpname "
else
 Tfil_grpname=" : "
fi
 
subdir=`echo $subdir | tr '[A-Z]' '[a-z]'`
rmv=`echo $rmv | tr '[A-Z]' '[a-z]'`

 
#   * Make sure file/subdirectory to be transfered does not exist 
#   *                                                on front-end,
#   * otherwise, issue an abort message and exit.
 
 
if [ "$metafl" != "yes" ] ; then
if [ -f "$despath/$target" -o -d  "$despath/$target" ] ; then
 eval "echo $despath/$target already exists !"
 exit 1
else
  :
fi
fi
cd $OUTPUTQ &&
Pid=$$ &&
Lhost=${HOSTID:=`hostname | cut -d'.' -f1`} &&
Stamp="${Lhost}_"`date +%Y%j%H%M%S` &&
 
Mainfrm=${HOSTID:=`hostname`} &&
HOSTIDf=`echo $Mainfrm | cut -c 1-3` &&
# if [ "$HOSTIDf" = 'ib3' -o "$HOSTIDf" = 'ib4' -o "$HOSTIDf" = 'ib8' -o "$HOSTIDf" = 'jou' ] ; then
if [ "$HOSTIDf" = 'ib3' -o "$HOSTIDf" = 'jou' ] ; then
# if [ "$HOSTIDf" = 'ib3' ] ; then
 Mainfrm='pollux'
elif [ "$HOSTIDf" = 'ib8' ] ; then
# elif [ "$HOSTIDf" = 'ib8' -o "$HOSTIDf" = 'jou' ] ; then
 Mainfrm='mez'
elif [ "$HOSTIDf" = 'ib4' ] ; then
 Mainfrm='castor'
elif [ "$HOSTIDf" = 'c1f' -o "$HOSTIDf" = 'c1h' -o "$HOSTIDf" = 'c1r' -o "$HOSTIDf" = 'c1s' ] ; then
 Mainfrm='spica'
elif [ "$HOSTIDf" = 'c2f' -o "$HOSTIDf" = 'c2h' -o "$HOSTIDf" = 'c2r' -o "$HOSTIDf" = 'c2s' ] ; then
 Mainfrm='hadar'
fi &&
Outq=$OUTPUTQ &&
sbmtcmd=" qsub -q $Mainfrm -r be2lnx -lT $time -lM $mfl -eo -o " &&

Tmpfl="/tmp/tmp.be2lnx.ftptmp.${Pid}_${Stamp}" &&
if [ "$noprint" = 'yes' ] ; then
 Outqfl="/dev/null"
else
 Outqfl="$Outq/be2lnx.${Mainfrm}.$target.outerr.$Pid"
fi &&
if [ "$subdir" != "yes" -a "$metafl" != "yes" ] ; then
  ed=`expr $target : '.*\.\([0-9][0-9][0-9]\)$' | sed -n -e '1,$p'` &&
  if [ -z "$ed" ] ; then
    Tflnm=`echo $target | tr '[a-z]' '[A-Z]' `
  else
    Tflnm=`echo $target | sed 's/....$//' | tr '[a-z]' '[A-Z]' `
  fi &&
  flnm=`echo $Tflnm | tr '[A-Z]' '[a-z]' `
fi &&
 
#   ****   Task of the script...   ****
 
#   * Assemble and submit the job ...
 
# set -x
# set -v
 Cache_sync_file="./.be2lnx_cache_sync_""`date -u '+%Y%j%H%M%S%s'`""_$$" &&
# cat << woofsub  &&
(cat << woofsub | $sbmtcmd $Outqfl ) &&
#! /bin/sh
 echo "" && echo "  be2lnx: - \`hostname\` - transferring $tarpath/$target to $despath : " &&
 $Tfil_grpname &&
 cd $despath &&
 RUNPATH=$despath &&
 export RUNPATH &&
 Cache_sync_file="$Cache_sync_file"  &&
 CnFs='no' && CnFs=\`pwd | sed -e 's/^\/cnfs\/.*$/yes/'\` &&
 CeTs='no' && CeTs=\`pwd | sed -e 's/^\/fs\/cetus\/.*$/yes/'\` &&
 if [ "\$CnFs" = 'yes' -o "\$CeTs" = 'yes' ] ; then
  Cwd=\`pwd\` 
 fi && 
 hostname &&
 date | sed -n -e 's/^/0 be2lnx : /p'  &&
if [ "$metafl" = "yes" ] ; then
  target='$target' &&
##  # (/usr/ucb/ftp -i -v $mdest << wooftp )  &&
##   (ftp -i -v $mdest << wooftp )  &&
##   ascii
##   cd $tarpath 
##   ls \$target $Tmpfl
##   quit 
## wooftp
  ssh $mdest "cd \$target; ls -ld \$target" > $Tmpfl &&
  if [ -f "$Tmpfl" -a -s "$Tmpfl" ] ; then 
   Tnlst=\`cat $Tmpfl | cut -d' ' -f1| sed -n -e 's/^.*\// /' -e 's/\$/ /' -e '1,\$p' | tr '\\012' '\\040'\` &&
   (\rm $Tmpfl || : ) 
  else
   echo "" && echo "  be2lnx: $tarpath/\$target appears to be empty ! " &&
   exit 1
  fi &&
 set -x &&
 # (/usr/ucb/ftp -i -v $mdest << wooftp )  &&
#  (ftp -i -v $mdest << wooftp )  &&
#  cd $tarpath 
#  binary
#  mget \$Tnlst 
#  quit 
# wooftp
 for prmtr in \$Tnlst
  do
   # rcp $mdest:$tarpath/\$prmtr \$prmtr  &&
    if [ "\$CnFs" = 'yes' ] ; then
    #ssh datasvr "cd \$Cwd ; srcp -p -b 4M $mdest:$tarpath/\$prmtr \$prmtr ; chmod u+w \$prmtr" 
     ssh datasvr "cd \$Cwd ; scp -p -o NoneSwitch=yes $mdest:$tarpath/\$prmtr \$prmtr ; chmod u+w \$prmtr" 
    elif [ "\$CeTs" = 'yes' ] ; then
    #ssh cetus "cd \$Cwd ; srcp -p -b 4M $mdest:$tarpath/\$prmtr \$prmtr ; chmod u+w \$prmtr" 
     ssh cetus "cd \$Cwd ; scp -p -o NoneSwitch=yes $mdest:$tarpath/\$prmtr \$prmtr ; chmod u+w \$prmtr" 
    else
     scp -p -o NoneSwitch=yes $mdest:$tarpath/\$prmtr \$prmtr  ; chmod u+w \$prmtr 
    #srcp -p -b 4M $mdest:$tarpath/\$prmtr \$prmtr  ; chmod u+w \$prmtr
    fi &&
    :
  done &&
 set +x &&
 if [ "\$CnFs" = 'yes' -o "\$CeTs" = 'yes' ] ; then
  (\rm -f \${Cache_sync_file} || : ) && echo 'Sync' > \${Cache_sync_file} && (\rm -f \${Cache_sync_file} || : ) 
 fi &&
 for prmtr in \$Tnlst
  do
  if [ -f "\$prmtr" -a -s "\$prmtr" ] ; then 
    : 
  else
    exit 1
  fi
  done &&
  hostname &&
  date | sed -n -e 's/^/0 be2lnx : /p'  &&
  set -x &&
  chmod $vmode \$Tnlst &&
  echo "" && echo "" && echo "  be2lnx: $despath/\$target :" && echo " " &&
  ls -al $despath/\$target &&
  if [ "$rmv" = "yes" ] ; then
 # (/usr/ucb/ftp -i -v $mdest << wooftp )  
##   (ftp -i -v $mdest << wooftp )  
##   cd $tarpath 
##   mdel \$Tnlst 
##   quit 
## wooftp
   ssh $mdest "cd $tarpath ; rm -r -f \$Tnlst"
  fi && exit || exit 1
else
 if [ "$subdir" = "yes" ] ; then
  descmd1="cd $tarpath/$target" &&
## #  (/usr/ucb/ftp -i -v $mdest << wooftp )  &&
##   (ftp -i -v $mdest << wooftp )  &&
##   ascii
##   \$descmd1 
##   ls .\/ $Tmpfl
##   quit 
## wooftp
  ssh $mdest "\$descmd1 ; ls .\/" > $Tmpfl &&
  if [ -f "$Tmpfl" -a -s "$Tmpfl" ] ; then 
   Tnlst=\`cat $Tmpfl | cut -d' ' -f1| sed -n -e 's/^.*\// /' -e '1,\$p' | tr '\\012' '\\040'\` &&
   (\rm $Tmpfl || : ) &&
  #if [ "\$CnFs" = 'yes' -o "\$CeTs" = 'yes' ] ; then
    mkdir -m 755 $target &&
  #else
  # mkdir $target
  #fi &&
   cd $target
  else
   echo "" && echo "  be2lnx: $tarpath/$target appears to be empty ! " &&
   exit 1
  fi &&
  descmd2="cd $tarpath" &&
  descmd3="rmdir $target" &&
# descmd4="mget \$Tnlst" &&
  for prmtr in \$Tnlst
  do
   # rcp $mdest:$tarpath/\$prmtr \$prmtr  
    if [ "\$CnFs" = 'yes' ] ; then
    #ssh datasvr "cd \$Cwd ; srcp -p -b 4M $mdest:$tarpath/\$prmtr \$prmtr ; chmod u+w \$prmtr"
     ssh datasvr "cd \$Cwd ; scp -p -o NoneSwitch=yes $mdest:$tarpath/\$prmtr \$prmtr ; chmod u+w \$prmtr"
    elif [ "\$CeTs" = 'yes' ] ; then
    #ssh cetus "cd \$Cwd ; srcp -p -b 4M $mdest:$tarpath/\$prmtr \$prmtr ; chmod u+w \$prmtr"
     ssh cetus "cd \$Cwd ; scp -p -o NoneSwitch=yes $mdest:$tarpath/\$prmtr \$prmtr ; chmod u+w \$prmtr"
    else
     scp -p -o NoneSwitch=yes $mdest:$tarpath/\$prmtr \$prmtr  ; chmod u+w \$prmtr
    #srcp -p -b 4M $mdest:$tarpath/\$prmtr \$prmtr  ; chmod u+w \$prmtr
    fi
  done  &&
  if [ "\$CnFs" = 'yes' -o "\$CeTs" = 'yes' ] ; then
   (\rm -f \${Cache_sync_file} || : ) && echo 'Sync' > \${Cache_sync_file} && (\rm -f \${Cache_sync_file} || : ) 
  fi &&
  descmd5=":" &&
  descmd6=":" &&
  Ttarget=$target &&
  TTnlst=\$Tnlst 
 else
  descmd1="cd $tarpath" &&
  descmd2=":" &&
  descmd3=":" &&
# descmd4="get $target $Tflnm" &&
  # rcp $mdest:$tarpath/$target $Tflnm &&
  (\rm -f $Tflnm || : ) &&
  if [ "\$CnFs" = 'yes' ] ; then
  #ssh datasvr "cd \$Cwd ; srcp -p -b 4M $mdest:$tarpath/$target $Tflnm ; chmod u+w $Tflnm"
   ssh datasvr "cd \$Cwd ; scp -p -o NoneSwitch=yes $mdest:$tarpath/$target $Tflnm ; chmod u+w $Tflnm"
  elif [ "\$CeTs" = 'yes' ] ; then
  #ssh cetus "cd \$Cwd ; srcp -p -b 4M $mdest:$tarpath/$target $Tflnm ; chmod u+w $Tflnm"
   ssh cetus "cd \$Cwd ; scp -p -o NoneSwitch=yes $mdest:$tarpath/$target $Tflnm ; chmod u+w $Tflnm"
  else
   scp -p -o NoneSwitch=yes $mdest:$tarpath/$target $Tflnm ; chmod u+w $Tflnm
  #srcp -p -b 4M $mdest:$tarpath/$target $Tflnm ; chmod u+w $Tflnm
  fi &&
  if [ "\$CnFs" = 'yes' -o "\$CeTs" = 'yes' ] ; then
   (\rm -f \${Cache_sync_file} || : ) && echo 'Sync' > \${Cache_sync_file} && (\rm -f \${Cache_sync_file} || : ) 
  fi &&
  descmd5="save $Tflnm $flnm" &&
#  descmd6="rm -r -f $Tflnm ; release $Tflnm" &&
  descmd6="release $Tflnm" &&
  Ttarget=$Tflnm &&
  Tnlst=$Tflnm &&
  TTnlst=$target
 fi &&
 set -x &&
 # (/usr/ucb/ftp -i -v $mdest << wooftp )  &&
#   (ftp -i -v $mdest << wooftp )  &&
#  \$descmd1 
#  binary
#  \$descmd4 
#  quit 
# wooftp
 set +x &&
 if [ "\$CnFs" = 'yes' -o "\$CeTs" = 'yes' ] ; then
  (\rm -f \${Cache_sync_file} || : ) && echo 'Sync' > \${Cache_sync_file} && (\rm -f \${Cache_sync_file} || : ) 
 fi &&
 for prmtr in \$Tnlst
  do
  if [ -f "\$prmtr" -a -s "\$prmtr" ] ; then 
    : 
  else
    exit 1
  fi
  done &&
  set -x &&
  chmod $vmode \$Tnlst &&
  chmod $vmode $despath/\$Ttarget &&
  \$descmd5 &&
  echo "" && echo "" && echo "  be2lnx: $despath/$target :" && echo " " &&
  ls -al $despath/\$Ttarget &&
  \$descmd6 &&
  if [ "$rmv" = "yes" ] ; then
 # (/usr/ucb/ftp -i -v $mdest << wooftp )  
##   (ftp -i -v $mdest << wooftp )  
##   \$descmd1 
##   mdel \$TTnlst 
##   \$descmd2 
##   \$descmd3 
##   quit 
## wooftp
  ssh $mdest "\$descmd1 ; rm -r -f \$TTnlst ; \$descmd2 ; \$descmd3"
  fi && ( rmllfls || : ) && exit || ( rmllfls || : ) && exit 2
fi
woofsub
echo "" && echo "  be2lnx: An nqs job is submitted, output will have $Pid suffix" && exit || exit 3
