#! /bin/sh
 
#    SEP 03/92 - F.Majaess
 
#id  maddlst - Mainly used to update front_end tapelist.
 
#    AUTHOR  - F.Majaess
 
#hd  PURPOSE - "maddlst" script is used mainly to update front-end tapelist 
#hd            "listfil" in "lpath" subdirectory by appending "newlist" file 
#hd            from "npath" subdirectory to the bottom of "oldlist" file in
#hd            "opath" subdirectory.
#hd            NOTE: "opath"/"oldfil" can be the same as "lpath"/"listfil"
#hd                  and neither of these two  files have to exist.
 
#pr  PARAMETERS:
#pr
#pr    PRIMARY/SECONDARY
#pr
#pr      list   = switch to list "listfil" contents ('yes'- to list, 
#pr               'no' - not to list (default))
#pr
#pr    PRIMARY
#pr
#pr      oldlist= name of the file containing old list (=LISTAPE).
#pr      opath  = path to the subdirectory where "oldlist" is residing.
#pr               (=$HOME/info)
#pr      newlist= name of the file containing new list to be added.
#pr               (=LISTAPE)
#pr      npath  = path to the subdirectory where "newlist" is residing.
#pr               (=$HOME/.queue/.data)
#pr      listfil= name of the file to be updated(=LISTAPE).
#pr      lpath  = path to the subdirectory where "listfil" is_residing/
#pr               to_reside (=$HOME/info).
#pr
 
#ex  EXAMPLE: 
#ex
#ex maddlst oldlist=listapb newlist=listapb listfil=listapb 

IFS=' '
export IFS
 
#  diagnostic codes...

#  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...
    
  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...
 
    *  )  ;;
 
    esac
  done
 
fi

# ensure all used parameters without a value are set to the appropriate
# default value...
 
uns=${uns=$USER}
usr=`echo $uns |  sed 's/^....//'`
grp=`echo $uns |  sed 's/...$//'`
Thome="/users/tor/$grp/$usr"
oldlist=${oldlist='LISTAPE'}
opath=${opath=$Thome/info}
newlist=${newlist='LISTAPE'}
npath=${npath=$Thome/.queue/.data}
listfil=${listfil='LISTAPE'}
lpath=${lpath=$Thome/info}
list=${list='no'}
 
oldlist=`echo $oldlist | tr '[a-z]' '[A-Z]'`
newlist=`echo $newlist | tr '[a-z]' '[A-Z]'`
listfil=`echo $listfil | tr '[a-z]' '[A-Z]'`
Tlistfil="$listfil" 
 
oldlist="$opath""/""$oldlist"
listfil="$lpath""/""$listfil"

# * Abort if "npath" subdirectory does not exist...
 
if [ ! -d "$npath" ] ; then
  exit 2
fi
 
# * Create "lpath" subdirectory if it does not exist...
 
if [ ! -d "$lpath" ] ; then
  \mkdir -m 755 $lpath
 #\chmod 750 $lpath
fi
 
cd $npath

Tnlst=`echo $newlist.*`
 
if [ -n "$Tnlst" -a "$Tnlst" != "$newlist."'*' ] ; then
  prmtrl=
  for prmtr in $Tnlst
  do
    prmtrl="$prmtr $prmtrl"
  done
  Tnlst="$prmtrl"
  prmtrl=
  if [ ! -f "$oldlist" ] ; then
    oldlist=" "
  fi
  if [ "$oldlist" = "$listfil" ] ; then
    (eval "\cat $Tnlst 1>> $listfil 2>/dev/null "     &&
     eval "\rm $Tnlst"                                      ||
     exit 3
    ) || exit 3
  else
    (eval "\cat $oldlist $Tnlst 1>> $listfil 2>/dev/null "   &&
     eval "\rm $Tnlst"                                      ||
     exit 4
    ) || exit 4
  fi
fi

if [ -f "$listfil" -a "$list" = "yes" ] ; then
  \cat $listfil
fi
exit
