#!/bin/sh -u

if [ "${1:-}" = "-x" ]; then set -xv; shift; fi

script=`basename $0`

usage() { echo "Usage: login as enstore to d0ensrv4 or stkensrv4"
	  echo "       enter $script [remove|shelve]"
	}

check_work() { local work1 action1
	       while read work1 action1
	       do
		 if [ $work1 != $(basename $work) -a -d $(dirname $work)/$work1 ]; then
		    rem=`ls -vI \*.\* $(dirname $work)/$work1 | awk 'END {print NR}'`
		    if [ $rem -gt 1 ]; then
		       echo "`date` There are $rem groups to $action1."
		    elif [ $rem -eq 1 ]; then
		       echo "`date` There is one group to $action1."
		    fi
		 fi
	       done << EOD
adic_write_protect_work		lock
adic_write_permit_work		unlock
adic_shelf_volumes_work		shelve
adic_purge_volumes_work		remove
EOD

	       ns=`find $work -type f -name \*.done -daystart -mtime -1 -print | awk 'END {print NR}'`
	       nf=`find $work -type f -name \*.fail -daystart -mtime -1 -print | awk 'END {print NR}'`
	       msg="Today's $script work: $ns groups ${action%e}ed, $nf groups failed."
	       echo "`date` $msg"
	       if [ $ns -gt 0 -o $nf -gt 0 ]; then
		 enstore alarm --raise --severity "I" --root-error "$msg"
	       fi
	     }

[ "`whoami`" != enstore ] && usage && exit 1

node=`hostname -s`
case $node in
  stkensrv4)
     clerk=stkensrv0
     type=DECDLT
     ;;
  d0ensrv4)
     clerk=d0ensrv0
     type=3480
     ;;
  *) usage
     exit 1
     ;;
esac

action=${1:-"shelve"}
case $action in
  shelve)
     work=$HOME/isa-tools/adic_shelf_volumes_work
     ;;
  remove)
     work=$HOME/isa-tools/adic_purge_volumes_work
     . $ENSTORE_DIR/sbin/gettkt
     ;;
  *) usage
     exit 1
     ;;
esac

# look for work at home first, then under /
[ -d $work ] || work=/$(basename $work)

if [ ! -d $work ]; then
  echo "`date` ERROR. $work does not exist"
  exit 1
fi

cd $work

rem=`ls -vI \*.\* | awk 'END {print NR}'`
if expr $rem : 0 >/dev/null; then
   echo "`date` There are no groups to $action. Goodbye."
   check_work
   exit
else
   if [ $rem -gt 1 ]; then
      /bin/echo -n "There are $rem groups to $action. Proceed [y/n]? "
   else
      /bin/echo -n "There is one group to $action. Proceed [y/n]? "
   fi
   if read ans && expr "$ans" : '[Yy]' >/dev/null; then
      :
   else
      exit
   fi
fi

output=/tmp/${script}.${action}.output

for i in `ls -vI \*.\*`; do
  t0=`date +'%s'`
  obox=`awk 'NR == 1 {print $NF}' $i`
  echo "`date` Group $i" >> $output
  # extract the volumes about to be ejected
  set `awk '{gsub(",","\n",$(NF-1)); print $(NF-1)}' $i`
  case $action in
    shelve)
      echo "Now moving volumes to be ejected to shelf library..."
      for vol in $*; do
	if enstore info --check $vol; then
	  lib=`enstore info --vol $vol | awk -F\' '$2 == "library" {print $4}'`
	  if ! echo $lib | grep "^shelf-" >/dev/null; then
	    enstore vol --new-library=shelf-$lib $vol
	  fi
	fi
      done
      ;;
    remove)
      echo "Now deleting volumes to be ejected from enstore..."
      if ! klist -s; then
	echo "`date` ERROR. Check kerberos credentials."
	exit 1
      fi
      if ! rsh $clerk ". /local/ups/etc/setups.sh; setup enstore; for vol in $*; do enstore vol --delete \$vol; done" 2>/dev/null; then
	echo "`date` ERROR. Failed to delete volumes!"
	exit 1
      fi
      ;;
  esac

  echo
  . $i |tee -a $output 2>&1
  echo
  echo
  touch $i
  t1=`date +'%s'`
  delta=`expr $t1 - $t0`
  deltam=`expr $delta / 60`
  deltas=`expr $delta % 60`
  echo That cycle took $deltam minutes $deltas seconds.
  /bin/echo -n "Have $# tapes been ${action%e}ed from I/O box $obox [y/n]? "
  if read ans && expr "$ans" : '[Yy]' >/dev/null; then
     echo "... success acknowledged"
     mv $i ${i}.done
  else
     echo "... failure acknowledged"
     mv $i ${i}.fail
  fi
  echo
  echo
  rem=`ls -vI \*.\* | awk 'END {print NR}'`
  if expr $rem : 0 >/dev/null; then
     echo "`date` There are no more groups to $action. Thank you."
     break
  else
     if [ $rem -gt 1 ]; then
       /bin/echo -n "There are $rem groups remaining. Do another [y/n]? "
     else
       /bin/echo -n "There is only one group remaining! Do it [y/n]? "
     fi
     if read ans && expr "$ans" : '[Yy]' >/dev/null; then
	continue
     else
	break
     fi
  fi
done

check_work
