#!/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

aits="DM07-CA2901 DM08-CA2902 DM09-CA2903 DM10-CA2904 DM11-CA2905 DM12-CA2906"
mammoths="DC03-CA2251 DC04-CA2252 DC05-CA2253 DC06-CA2254"
dlts="DE01-CA2501 DE02-CA2502"

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

#dismount drive hack
if [ "${1:-}" = "-d" ]; then dm=1; mo=0; on=0; of=1; co=0; shift; fi

#mount drive hack
if [ "${1:-}" = "-m" ]; then dm=1; mo=1; on=1; of=1; co=0; shift; fi

# media changer hack.  if media_changer == dasadmin, then use dasadmin instead of enstore
media_changer=aml2r1.media_changer
if [ "${1:-}" = "-M" ]; then media_changer=${2:-MISSING}; shift; shift; fi

#specify drive-tape, this can be an individual drive-tape or a set or aits/dlts/mammoths
if [ -n "${1-}" ]; then
  if   [ "$1" = "aits" ];     then sets="$aits"
  elif [ "$1" = "dlts" ];     then sets="$dlts"
  elif [ "$1" = "mammoths" ]; then sets="$mammoths"
  else                             sets="$1"
  fi
  shift
else
  sets="$aits $mammoths $dlts"
fi

maxtries=300
sleeptime=3


checkdas()  { td=$1
              count=0
              if [ "$media_changer" = "dasadmin" ]; then
                lookfor="dasadmin"
              else
                lookfor="enstore med"
              fi
              while [ $count -le $maxtries ]; do
                if [ `ps axuww| grep "$lookfor" | grep $td | egrep -c 'mount|dismoumt'` -eq 0 ]; then
  		  /bin/echo -n " " `date` mcclr
                  return 0
                fi
                sleep $sleeptime
                count=`expr $count + 1`
              done
  	      /bin/echo -n " " `date` robot FAILED
              return 1
            }


edismount() { tap=`echo $1 | tr '[a-z]' '[A-Z]'`
              drv=`echo $2 | tr '[a-z]' '[A-Z]'`
              checkdas $drv
              if [ $? -eq 0 ]; then
                if [ "$media_changer" = "dasadmin" ]; then
                   /bin/echo -n " " `date` issuing dasadmin dismount -d $drv
                   dasadmin dismount -d $drv </dev/null >/dev/null 2>&1 &
                else
                   /bin/echo -n " " `date` enstore med --dismount $media_changer $tap $drv
                   enstore med --dismount $media_changer $tap $drv </dev/null >/dev/null 2>&1 &
                fi
                if [ $co -eq 0 ]; then
                  checkdas $drv
                fi
                return 0
              fi
              return 1
            }

emount()    { tap=`echo $1 | tr '[a-z]' '[A-Z]'`
              drv=`echo $2 | tr '[a-z]' '[A-Z]'`
              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
                return 0
              fi
              checkdas $drv
              if [ $? -eq 0 ]; then
                if [ "$media_changer" = "dasadmin" ]; then
                   /bin/echo -n " "`date` issuing dasadmin mount -t $typ $tap $drv
                   dasadmin mount -t $typ $tap $drv </dev/null >/dev/null 2>&1 &
                else
                   /bin/echo -n " " `date` issuing enstore med --mount $media_changer $tap $drv
                   enstore med --mount $media_changer $tap $drv  </dev/null >/dev/null 2>&1 &
                fi
                if [ $co -eq 0 ]; then
                  checkdas $drv
                fi
                return 0
              fi
              return 1
            }

eonline()   { drv=`echo $1 | tr '[a-z]' '[A-Z]'`
              checkdas $drv
              if [ $? -eq 0 ]; then
                count=0
                while [ $count -le $maxtries ]; do
                  if [ `rip-drive $drv status | grep -c "ONLINE"` -eq 1 ]; then
                    return 0
                  fi
                  sleep $sleeptime
                  count=`expr $count + 1`
                done
              fi
              return 1
            }

eoffline()  { drv=`echo $1 | tr '[a-z]' '[A-Z]'`
              checkdas $drv
              if [ $? -eq 0 ]; then
                rip-drive $drv 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
                while [ $count -le $maxtries ]; do
                  if [ `rip-drive $drv status | grep -c "DR_OPEN"` -eq 1 ]; then
                    return 0
                  fi
                  sleep $sleeptime
                  count=`expr $count + 1`
                done
              fi
              return 1
            }

while true; do

# offline the drives and check for offline drive status
  if [ $of -eq 1 ]; then
    for aset in $sets; do
      drive=`echo $aset | cut -f1 -d-`
      eval ok=\${I$drive:-0}
      if [ $ok -eq 0 ]; then
        /bin/echo -n `date` offline $drive
        eoffline $drive
        stat=$?
        eval I$drive=\$stat
        echo " " $stat `date`
      fi
    done
  fi

# dismount drives
  if [ $dm -eq 1 ]; then
    for aset in $sets; do
      drive=`echo $aset | cut -f1 -d-`
      tape=`echo  $aset | cut -f2 -d-`
      eval ok=\${I$drive:-0}
      /bin/echo -n `date` dismount $drive $ok
      edismount $tape $drive
      stat=$?
      eval I$drive=\$stat
      echo " " $stat `date`
    done
  fi

# mount drives
  if [ $mo -eq 1 ]; then
    for aset in $sets; do
      drive=`echo $aset | cut -f1 -d-`
      tape=`echo  $aset | cut -f2 -d-`
      eval ok=\${I$drive:-0}
      if [ $ok -eq 0 ]; then
        /bin/echo -n `date` mount $tape $drive
        emount $tape $drive
        stat=$?
        eval I$drive=\$stat
        echo " " $stat `date`
      fi
    done
  fi

# check for online drive status
  if [ $on -eq 1 ]; then
    for aset in $sets; do
      drive=`echo $aset | cut -f1 -d-`
      eval ok=\${I$drive:-0}
      if [ $ok -eq 0 ]; then
        /bin/echo -n `date` online $drive
        eonline $drive
        stat=$?
        eval I$drive=\$stat
        echo " " $stat `date`
      fi
    done
  fi

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

done
