 
#    Jun 12/18 - F.Majaess (Revised for use on Science)
#    Jun 04/04 - F.Majaess (Revised for "cmcarc" mode)
#    SEP 03/03 - F.Majaess (Revise for "short term" dataclass use)
#    MAR 06/97 - F.Majaess (Revised to allow normal users to save
#                           data on the master data directory)
#    JAN 31/95 - F.Majaess ( Add 'cfs' option)
#    JAN 24/94 - F.Majaess ( change "RUNPATH" to "CCRNTMP")
#    FEB 10/93 - F.Majaess (remove PASSE occurences)
#    FEB 01/93 - F.Majaess (Implement approximate tape space usage reporting)
#    NOV 09/92 - F.Majaess
 
#id writape - Used to manage archival of files.
 
#   AUTHOR  - F.Majaess
 
#hd PURPOSE - "writape" script is used to manage archival of files 
#hd           contained in "dpath" subdirectory.
 
#pr  PARAMETERS:
#pr
#pr    PRIMARY
#pr
#pr      nolist = switch controlling updating tape list(s) (=no)
#pr      dpath  = full path to the subdirectory containing the files
#pr                 to be dumped (=$Tmppath/$Tmpdir)
#pr      listape= original tapelist filename (=OCARLST).
#pr      lpath  = path to the subdirectory where tapelist files are
#pr               kept (=$HOME/info).
#pr      npath  = path to the subdirectory where tapelist update file 
#pr               is kept (=$HOME/.queue/.data)
#pr      uns    = user's account code (=$USER)
#pr
 
#ex  EXAMPLE: 
#ex  
#ex writape dpath=$CCRNTMP/subdir tape=.origtap 

# set -x
# set -v
sdfltifs
if [ "$abort" = 'abort' ] ; then
  tapexit 9
else
# set -x
  set +x
#  echo $@  
 
#  *  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...
    
  if [ -n "$prmtrl" ] ; then
  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
  fi
  prmtrl=$tprmtr
  tprmtr=
 
fi
prmtrl=${prmtrl=''}


#   * Ignore unused parameters... 
 
if [ -n "$prmtrl" ] ; then
  
  echo "" ; echo "" ; echo " writape: ignored: $prmtrl " ; echo ""

fi
 
# ensure all used parameters without a value are set to the appropriate
# default value...
 
# cfs=${cfs='no'}
# hpnls=${hpnls=$cfs}
uns=`whoami`
Thome="/home/`whoami`"
tape='000000'
dpath=$Tmppath/$Tmpdir
lpath=${lpath=$Thome/info}
npath=${npath=$Thome/.queue/.data}
listape=${listape='HPNLSDATA'}
# vmode=${vmode='550'}
vvmode='550'
 
listape=`echo $listape | tr '[a-z]' '[A-Z]'`

if [ "$invoker_deck" != 'othr2fe' -a "$invoker_deck" != 'fe2othr' ] ; then 
 if [ -n "$nolist" ] ; then
  if [ "$nolist" = 'on' ] ; then nolist='yes' ; fi
  if [ "$nolist" != 'yes' -a "$nolist" != 'no' ] ; then nolist='no' ; fi
 else
  nolist='no'
 fi
fi

# * Abort if "dpath" subdirectory does not exist...
 
if [ ! -d "$dpath" ] ; then
  echo "" && echo "" && echo " writape: $dpath does not exist !" && echo "" &&
  (exit 2)
fi &&

# * If updating tape list(s)...
# 
if [ "$nolist" != "yes" ] ; then
# 
# * Abort if "npath" subdirectory does not exist...
 
 if [ ! -d "$npath" ] ; then
  echo "" && echo "" && echo " writape: $npath does not exist !" && echo "" &&
  (exit 3)
 fi &&
# 
# * Create "lpath" subdirectory if it does not exist...
# 
 if [ ! -d "$lpath" ] ; then
  \mkdir -m 755 $lpath 
 #\mkdir $lpath &&
 #\chmod 750 $lpath
 fi 
fi &&
# 
cd $dpath &&
echo "" && echo "" && echo " writape: $dpath subdirectory contents..." && echo "" &&
ls -al $dpath | sed -n -e 's/^/ /p' &&

Tdate=`date '+(%y%j%H%M%S)'` &&
Tnlst= &&
Tnlst=`echo *` &&
sdiagdir &&
if [ "$Tnlst" = '*' ] ; then
 echo "" && echo "" && echo " writape: empty subdirectory $dpath " && echo "" &&
 (exit 5 ) 
fi &&

#!!!!
# * Dump files into the file server 
unset Taperr_msg &&

# * If updating tape list(s)...
 
if [ "$nolist" != "yes" ] ; then
# ((eval "\rm $dpath/.WRITAPE.$listape.tmp.$Tpid.$Tpid  2>/dev/null" ) || : )
#(eval "\rm $dpath/.WRITAPE.$listape.tmp.$Tpid.$Tpid  " || : ) &&
#(eval "\rm $dpath/.WRITAPE.$listape.$Tpid.$Tpid " || : )
 : 
fi &&
 
# * Archive files into the archive server ...
 
echo "" && echo "" && echo " writape: attempt archiving files on the archive server..." && echo "" &&
cd $dpath &&
Taperr_msg="problem in archiving files on the archive server" &&
# if [ "$masterdir" = 'on' ] ; then
#   Toffcl='offcl=yes'
# elif [ "$masterdir" = 'off' ] ; then
#   Toffcl='offcl=no'
# else
#   unset Toffcl
# fi &&
if [ "$shortermdir" = 'on' ] ; then
  Tshorterm='shorterm=yes'
elif [ "$shortermdir" = 'off' ] ; then
  Tshorterm='shorterm=no'
else
  unset Tshorterm
fi &&
Tarcoptns=' ' &&
if [ -n "$arclabel" ] ; then
 Tarcoptns="arclabel=$arclabel"
fi &&
# if [ "$arc_large_size_permit" = 'on' ] ; then
#  Tarcoptns="$Tarcoptns arc_large_size_permit=on"
# fi &&
if [ -n "$hpcarc_project" ] ; then
 Tarcoptns="$Tarcoptns hpcarc_project=$hpcarc_project"
fi &&
if [ -n "$nochksum" ] ; then
 Tarcoptns="$Tarcoptns nochksum=$nochksum"
fi &&

# archive $rep $Toffcl $Tshorterm $Tarcoptns $Tnlst &&
archive $Tshorterm $Tarcoptns $Tnlst &&
unset Taperr_msg &&
echo "" && echo " writape: files archived on the archive server" &&
TmStmp=`date '+%a'` &&
  set -x &&
prmtrl= &&
if [ "$nolist" != "yes" -a -s "./.archive.list" ] ; then
 Taperr_msg=" problem in updating $lpath/$listape " &&
 if [ -d "$lpath/." -a -n "$listape" ] ; then
  touch $lpath/$listape &&
  if [ ! -s $lpath/$listape ] ; then
   chmod go+r $lpath/$listape 
  fi &&
  cat ./.archive.list >> $lpath/$listape &&
  echo "" && echo " writape: $lpath/$listape was updated" && echo ""
 else
  touch $npath/.WRITAPE.$listape.$Tpid.$Tpid &&
  cat ./.archive.list >> $npath/.WRITAPE.$listape.$Tpid.$Tpid &&
  echo "" && echo " writape: Problem updating $lpath/$listape. Update info preserved in $npath/.WRITAPE.$listape.$Tpid.$Tpid" && echo ""
 fi &&
 unset Taperr_msg
fi &&
(\rm -f ./.archive.list || : ) &&
sdiagdir &&
 
#!!!!

tapexit 0 &&
sdiagdir &&
Taperr_msg=" problem in removing $dpath subdirectory "  && 
\rm -rf $dpath  &&
echo "" && echo "" && echo " writape: $dpath subdirectory removed " && echo "" &&
unset Taperr_msg && 
set -x || 
tapexit 5
fi
