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

# test/$RCSfile$  $Revision$

. /usr/local/etc/setups.sh
setup enstore 

usage="`basename $0` -D drive [-V volume] [-M media-changer] [-E enstore_config_host] [-m] [-d] [-q]
  where
    -m mounts the tape once
    -d dismounts the tape once
and -q returns tape and media_changer for the drive
"

maxtries_drive=50
sleeptime_drive=5

maxtries_robot=360
sleeptime_robot=5

sleeptime=0

dm=1 # dismount flag
mo=1 # mount    flag
on=1 # online   flag
of=1 # offline  flag
co=1 # continuous dismount->mount->dismount flag


# This information belongs in the config file, BUT this script has the
# requirement that it must not depend enstore being up.  Note the
# media changer could be a direct dasadmin call and not go through the
# enstore media changer.  This needs work.

# Also note that if dasadmin is the media_changer, then this needs to
# be run on the correct host - the one that "owns" the right not mount
# a tape on the drive; this is not (usually) the node that the drive's
# scsi cable is connected to.

defaults="DE01-CA2502:CA2513:CA2524:CA2503:CA2547:CA2557:CA2568:CA2579:CA2590:-aml2r1.media_changer-rip7.fnal.gov \
          DE02-CA2504:CA2515:CA2527:CA2505:CA2549:CA2559:CA2570:CA2581:CA2596:-aml2r1.media_changer-rip7.fnal.gov \
          DC03-CA2252:CA2256:CA2257:CA2258:CA2259:CA2260:-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DC04-CA2269:CA2261:CA2272:CA2273:CA2290:CA2291:-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DC05-CA2250:CA2292:CA2293:CA2294:CA2295:CA2296:-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DC06-CA2268:CA2267:CA2297:CA2298:CA2299:-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DM07-000087-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DM08-000088-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DM09-000089-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DM10-000090-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DM11-000091-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DM12-000092-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DE13-CA2548-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DE14-CA2549-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DC15-CA2260-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DC16-CA2261-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DC17-CA2262-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DM18-PRE040:PRE041:PRE042:PRE043:PRE044:PRE045:PRE046:PRE047:-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DM19-CA2949-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DC20-CA2263-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DC21-CA2264-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DC22-CA2265-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DC23-CA2266-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DC24-CA2267-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DC25-CA2268-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DC26-CA2269-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DC27-CA2270-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DC28-CA2271-aml2r2.media_changer-d0ensrv2.fnal.gov \
          DC29-CA2272-aml2r2.media_changer-d0ensrv2.fnal.gov \
          JDE01-CA2530:CA2531-amlj.media_changer-stkensrv2.fnal.gov \
          JDE02-CA2532:CA2533-amlj.media_changer-stkensrv2.fnal.gov \
          JDE03-CA2534:CA2535-amlj.media_changer-stkensrv2.fnal.gov \
          JDE04-CA2536:CA2537-amlj.media_changer-stkensrv2.fnal.gov \
          DI36-PRG000-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI37-PRG001-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI38-PRG002-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI39-PRG003-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI40-PRG004-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI41-PRG005-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI42-PRG006-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI43-PRG007-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI44-PRG008-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI45-PRG009-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI46-PRG010-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI47-PRG011-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI48-PRG012-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI49-PRG013-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI50-PRG014-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI51-PRG015-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI52-PRG016-aml2r1.media_changer-d0ensrv2.fnal.gov \
          DI53-PRG017-aml2r1.media_changer-d0ensrv2.fnal.gov \
          "

media_changer="MISSING"
volume="MISSING"
drive="MISSING"
ens_host="MISSING"
query_flag=0
dismount_flag=0
mount_flag=0


# function to get the next volume from the volume list
popVol() { vol=`echo $volume |cut -f $counter -d: 2>/dev/null`
           if [ -z "$vol" ]; then
              counter=1
              vol=`echo $volume |cut -f $counter -d: 2>/dev/null`
           else
              counter=`expr $counter + 1`
           fi
           echo $vol
         }

# function that takes care of naming convention in the AML/J
driveName() { case $1 in
                JDE01|JDE02|JDE03|JDE04) echo $1 | sed -e 's/J//'
                      ;;
                *) echo $1
                      ;;
              esac
             }

# function that waits for tape to be mounted/dismount
checkdas()  { rdrv=`echo $1 | tr '[a-z]' '[A-Z]'`
              drv=`driveName $drv`
              count=0
              if [ "$media_changer" = "dasadmin" ]; then
                lookfor="dasadmin"
              else
                lookfor="enstore med"
              fi
              echo `date` "   waiting for any $lookfor (dis)mount $drv processes to complete "
              while [ $count -le $maxtries_robot ]; do
                if [ `ps axuww| grep "$lookfor" | grep $drv | egrep -c 'mount|dismoumt'` -eq 0 ]; then
                  echo `date` "   no processes active"
                  return 0
                fi
                sleep $sleeptime_robot
                count=`expr $count + 1`
              done
              echo `date` "robot FAILED, $lookfor process still active"
              exit 1
            }

# function that dismounts a tape
edismount() { tap=`echo $1 | tr '[a-z]' '[A-Z]'`
              rdrv=`echo $2 | tr '[a-z]' '[A-Z]'`
              drv=`driveName $drv`
              # make sure there isn't some other pending request for this drive, exit if there is one pending
              checkdas $rdrv
              if [ "$media_changer" = "dasadmin" ]; then
                 echo `date` "issuing dasadmin dismount -d $drv"
                 dasadmin dismount -d $drv </dev/null >/dev/null 2>&1 &
              else
                 echo `date` "enstore med --dismount $media_changer $tap $drv"
                 enstore med --dismount $media_changer $tap $drv </dev/null >/dev/null 2>&1 &
              fi
              # if just doing once, wait for it to complete, otherwise we will check later
              if [ $co -eq 0 ]; then
                checkdas $rdrv
              fi
              return 0
            }

# function that mounts a tape
emount()    { tap=`echo $1 | tr '[a-z]' '[A-Z]'`
              rdrv=`echo $2 | tr '[a-z]' '[A-Z]'`
              drv=`driveName $drv`
              # make sure there isn't some other pending request for this drive, exit if there is one pending
              checkdas $rdrv
              if [ "$media_changer" = "dasadmin" ]; then
                 if    [ `echo $drv | grep -c DC` -eq 1 ] ; then
                   typ="8MM"
                 elif  [ `echo $drv | grep -c DM` -eq 1 ] ; then
                   typ="8MM"
                 elif  [ `echo $drv | grep -c DE` -eq 1 ] ; then
                   typ="DECDLT"
                 else
                   echo "UNKNOWN DRIVE TYPE" $drv
                   exit 1
                 fi
                 echo `date` "issuing dasadmin mount -t $typ $tap $drv"
                 dasadmin mount -t $typ $tap $drv </dev/null >/dev/null 2>&1 &
              else
                 echo `date` "issuing enstore med --mount $media_changer $tap $drv"
                 enstore med --mount $media_changer $tap $drv  </dev/null >/dev/null 2>&1 &
              fi
              # if just doing once, wait for it to complete, otherwise we will check later
              if [ $co -eq 0 ]; then
                checkdas $rdrv
              fi
              return 0
            }

# function that waits for a drive to go online
eonline()   { rdrv=`echo $1 | tr '[a-z]' '[A-Z]'`
              drv=`driveName $rdrv`
              # make sure there isn't some other pending request for this drive, exit if there is one pending
              checkdas $rdrv
              count=0
              echo `date` "   waiting for drive $rdrv online "
              while [ $count -le $maxtries_drive ]; do
                if [ `rip-drive $rdrv status | egrep -c "ONLINE|READY"` -eq 1 ]; then
                  echo `date` "  online"
                  return 0
                fi
                sleep $sleeptime_drive
                count=`expr $count + 1`
              done
              echo `date` "drive $rdrv failed to go ONLINE"
              exit 1
            }

# function that waits for a drive to go offline
eoffline()  { rdrv=`echo $1 | tr '[a-z]' '[A-Z]'`
              drv=`driveName $rdrv`
              # make sure there isn't some other pending request for this drive, exit if there is one pending
              checkdas $rdrv
              get_OS=`rip-drive $rdrv uname |cut -f1 -d\ `
              rOS=`echo $get_OS|cut -f2 -d\ `
              case $rOS in
                   IRIX*) offline=unload;;
                   *)     offline=offline;;
              esac
              echo `date` "issuing $offline to $rdrv"
              rip-drive $rdrv $offline 1>/dev/null 2>&1
              # can't check status of rip-drive offline command because of the case
              # where the tape is already ejected, then an offline command returns an error
              # and this is really ok, so just continue - will error out on the dr_open
              # check if tape can't be ejected.
              count=0
              echo `date` "   waiting for drive $rdrv offline "
              while [ $count -le $maxtries_drive ]; do
                if [ `rip-drive $rdrv status | egrep -c "DR_OPEN|Not READY"` -eq 1 ]; then
                  echo `date` "   offline"
                  return 0
                fi
                sleep $sleeptime_drive
                count=`expr $count + 1`
              done
              echo `date` "drive $rdrv failed to eject tape go OFFLINE"
              exit 1
            }


# parse the options
while getopts dmM:V:D:qe:s:h flag ; do
  case $flag in
       d)  dismount_flag=1; dm=1; mo=0; on=0; of=1; co=0     # dismount
           ;;
       m)  mount_flag=1;    dm=1; mo=1; on=1; of=1; co=0     # mount
           ;;
       M)  media_changer=$OPTARG                             # media changer
           ;;
       V)  volume=`echo $OPTARG|tr '[a-z]' '[A-Z]'`          # volume
           ;;
       D)  drive=`echo $OPTARG|tr '[a-z]' '[A-Z]'`           # drive
           ;;
       q)  query_flag=1;    co=0                             # query
           ;;
       e)  ens_host=$OPTARG                                  # enstore config host
           ;;
       s)  sleeptime=$OPTARG                                 # sleep time between successive cycles
           ;;
       h) echo "$usage" ; exit 0                             # help
          ;;
       \?) echo BAD SYNTAX. Correct usage is >&2; echo "$usage">&2; exit 1
   esac
done
shift `expr $OPTIND - 1`        # shift options away

# we need the drive to be specified
if [ "$drive" = "MISSING" ]; then
    echo "Drive must be specified"
    echo "$usage"
    exit 1
fi

if [ "$volume" = "MISSING" -o "$media_changer" = "MISSING" -o "$ens_host" = "MISSING" ]; then

  # get the default string for this drive
  default_info=`echo $defaults | sed -e "s/.*\(${drive}\)/\1/" | cut -f1 -d\ `
  if [ `echo $default_info|cut -f1 -d-` != ${drive} ]; then
     echo "Can not find defaults for  tape for drive $drive"
     exit 1
  fi

  # if the volume isn't specified, get one based on the default for the drive
  if [ "$volume" = "MISSING" ]; then
      volume="`echo $default_info|cut -f2 -d-`"
      if [ "${volume:-MISSING}" = "MISSING" ]; then
         echo "Can not find default tape for drive $drive"
         exit 1
      fi
  fi

  # if the media_changer isn't specified, get one based on the default for the drive
  if [ "$media_changer" = "MISSING" ]; then
      media_changer="`echo $default_info|cut -f3 -d-`"
      if [ "${media_changer:-MISSING}" = "MISSING" ]; then
         echo "Can not find default media_changer for drive $drive"
         exit 1
      fi
  fi

  # if the enstore config host isn't specified, get one based on the default for the drive
  if [ "$ens_host" = "MISSING" ]; then
      ens_host="`echo $default_info|cut -f4 -d-`"
      if [ "${ens_host:-MISSING}" = "MISSING" ]; then
         echo "Can not find default enstore host for drive $drive"
         exit 1
      fi
  fi
fi

if [ "$media_changer" != "dasadmin" ]; then
    export ENSTORE_CONFIG_HOST=$ens_host
    export ENSTORE_CONFIG_PORT=7500 # bad, but this will never change while I work here
    # should I check if media changer is alive?
fi

# print out a little message to calm users
if [ $co -eq 1 ]; then
    echo "`date` Starting continuous test cycle for drive $drive with volume $volume using media_changer $media_changer ($ens_host configuration)"
else
    if [ $dismount_flag -ne 0 ]; then
       echo "`date` Dismounting tape from drive $drive using media_changer $media_changer ($ens_host configuration)"
       echo
    elif [ $mount_flag -ne 0 ]; then
       echo "`date` Mounting volume $volume on drive $drive using media_changer $media_changer ($ens_host configuration)"
       echo
    elif [ $query_flag -ne 0 ]; then
       echo "`date` Defaults for drive $drive are volume $volume and media_changer $media_changer ($ens_host configuration)"
       scsi_node=`conf.sh | grep $drive|cut -f2 -d:`
       if [ -z "$scsi_node" ]; then
          scsi_node="UNKNOWN (call developers)"
          other_movers="UNKNOWN (call developers)"
       else
         other_movers="`conf.sh |grep $scsi_node|cut -f1 -d:`"
       fi
       echo "`date` Drive $drive has scsi connections to $scsi_node.  Movers on this node are" $other_movers
       exit 0
    fi
fi

counter=1
volume="$volume:"


while true; do

  vol2use=`popVol`

# offline the drive and check for offline drive status
  if [ $of -eq 1 ]; then
     echo `date` "offline $drive"
     eoffline $drive
     stat=$?
     echo " " $stat `date`
  fi

# dismount tape from drive
  if [ $dm -eq 1 ]; then
     echo `date` "dismount $drive"
     edismount $vol2use $drive
     stat=$?
     echo " " $stat `date`
  fi

# mount tape in drive
  if [ $mo -eq 1 ]; then
     echo `date` "mount $vol2use $drive"
     emount $vol2use $drive
     stat=$?
     echo " " $stat `date`
  fi

# check for online drive status
  if [ $on -eq 1 ]; then
     echo `date` "online $drive"
     eonline $drive
     stat=$?
     echo " " $stat `date`
  fi

  if [ $co -eq 0 ]; then break; fi

  sleep $sleeptime

done
