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

if [ "${1:-}" = "-C" ] ; then copyit=1; shift; else copyit=0; fi

# script to return a random file name(s) for tapes in the current inventory

node=`uname -n| sed -e 's/\([^\.]\)\..*/\1/'`
gang=`gang`

HTTP=http://${gang}srv2.fnal.gov
COUNT=${1:-1}
FORGET=${2:-CLN}
MUSTHAVE=${3:-""}
here=`pwd`

# first go get the volumes that are defined in the inventory
echo `date` wget $HTTP/enstore/tape_inventory/VOLUMES_DEFINED 1>&2
wget $HTTP/enstore/tape_inventory/VOLUMES_DEFINED 2>/dev/null
rc=$?; if [ $rc -ne 0 ]; then echo "wget VOLUMES_DEFINED FAILED"l exit $rc;else echo `date` got it 1>&2 ; fi
cat VOLUMES_DEFINED | egrep -iv "$FORGET|CLN|null|^Date|^label|DELETED|NOTALLOWED|NOACCESS|\.none\$" | egrep "$MUSTHAVE" >V_D
rm -f VOLUMES_DEFINED
N=`wc V_D| awk '{print $1}'`
if [ $N -eq 0 ]; then
   echo "NO files found after excluding \"$FORGET\"  and then searching for \"$MUSTHAVE\""
   exit 1
fi

done=$COUNT
loops=0
RC=0

while [ $done -gt 0 ]; do

  cd $here
  loops=`expr $loops + 1`
  if [ $loops -ge 1000 ]; then echo "TOO MANY LOOPS = $loops, EXIT"; rm -f V_D;exit 1; fi

  # choose a random volume from the list
  ran=`python -c "import whrandom;print whrandom.randint(1,"$N")"`
  tape=`head -n $ran V_D|tail -n 1| awk '{print $1}'`
  echo `date` enstore vol --vol $tape 2>&1
  tinfo=`enstore vol --vol $tape`
  echo "$tinfo" 2>&1
  if [ `echo $tinfo | egrep -c 'DELETED|NOTALLOWED|NOACCESS'` -ne 0 ]; then
    echo REJECTED 2>&1
    continue
  fi
  
  echo `date` wget $HTTP/enstore/tape_inventory/$tape  2>/dev/null 1>&2
  wget $HTTP/enstore/tape_inventory/$tape  2>/dev/null
  rc=$?; if [ $rc -ne 0 ]; then echo "wget $tape FAILED "l exit $rc;else echo `date` got it 1>&2 ; fi
  if [ `egrep -c 'NOACCESS|NOTALLOWED' $tape` -ne 0 ]; then echo `date` REJECTED NOACCESS/NOTALLOED 1>&2; rm -f $tape; continue; fi
  grep /pnfs $tape | egrep -v 'deleted|  yes /' > ${tape}_
  rm -f $tape
  NF=`wc ${tape}_| awk '{print $1}'`
  if [ $NF -le 0 ]; then echo `date` REJECTED NO FILES 1>&2; rm  ${tape}_; continue; fi

  # choose a random file on the random volume
  ran=`python -c "import whrandom;print whrandom.randint(1,"$NF")"`
  file=`head -n $ran ${tape}_|tail -n 1| awk '{print $NF,$1,$3,$4,$2}'`
  F=`echo $file | awk '{print $1}'`
  R=`echo $file | awk '{print $2,$3,$4,$5}'`

  # make sure the random file has hpss as part of its name
  if [ `echo $F | grep -ci "hpss"` = 0 ]; then continue; fi

  # user could have renamed it - find current name using pnfsid
  if [ ! -r $F ]; then
    bfid=`echo $file | awk '{print $NF}'`
    info=`enstore file --bfid $bfid 2>/dev/null`
    if [ $? -ne 0 ]; then echo `date` REJECTED CAN NOT FIND BFID 1>&2; rm  ${tape}_; continue; fi
    pnfsid=`echo $info | sed -e "s/.*pnfsid': '//" -e "s/'.*//"`
    D=`dirname $F`
    cd $D 2>/dev/null
    if [ $? -ne 0 ]; then echo `date` REJECTED CAN NOT FIND DIRECTORY 1>&2; rm  ${tape}_; continue; fi
    FN=`cat ".(nameof)($pnfsid)" 2>/dev/null`
    if [ $? -ne 0 ]; then echo `date` REJECTED CAN NOT FIND FILENAME 1>&2; rm  ${tape}_; continue; fi
    if [ ! -r $FN ]; then echo `date` REJECTED CAN NOT READ  FILE 1>&2; rm  ${tape}_; continue; fi
    file="$FN $R"
    F=$FN
  fi
  rm -f ${tape}_

  # ok, just tell the user the name of the file and do another if needed
  # or copy the file to /dev/null if user requested this
  if [ $copyit -eq 0 ]; then
    echo $file
  else
    cmd="encp --verbose=3  --max-resubmit=7 $F /dev/null"
    echo $cmd
    eval $cmd
    rc=$?
    RC=`expr $RC + $rc`
  fi
  done=`expr $done - 1`
done

rm -f V_D
cd $here

exit $RC
