 
#    Jun 12/18 - F.Majaess (Revised for Science)
#    Jan 07/09 - F.Majaess (Revised for af/alef)
#    May 14/08 - F.Majaess (Revised for sa/saiph,ib/dorval-ib)
#    Oct 16/06 - F.Majaess (Revised for ma/maia)
#    Mar 01/06 - F.Majaess (Revised for rg/rigel)
#    Jun 04/04 - F.Majaess (Revised to support "cmcarc" mode)
#    Sep 03/03 - F.Majaess (Revise for "short term" dataclass use)
#    Feb 25/02 - F.Majaess (Revised for ya/yata)
#    Jan 05/00 - F.Majaess (Added kz/kaze)
#    Jan 21/99 - F.Majaess (Added yo/yonaka)
#    Jun 02/98 - F.Majaess (Revised to allow searching user directory only)
#    Aug 13/97 - F.Majaess (Revised for sx/hiru)
#    Mar 18/97 - F.Majaess (Revised to allow targeting only
#                           the master data directory)
#    Mar 25/96 - F.Majaess (Revise for sx4)
#    Oct 13/95 - F.Majaess (Add "noqsub" to "plx2or" call)
#    Aug 09/95 - F.Majaess (Add time delay before submitting next job)
#    Jun 22/95 - F.Majaess (Revise for orion)
#    APR 11/95 - F.Majaess (Add 'rmtdespath' option)
#    JAN 31/95 - F.Majaess (Add 'cfs' option)
#    DEC 05/94 - F.Majaess (Set vmode=640)
#    Nov 14/94 - F.Majaess (Revise for sx3r)
#    JUN 07/94 - F.Majaess (Get rid of "..._Link" file(s) via "release")
#    APR 27/94 - F.Majaess (revised for sending data to Victoria)
#    JAN 19/93 - F.Majaess (revised to stripp off padded data)
#    OCT 27/92 - F.Majaess
 
#id taprlse - Used to release reserved tape. 
 
#   AUTHOR  - F.Majaess
 
#hd PURPOSE - "taprlse" script is used to release a tape reserved
#hd           by "taprsrv" , and possibly transfer loaded data. 
#hd           NOTE: a valid tape number must be defined in "tape"
#hd                 environment variable as well as the mode for
#hd                 reserving the tape 'read/write' in "mode". 
#hd                 If it is a tape load, "mscan" should have 
#hd                 machine destination for the data.
 
#pr  PARAMETERS:
#pr
#pr      tape   = tape number (=000000)
#pr      mode   = mode for tape reservation (=read)
#pr      mscan  = destination for loaded data 
#pr               (=maia/saiph/dorval-ib/alef/pollux/both/svvic/vic/svcrd/crd)
#pr               maia    - data transferred to MAIA  and then deleted
#pr                         from pollux file system (default based
#pr                         on $BERUNPATH).
#pr               saiph   - data transferred to SAIPH and then deleted
#pr                         from pollux file system (default based
#pr                         on $BERUNPATH).
#pr             dorval-ib - data kept only on local filesystem.
#pr                  alef - data kept only on local filesystem.
#pr               pollux  - data kept only on local filesystem.
#pr               both    - data transferred to back-end (based on 
#pr                         BERUNPATH setting) and kept on server
#pr                         file system.
#pr               svvic   - data transferred to Victoria and kept on 
#pr                         server file system.
#pr               vic     - data transferred to Victoria and then 
#pr                         deleted from server file system.
#pr               svcrd   - data transferred to Orion and kept on 
#pr                         server file system.
#pr               crd     - data transferred to Orion and then 
#pr                         deleted from server file system.
#pr
 
#ex  EXAMPLE: 
#ex  
#ex taprlse mscan=both 

set -x
# set -v
if test "$abort" != "abort" ; then
set +x
#  diagnostic codes...

#  echo $@  
 
#  *  check if switches/parameters specified on script call
 
if test $# -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 ;;
 
#   * anything else case skip...
 
    *  )  tprmtr="$tprmtr $prmtr";;
 
    esac
  done
  prmtrl=$tprmtr
  tprmtr=
 
fi
prmtrl=${prmtrl=}

#   * Ignore unused parameters... 
 
if test -n "$prmtrl"  ; then
  
  echo "" ; echo "" ; echo " taprlse: ignored $prmtrl !" ; echo ""

fi
 
# ensure all used parameters without a value are set to the appropriate
# default value...

tape=${tape='000000'}
mode=${mode='read'}
cfs=${cfs='no'}
if [ "$besc" = 'on' -o "$mscan" = 'besc' ] ; then
 
   HOSTIDf=`echo $HOSTID | sed -e 's/eccc.*-ppp/cs/g' -e 's/^ppp/cs/g' | cut -c 1-3`
   case $HOSTIDf in
    xc1) bemchn='hare' ;;
    xc2) bemchn='brooks' ;;
    xc3) bemchn='banting' ;;
    xc4) bemchn='daley' ;;
      *) echo " taprlse: unsupported platform `hostname` !" && echo "" && abort='abort' && ( exit 2) ;;
   esac
   mscan='besc'
else
  if [ "$mscan" = 'cidsc03' ] ; then
    unset mscan
  fi
  
  Hall=`echo "$SITESTORE_ROOT" | sed -n -e 's/^\/space\/hall/hall/p' | sed -n -e 's/\/sitestore\/.*$//p'`
  if [ "$Hall" = 'hall1' ] ; then
   bemchn='hare'
  elif [ "$Hall" = 'hall2' ] ; then
   bemchn='brooks'
  elif [ "$Hall" = 'hall3' ] ; then
   bemchn='banting'
  elif [ "$Hall" = 'hall4' ] ; then
   bemchn='daley'
  else
   bemchn=${bemchn:-'cidsc03'}
  fi
  
  if [ "$bemchn" = 'hare' ] ; then
   ssh hare hostname >> /dev/null && : || echo 'Hare seems unaccessible'
  elif [ "$bemchn" = 'brooks' ] ; then
   ssh brooks hostname >> /dev/null && : || echo 'Brooks seems unaccessible'
  elif [ "$bemchn" = 'banting' ] ; then
   ssh banting hostname >> /dev/null && : || echo 'Brooks seems unaccessible'
  elif [ "$bemchn" = 'daley' ] ; then
   ssh daley hostname >> /dev/null && : || echo 'Brooks seems unaccessible'
  fi
  mscan=${mscan=${bemchn}}
  mscan=`echo $mscan | tr '[A-Z]' '[a-z]'` ;
  if [ "$mscan" = 'pp1' ] ; then
   mscan='eccc-ppp1'
  elif [ "$mscan" = 'pp2' ] ; then
   mscan='eccc-ppp2'
  elif [ "$mscan" = 'pp3' ] ; then
   mscan='eccc-ppp3'
  elif [ "$mscan" = 'pp4' ] ; then
   mscan='eccc-ppp4'
  fi
  case $mscan in
     hare|brooks|banting|daley|eccc-ppp1|eccc-ppp2|eccc-ppp3|eccc-ppp4) : ;;
     *        ) echo "" && echo "" && echo " taprlse: illegal destination $mscan setting!" && echo "" && abort='abort' && ( exit 2) ;;
  esac
fi
case $bemchn in
  hare) Mdest='hr';;
  brooks) Mdest='br';;
  daley) Mdest='dy';;
  banting) Mdest='ba';;
   * ) Mdest='ba';;
esac
#
vmode=${vmode='644'}
 
tapexit 0 &&
sdiagdir  &&
if test  "$tape" = "000000" -a "$cfs" != 'yes'  ; then
 echo "" && echo "" && echo " taprlse: a valid tape number must be specified ? " && echo "" &&
 abort='abort' &&
 (exit 1)
else
 if test  "$mode" = 'read'  ; then
  if test "$cfs" = 'yes' -o "$hpnls" = 'yes' ; then
   echo "" && echo "" && echo " taprlse: attempt retrieving the files from the archive server " && echo "" &&
   Taperr_msg="problem in retrieving the files from the archive server" &&
   cd $Tmppath/$Tmpdir &&
   Tnlst=`echo *` &&
   if [ "$hpcarc_mode" = 'yes' -o "$hpcarc_mode" = 'on' ] ; then
    if [ "$shortermdir" = 'on' ] ; then
     Tshorterm='shorterm=yes'
    elif [ "$shortermdir" = 'off' ] ; then
      Tshorterm='shorterm=no'
    else
      unset Tshorterm
    fi &&
    if [ -n "$Tarcoptns" ] ; then
     unset Tarcoptns
    fi &&
    if [ -n "$arclabel" ] ; then
     Tarcoptns="arclabel='""$arclabel""'"
    fi &&
    if [ -n "$hpnls_user" ] ; then
     Tarcoptns="$Tarcoptns hpnls_user='""$hpnls_user""'"
    fi &&
    if [ -n "$hpcarc_project" ] ; then
     Tarcoptns="$Tarcoptns hpcarc_project='""$hpcarc_project""'"
    fi &&
    if [ -n "$hpcarc_indvdl" ] ; then
     Tarcoptns="$Tarcoptns hpcarc_indvdl='""$hpcarc_indvdl""'"
    fi &&
    if [ -n "$hpcarc_nobundle" ] ; then
     Tarcoptns="$Tarcoptns hpcarc_nobundle='""$hpcarc_nobundle""'"
    fi &&
    if [ -n "$hpcarc_name" ] ; then
     Tarcoptns="$Tarcoptns hpcarc_name=$hpcarc_name" &&
     if [ "$Tnlst" = 'all' ] ; then
      Reset_Tnlst='yes' &&
      (\rm -f all || : ) 
     fi
    fi &&
    retriev $Tshorterm $Tarcoptns $Tnlst 
   else
    if [ "$masterdir" = 'on' ] ; then
     Toffcl='offcl=yes'
    elif [ "$masterdir" = 'off' ] ; then
      Toffcl='offcl=no own=yes'
    else
      unset Toffcl
    fi &&
    if [ "$shortermdir" = 'on' ] ; then
    #Tshorterm='shorterm=yes'
    ### Disable on HPNLS by setting to 'no'
      Tshorterm='shorterm=no'
    elif [ "$shortermdir" = 'off' ] ; then
      Tshorterm='shorterm=no'
    else
      unset Tshorterm
    fi &&
    ### Disable on HPNLS by hard-code to 'no'
    Tshorterm='shorterm=no' &&
 #  retriev $Toffcl $backup $Tnlst &&
    if [ "$backup" = 'on' -o "$backup" = 'yes' ] ; then
      Tbackup='backup'
    else
      unset Tbackup
    fi &&
    if [ -n "$Tarcoptns" ] ; then
     unset Tarcoptns
    fi &&
    if [ "$arcfile" = 'none' -o "$noarcmode" = 'yes' ] ; then
      if [ -n "$arclabel" ] ; then
       echo " taprlse: arclabel(=$arclabel) must not be set when arcfile=none !" &&
       exit 1
      fi &&
      Tarcoptns="noarcmode=yes"
    else
     if [ -n "$arclabel" ] ; then
      Tarcoptns="arclabel='""$arclabel""'"
     fi &&
     if [ -n "$arcfile" ] ; then
      Tarcoptns="$Tarcoptns noarcmode=no arcfile=$arcfile" &&
      if [ "$Tnlst" = 'all' ] ; then
       Reset_Tnlst='yes' &&
       (\rm -f all || : ) 
      fi
     else
      Tarcoptns="$Tarcoptns noarcmode=no"
     fi
    fi &&
    retriev $Toffcl $Tshorterm $Tarcoptns $Tbackup $Tnlst
   fi &&
   if [ "$Reset_Tnlst" = 'yes' ] ; then
    unset Reset_Tnlst &&
    Tnlst=`echo *` 
   fi &&
   chmod u+w $Tnlst &&
   unset Taperr_msg
  fi && 
  echo "" && echo "" && echo " taprlse: $Tmppath/$Tmpdir subdirectory content: " && echo "" &&
  ls -al $Tmppath/$Tmpdir | sed -n -e 's/^/ /p'  &&
  cd $Tmppath/$Tmpdir &&
  if test \( "$OS" = 'Linux' -a \( \( "$mscan" = 'eccc-ppp1' -o "$mscan" = 'eccc-ppp2' -o "$mscan" = 'eccc-ppp3' -o "$mscan" = 'eccc-ppp4' \) -o "$mscan" = "svvic" -o "$mscan" = "both" -o "$mscan" = "svcrd" \) \) -o \( "$OS" = 'Linux' -a "$data_transfer" = 'yes' \)  ; then
    echo "" && echo "" && echo " taprlse: attempting official save of $Tmppath/$Tmpdir subdirectory files  " && echo "" &&
    cd $Tmppath/$Tmpdir &&
    Tnlst=`echo *` &&
    if [ "$SITE_ID" = 'DrvlSC' ] ; then
     dpalist=${dpalist:='on'} &&
     export dpalist
    fi &&
    for prmtr in $Tnlst
     do
      lprmtr=`echo $prmtr | tr '[A-Z]' '[a-z]' ` &&
      save $prmtr $lprmtr &&
      if test "$mscan" = 'eccc-ppp1' -o "$mscan" = 'eccc-ppp2' -o "$mscan" = 'eccc-ppp3' -o "$mscan" = 'eccc-ppp4' -o \( "$OS" = 'Linux' -a "$data_transfer" = 'yes' \) ; then
       release $prmtr
      fi &&
      echo "" && echo " taprlse: officially saved $lprmtr " && echo ""
     done  &&
     if test "$data_transfer" = 'yes' -a "$delet_files" = 'yes' -a -s "./.obtnfl_be_list" ; then
      ( \rm -f delet_set_job || : ) &&
      touch delet_set_job &&
      (cat << woof >> delet_set_job ) &&
#!/bin/sh
set -a

. betapath2  # sets path for new package (required until old package is retired)

#  * ..................... Definition of Global Parameters .....................

 username="$USER"; user="$USER"; CUSTOM_ENV_FILE="$CUSTOM_ENV_FILE" ;

#  * ........................... Parmsub Parameters ............................

 lopgm="lopgm"; 

#  * ............................ Condef Parameters ............................

 nextjob=off
 noprint=${noprint:-'on'}

#  * ............................. Deck Definition .............................

#deck delet_set
jobname=delet_set ; time=900 ; memory=500mb
. comjcl.cdk

cat > Execute_Script <<'end_of_script'
if [ "\$SITE_ID" = 'DrvlSC' ] ; then dpalist=\${dpalist:='on'} ; export dpalist ; fi
woof
      (cat ./.obtnfl_be_list | egrep -v '^ mdest=' | egrep -v '^ delet ' >> delet_set_job) &&
      (cat << 'woof' >> delet_set_job) &&
end_of_script
cat > Input_Cards <<end_of_data

+   .    :    .    :    .    :    .    :    .    :    .    :    .    :    .    :
0DELETE FILES.
0DELET_SET --------------------------------------------------------- DELET_SET

end_of_data

. endjcl.cdk

#end_of_job
woof
      DestNtn=`cat ./.obtnfl_be_list | egrep '^ mdest=' | head -1` &&
      if test -n "$DestNtn" ; then
       echo "" && echo " Deletion of transferred files is requested, submitting:" && echo "" &&
       echo " ============== " && echo "" &&
       cat delet_set_job &&
       echo "" && echo " ============== " &&
       echo "" && echo " with --> $DestNtn <-- " && echo "" &&
       ( set -x && rsub delet_set_job $DestNtn || : )
      fi &&
      (\rm -f ./.obtnfl_be_list1 || : ) &&
      touch .obtnfl_be_list1 &&
      ( cat ./.obtnfl_be_list | egrep '^ delet ' >> .obtnfl_be_list1 || : ) &&
      if test -s "./.obtnfl_be_list1" ; then
       echo "" && echo " Deletion of transferred files is requested, invoking:" && echo "" &&
       echo " ============== " && echo "" &&
       cat ./.obtnfl_be_list1 &&
       echo "" && echo " ============== " && echo "" &&
       ( . ./.obtnfl_be_list1 | egrep '^your job' || : )
      fi
     fi && 
     (\rm -f ./.obtnfl_be_list || : ) &&
     (\rm -f ./.obtnfl_be_list1 || : ) &&
     (\rm -f delet_set_job || : ) &&
     sdiagdir  
  fi &&
  unset rmtdespath &&
  if test -n "$RMTRUNPATH"  ; then
    rmtdespath="despath=$RMTRUNPATH"
  fi &&

  if test -n "$fil_grpname" ; then
   Tfil_grpname="fil_grpname=$fil_grpname"
  fi &&

  if [ "$OS" = 'Linux' -a "$data_transfer" = 'yes' ] ; then
    \rm -rf $Tmppath/$Tmpdir  && echo "" && echo "" && echo " taprlse: $Tmppath/$Tmpdir removed " && echo " " 
  else
   case $mscan in
    hare|brooks|banting|daley|both) TrnPrfx=lnx2be &&
                         echo "" && echo "" && echo " taprlse: $TrnPrfx $Mdest noqsub=yes nooffsv=no rmv=yes $Tfil_grpname tarpath=$Tmppath target=$Tmpdir " && echo "" \
                && $TrnPrfx $Mdest noqsub=yes nooffsv=no rmv=yes $Tfil_grpname tarpath=$Tmppath target=$Tmpdir \
                && : ;;
    svvic|vic) TrnPrfx=lnx2vic &&
    echo "" && echo "" && echo " taprlse: $TrnPrfx nooffsv=no rmv=yes $rmtdespath tarpath=$Tmppath time=$time target=$Tmpdir " && echo "" \
                && $TrnPrfx nooffsv=no rmv=yes $rmtdespath tarpath=$Tmppath time=$time target=$Tmpdir ;;
    eccc-ppp1|eccc-ppp2|eccc-ppp3|eccc-ppp4)  \rm -rf $Tmppath/$Tmpdir  && echo "" && echo "" && echo " taprlse: $Tmppath/$Tmpdir removed " && echo " " ;;
    *        ) echo "" && echo "" && echo " taprlse: illegal destination $mscan " && echo "" && abort='abort' && ( exit 2) ;;
   esac
  fi
 else
  if test "$newtape" = "yes" -o "$mode" = "copy" -o "$mode" = 'convert' ; then
    if test -d $Tmppath/$Tmpdir ; then 
     ( \rm -rf $Tmppath/$Tmpdir || : ) &&
     echo "" && echo "" && echo " taprlse: $Tmppath/$Tmpdir removed " && echo " " 
    fi
  fi
 fi
fi || abort='abort' 
fi
set -x
if test "$abort" = "abort" ; then
 (exit 4)
fi
