#!/bin/sh 
#
# Exit Status:
# 0 Alles Ok
# 1 Illegal Request
# 2 Fatal Error
#

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

if [ -d "/tmp/amanda" ]; then
	DBGFILE=/tmp/amanda/changer.debug
else
	DBGFILE=/dev/null
fi

USE_VERSION_SUFFIXES="no"
if test "$USE_VERSION_SUFFIXES" = "yes"; then
	SUF="-2.4.2p2"
else
	SUF=
fi

myname=$0
tape=`amgetconf$SUF tapedev`
TAPE=`amgetconf$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
MT=mt
MTF=-f
MTX=mtx
DD=/bin/dd
firstslot=1
lastslot=5
# counted from 1 !!!
cleanslot=6

changerfile=`amgetconf$SUF changerfile`

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

readstatus() {
  used=`$MTX -s |
    sed -n 's/Drive: No tape Loaded/-1/p;s/Drive: tape \(.\) loaded/\1/p'`

  if [ -z "$used" ]; then
    used="-1";
  fi
}


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

reset() {
  readstatus
  if [ $used -gt 0 ];then
    $MTX -u $used
  fi
  res=`$MTX -l 1`
  if [ $? -eq 0 ];then
    echo "1 $tape"
    exit 0
  else
    echo "1 $res"
    exit 1
  fi
}
#
#
loadslot() {
  readstatus
  echo "     -> loaded $used" >> $DBGFILE
  whichslot=$1
  case $whichslot in
    current)
	     if [ $used -lt 0 ];then
	       $MTX -l 1
	       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 [ $used -gt 0 ];then
      echo "     -> unload $used" >> $DBGFILE
      res=`$MTX -u $used`
      status=$?
      echo "     -> status $status" >> $DBGFILE
      echo "     -> res    $res" >> $DBGFILE
      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" >> $DBGFILE
    res=`$MTX -l $load`
    status=$?
    echo "     -> status $status" >> $DBGFILE
    echo "     -> res    $res" >> $DBGFILE
    if [ $status -eq 0 ];then
      echo "     -> rew $load" >> $DBGFILE
      $MT $MTF $tape rewind
      $DD if=$tape count=1 >> $DBGFILE 2>&1
      echo "$load $tape"
      exit 0
    else
      echo "$load $res"
      exit 2
    fi
}
#
info() {
  readstatus
  echo "     -> info   $used" >> $DBGFILE
  if [ $used -lt 0 ];then
    echo "0 $lastslot 1"
  else
    echo "$used $lastslot 1"
  fi
  exit 0
}
#
echo Args "->" "$@" >> $DBGFILE
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
