#!/bin/sh 
#
# Exit Status:
# 0 Alles Ok
# 1 Illegal Request
# 2 Fatal Error
#
# Contributed by Eric DOUTRELEAU <Eric.Doutreleau@int-evry.fr>
# This is supposed to work with Zubkoff/Dandelion version of mtx

# try to hit all the possibilities here
prefix=/usr/local
exec_prefix=${prefix}
libexecdir=${exec_prefix}/libexec
 
PATH=$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb:/usr/local/bin
export PATH


USE_VERSION_SUFFIXES="no"
if test "$USE_VERSION_SUFFIXES" = "yes"; then
	SUF="-2.4.1p1"
else
	SUF=
fi

myname=$0
tape=`getconf$SUF tapedev`
TAPE=`getconf$SUF changerdev`; export TAPE # for mtx command
if [ "$tape" = "/dev/null" -o "$TAPE" = "/dev/null" ]; then
  echo "Both tapedev and changerdev must be specified in config file";
  exit 2;
fi
fake=0
MT=/bin/mt
MTF=-f
MTX=mtx
DD=/bin/dd
firstslot=1
lastslot=6
# counted from 1 !!!
cleanslot=-2

changerfile=`getconf$SUF changerfile`

cleanfile=$changerfile-clean
accessfile=$changerfile-access
slotfile=$changerfile-slot
[ ! -f $cleanfile ] && echo 0 > $cleanfile
[ ! -f $accessfile ] && echo 0 > $accessfile
[ ! -f $slotfile ] && echo 0 > $slotfile
cleancount=`cat $cleanfile`
accesscount=`cat $accessfile`
#

readstatus() {
  used=`$MTX status |
    sed -n 's/Data Transfer Element:.Empty/-1/p;s/Data Transfer Element:.Full (Storage Element \(.\) Loaded)/\1/p'`

  if [ "$used" -eq -1 ]; then
    used=`cat $slotfile`
    fake=1
  fi
}


eject() {
  readstatus 
  if [ $used -gt 0 ];then
    $MTX unload $used 2>/dev/null
    echo 0 $tape
    exit 0
  else
    echo "0 Drive was not loaded"
    exit 1
  fi
}

reset() {
  readstatus
  if [ $used -gt 0 ];then
    $MTX unload $used 2>/dev/null
  fi
  res=`$MTX load 1 2>&1`
  if [ $? -eq 0 ];then
    echo "1 $tape"
    exit 0
  else
    echo "1 $res"
    exit 1
  fi
}
#
#
loadslot() {
  readstatus
  echo "     -> loaded $used" >> /tmp/changer.debug
  whichslot=$1
  case $whichslot in
    current)
	     if [ $used -lt 0 ];then
	       $MTX load 1 2>/dev/null
	       echo "1 $tape"
	       exit 0
	     else 
	       echo "$used $tape"
	       exit 0
	     fi
	     ;;
    next|advance)
	  load=`expr $used + 1`
	  [ $load -gt $lastslot ] && load=$firstslot
	  ;;
    prev)
	  load=`expr $used - 1`
	  [ $load -lt $firstslot ] && load=$lastslot
	  ;;
    first)
	  load=$firstslot
	  ;;
    last)
	  load=$lastslot
	  ;;
    [$firstslot-$lastslot])
	  load=$1
	  ;;
    clean)
	  load=$cleanslot
	  ;;
    *)
       echo "0 illegal request"
       exit 1
       ;;
    esac

    if [ $load = $used ]; then
        echo "$used $tape"
        exit 0
    fi

    if [ $load = $cleanslot ]; then
	expr $cleancount + 1 > $cleanfile
	echo 0 > $accessfile
    else
	expr $accesscount + 1 > $accessfile
	if [ $accesscount -gt 9 ]; then
		$myname -slot clean >/dev/null
	fi
    fi

    # Slot 6 might contain an ordinary tape rather than a cleaning
    # tape. A cleaning tape auto-ejects; an ordinary tape does not.
    # We therefore have to read the status again to check what
    # actually happened.
    readstatus
	

    if [ $fake -eq 0 ];then
      echo "     -> unload $used" >> /tmp/changer.debug
      res=`$MTX unload $used 2>&1`
      status=$?
      echo "     -> status $status" >> /tmp/changer.debug
      echo "     -> res    $res" >> /tmp/changer.debug
      if [ $status -ne 0 ];then
        echo "$res"
        exit 2
      fi
    fi
    if [ $whichslot = advance ];then
      echo "$load /dev/null"
      exit 0
    fi
    echo "     -> load   $load" >> /tmp/changer.debug
    res=`$MTX load $load 2>&1`
    status=$?
    echo "     -> status $status" >> /tmp/changer.debug
    echo "     -> res    $res" >> /tmp/changer.debug
    if [ $status -eq 0 ];then
      echo "     -> rew $load" >> /tmp/changer.debug
      $MT $MTF $tape rewind
      $DD if=$tape count=1 >> /tmp/changer.debug 2>&1
      echo "$load $tape"
      echo "$load" > $slotfile
      exit 0
    else
	res2=`echo $res | grep "Storage Element . is Empty"`
	if [ -z $res2 ]; then
   	  echo "$load $res"
      	  exit 2
	else
	  echo "$load $res"
	  echo "$load" > $slotfile
	  exit 1
	fi
    fi
}
#
info() {
  readstatus
  echo "     -> info   $used" >> /tmp/changer.debug
  if [ $used -lt 0 ];then
    slot=`cat $slotfile`
    echo "$slot $lastslot 1"
  else
    echo "$used $lastslot 1"
  fi
  exit 0
}
#
echo "Args -> $*" >> /tmp/changer.debug
while [ $# -ge 1 ];do
  case $1 in
    -slot)
	   shift
	   loadslot $*
	   ;;
    -info)
	   shift
	   info
	   ;;
    -reset)
	    shift
	    reset
	    ;;
    -eject)
	    shift
	    eject
	    ;;
    *)
       echo "Unknown option $1"
       exit 2
       ;;
  esac
done
