#!/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
	logfile=/tmp/amanda/changer.debug
else
	logfile=/dev/null
fi

USE_VERSION_SUFFIXES="no"
if test "$USE_VERSION_SUFFIXES" = "yes"; then
	SUF="-2.4.5p1"
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

MTX=mtx

if [ -x $sbindir/ammt$SUF ]; then
	MT=$sbindir/ammt$SUF
	MTF=-f
elif [ -x "/bin/mt" ]; then
	MT=/bin/mt
	MTF=-f
else
	answer="<none> $myname: mt program not found"
	code=1
	echo "Exit -> $answer" >> $logfile
	echo "$answer"
	exit $code
fi
echo MT "->" $MT $MTF >> $logfile

if [ -x $sbindir/amdd$SUF ]; then
	DD=$sbindir/amdd$SUF
elif [ -x "/bin/dd" ]; then
	DD=/bin/dd
else
	answer="<none> $myname: dd program not found"
	code=1
	echo "Exit -> $answer" >> $logfile
	echo "$answer"
	exit $code
fi
echo DD "->" $DD >> $logfile

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
    answer="0 $tape"
    code=0
    echo "Exit -> $answer" >> $logfile
    echo "$answer"
    exit $code
  else
    answer="<none> $myname: Drive was not loaded"
    code=1
    echo "Exit -> $answer" >> $logfile
    echo "$answer"
    exit $code
  fi
}

reset() {
  readstatus
  if [ $used -gt 0 ];then
    $MTX -u $used
  fi
  res=`$MTX -l 1`
  if [ $? -eq 0 ];then
    answer="1 $tape"
    code=0
    echo "Exit -> $answer" >> $logfile
    echo "$answer"
    exit $code
  else
    answer="1 $res"
    code=1
    echo "Exit -> $answer" >> $logfile
    echo "$answer"
    exit $code
  fi
}
#
#
loadslot() {
  readstatus
  echo "     -> loaded $used" >> $logfile
  whichslot=$1
  case $whichslot in
    current)
	     if [ $used -lt 0 ];then
	       $MTX -l 1
	       used=1
	     fi
	     answer="$used $tape"
	     code=0
	     echo "Exit -> $answer" >> $logfile
	     echo "$answer"
	     exit $code
	     ;;
    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
	  ;;
    *)
       answer="<none> $myname: illegal request: \"$whichslot\""
       code=1
       echo "Exit -> $answer" >> $logfile
       echo "$answer"
       exit $code
       ;;
    esac

    if [ $load = $used ]; then
        answer="$used $tape"
        code=0
        echo "Exit -> $answer" >> $logfile
        echo "$answer"
        exit $code
    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" >> $logfile
      res=`$MTX -u $used`
      status=$?
      echo "     -> status $status" >> $logfile
      echo "     -> res    $res" >> $logfile
      if [ $status -ne 0 ];then
        answer="<none> $myname: $res"
        code=2
        echo "Exit -> $answer" >> $logfile
        echo "$answer"
        exit $code
      fi
    fi
    if [ $whichslot = advance ];then
      answer="$load /dev/null"
      code=0
      echo "Exit -> $answer" >> $logfile
      echo "$answer"
      exit $code
    fi
    echo "     -> load   $load" >> $logfile
    res=`$MTX -l $load`
    status=$?
    echo "     -> status $status" >> $logfile
    echo "     -> res    $res" >> $logfile
    if [ $status -eq 0 ];then
      echo "     -> rew $load" >> $logfile
      $MT $MTF $tape rewind
      $DD if=$tape bs=32k count=1 >> $logfile 2>&1
      answer="$load $tape"
      code=0
    else
      answer="$load $res"
      code=2
    fi
    echo "Exit -> $answer" >> $logfile
    echo "$answer"
    exit $code
}
#
info() {
  readstatus
  echo "     -> info   $used" >> $logfile
  if [ $used -lt 0 ];then
    used=0
  fi
  answer="$used $lastslot 1"
  code=0
  echo "Exit -> $answer" >> $logfile
  echo "$answer"
  exit $code
}
#
echo Args "->" "$@" >> $logfile
while [ $# -ge 1 ];do
  case $1 in
    -slot)
	   shift
	   loadslot $*
	   ;;
    -info)
	   shift
	   info
	   ;;
    -reset)
	    shift
	    reset
	    ;;
    -eject)
	    shift
	    eject
	    ;;
    *)
       answer="<none> $myname: Unknown option $1"
       code=2
       echo "Exit -> $answer" >> $logfile
       echo "$answer"
       exit $code
       ;;
  esac
done
