#!/bin/sh
set -u  # force better programming and ability to use check for not set
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

# bin/$RCSfile$  $Revision$

ptime() {
python -c '
import time
print time.strftime("%c",time.localtime('$1'))
'
}

if [ -z "${1-}" ] ; then
  # write the output to a sub-directory in the inquisitor's web directory
  output=`conf.sh -k inquisitor | awk '($1 ~ /html_file/) {print $2}' | sed -e "s/'//g" -e "s/,//g"`tape_inventory
  if [ "$output" = "" ]; then exit 2; fi
  if [ ! -d $output ] ; then mkdir -p $output || exit 3; fi
  out=$output/VOLUMES_DEFINED
  if [ -e $out ]; then rm  $out;fi
  echo Date this volume listing was generated: `date` > $out
  # get all the volumes that have been defined
  #vols=`enstore volume --vols | cut -f1 -d\ | grep -v label`
  enstore volume --vols >> $out
  vols=`cat $out | cut -f1 -d\ | egrep -v 'label|Date|\.deleted'`
  # report volume quotas
  out=$output/VOLUME_QUOTAS
  echo Date this listing was generated: `date` > $out
  $ENSTORE_DIR/src/report_volume_quotas.py >> $out
else
  vols="$1"
  output=""
fi

number=`echo "$vols" | wc -l`


# now loop over them and save the output
if [ "$output" != "" ]; then
  total_bytes=/tmp/total_bytes
  rm -f $total_bytes
  tape_total=$output/TOTAL_BYTES_ON_TAPE
  accessed=/tmp/accessed
  rm -f $accessed
  last_accessed=$output/LAST_ACCESS
fi
count=0
for vol in $vols; do
  count=`expr $count + 1`
  echo "$vol  volume $count of $number"
  finfo=`enstore file --list=$vol`
  gbused=`echo "$finfo" | awk '/'"$vol"'/ {b=b+$3} END {printf "%.3g",b/1024/1024/1024}'`
  if [ `echo $finfo | grep -c NULL` -eq 0 ]; then gbtape=$gbused; else gbtape=0; fi
  vinfo=`enstore volume --vol=$vol`
  la=`echo "$vinfo" | awk '($1 ~ /last_access/) {print $2}'`
  lat="`ptime $la`"
  b=`echo "$vinfo"  | awk '($1 ~ /remaining_bytes/ ) {print $2}' | sed -e 's/L,//g' |sed -e 's/,//g'`
  gb=`echo "$b / 1073741824" | bc`
  si=`echo "$vinfo"  | awk '($1 ~ /system_inhibit/ ) {print $2,$3}' | sed -e "s/'//g" -e "s/,//g"`
  ui=`echo "$vinfo"  | awk '($1 ~ /user_inhibit/ )   {print $2,$3}' | sed -e "s/'//g" -e "s/,//g"`
  if [ "$output" != "" ]; then
    out=$output/$vol
    if [ -e $out ]; then rm  $out;fi
    echo "Volume:            $vol"      >>$out
    echo "Last accessed on:  $lat"      >>$out; echo $la $lat $vol >>$accessed
    echo "GBytes free:       $gb"       >>$out
    echo "GBytes written:    $gbused"   >>$out; echo $gbtape >>$total_bytes
    echo "Inhibits:          $si + $ui" >>$out
    echo "Date of this info: `date`"    >>$out
    echo                                >>$out
    echo "$finfo"                       >>$out
    echo                                >>$out
    echo "$vinfo"                       >>$out
  else
    echo "Volume:            $vol"
    echo "Last accessed on:  $lat"
    echo "GBytes free:       $gb"
    echo "GBytes written:    $gbused"
    echo "Inhibits:          $si + $ui"
    echo "Date of this info  `date`"
    echo
    echo "$finfo"
    echo
    echo "$vinfo"
  fi
done

# clean up the output area and make general info files
if [ "$output" != "" ]; then
  tape_bytes=`awk '{t=t+$1} END {print t}' $total_bytes`
  rm -f $tape_total
  echo "$tape_bytes GB" >$tape_total
  rm -f $last_accessed
  sort $accessed > $last_accessed
  here=`pwd`
  cd $output
  find . -daystart -mtime +2 -exec rm -f {} \;
  cd $here
fi
