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

# $Id$

F1=/diska/tape-inventory/AML2-VOLUMES.html
F2=/diska/tape-inventory/VOLUMES_DEFINED

F=/tmp/lostVols
rm -f $F
echo `date` Report of \"Incorrect\" Volumes >$F
echo >>$F


# look for ejected volumes, but still active in enstore
t=0
grep "Ejected" $F1 | while read volser vol rest; do
  if [ `grep -c $vol $F2` -ne 0 ]; then 
    V=`grep $vol $F2`
    if [ `echo "$V" | grep -c shelf` -eq 0 ]; then
      t=`expr $t + 1`
      echo $t $vol $rest from AML2 but found in enstore: $V  MOVE to shelf library.>>$F
   fi
  fi
done
echo >>$F


# look for ejected volumes and not in enstore at all
grep "Ejected" $F1 | while read volser vol rest; do
  if [ `grep -c $vol $F2` -eq 0 ]; then 
      t=`expr $t + 1`
     echo $t $vol $rest from AML2 and not found in enstore: CLEANUP up AML2 database.>>$F
   fi
done
echo>>$F

# look for volumes in aml2 and not in enstore at all 
grep "media" $F1 |grep -v "Ejected" | while read volser vol rest; do 
  if [ `grep -c $vol $F2` -eq 0 ]; then  
      t=`expr $t + 1` 
     echo $t $vol $rest in AML2 and not found in enstore: DECLARE to Enstore>>$F
   fi 
done 
echo >>$F

/usr/bin/Mail -s "AML/2 tapes that need re-organizing" enstore-admin@fnal.gov <$F
