Imported Debian patch 2.5.2p1-1
[debian/amanda] / changer-src / chg-multi.sh.in
index 9ac098ee4afa45b4af6265a3a5a3242c20633df0..a5d3fdbbe14a068a74ebc4b5da1c25a163c33e83 100644 (file)
 # chg-multi.sh - generic tape changer script
 #
 
-prefix=@prefix@
+# source utility functions and values from configure
 exec_prefix=@exec_prefix@
-sbindir=@sbindir@
 libexecdir=@libexecdir@
+. ${libexecdir}/chg-lib.sh
 
 pname="chg-multi"
 
-PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb
-export PATH
-
 if [ -d "@AMANDA_DBGDIR@" ]; then
        logfile=@AMANDA_DBGDIR@/changer.debug
 else
        logfile=/dev/null
 fi
 
-echo Args "->" "$@" >> $logfile
+echo `_ "arguments ->"` "$@" >> $logfile
 
-USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
-if test "$USE_VERSION_SUFFIXES" = "yes"; then
-       SUF="-@VERSION@"
-else
-       SUF=
-fi
+ourconf=`amgetconf changerfile`
 
-ourconf=`amgetconf$SUF changerfile`
-
-if [ -x $sbindir/ammt$SUF ]; then
-       MT=$sbindir/ammt$SUF
-       MTF=-f
-elif [ -x "@MT@" ]; then
-       MT=@MT@
-       MTF=@MT_FILE_FLAG@
-else
-       answer="<none> $myname: mt program not found"
-       code=1
-       echo "Exit -> $answer" >> $logfile
-       echo "$answer"
-       exit $code
+if ! error=try_find_mt; then
+    echo <none> $error
+    exit 2
 fi
-echo MT "->" $MT $MTF >> $logfile
 
 EXPR=expr
 # EXPR=/usr/local/bin/expr # in case you need a more powerful expr...
@@ -78,85 +58,78 @@ EXPR=expr
 # read in some config parameters
 
 if [ ! -f "$ourconf" ]; then
-       answer="<none> $pname: $ourconf does not exist"
-       code=2
-       echo "Exit -> $answer" >> $logfile
-       echo "$answer"
-       exit $code
+       answer=`_ '<none> %s: %s does not exist' "$pname" "$ourconf"`
+       echo `_ 'Exit ->'` $answer >> $logfile
+       echo $answer
+       exit 2
 fi
 
 firstslot=`awk '$1 == "firstslot" {print $2}' $ourconf 2>/dev/null`
 if [ -z "$firstslot" ]; then
-       answer="<none> $pname: firstslot not specified in $ourconf"
-       code=2
-       echo "Exit -> $answer" >> $logfile
-       echo "$answer"
-       exit $code
+       answer=`_ '<none> %s: firstslot not specified in %s' "$pname" "$ourconf"`
+       echo `_ 'Exit ->'` $answer >> $logfile
+       echo $answer
+       exit 2
 fi
 
 lastslot=`awk '$1 == "lastslot" {print $2}' $ourconf 2>/dev/null`
 if [ -z "$lastslot" ]; then
-       answer="<none> $pname: lastslot not specified in $ourconf"
-       code=2
-       echo "Exit -> $answer" >> $logfile
-       echo "$answer"
-       exit $code
+       answer=`_ '<none> %s: lastslot not specified in %s' "$pname" "$ourconf"`
+       echo `_ 'Exit ->'` $answer >> $logfile
+       echo $answer
+       exit 2
 fi
 
 nslots=`$EXPR $lastslot - $firstslot + 1`
 
 gravity=`awk '$1 == "gravity" {print $2}' $ourconf 2>/dev/null`
 if [ -z "$gravity" ]; then
-       answer="<none> $pname: gravity not specified in $ourconf"
-       code=2
-       echo "Exit -> $answer" >> $logfile
-       echo "$answer"
-       exit $code
+       answer=`_ '<none> %s: gravity not specified in %s' "$pname" "$ourconf"`
+       echo `_ 'Exit ->'` $answer >> $logfile
+       echo $answer
+       exit 2
 fi
 
 needeject=`awk '$1 == "needeject" {print $2}' $ourconf 2>/dev/null`
 if [ -z "$needeject" ]; then
-       answer="<none> $pname: needeject not specified in $ourconf"
-       code=2
-       echo "Exit -> $answer" >> $logfile
-       echo "$answer"
-       exit $code
+       answer=`_ '<none> %s: needeject not specified in %s' "$pname" "$ourconf"`
+       echo `_ 'Exit ->'` $answer >> $logfile
+       echo $answer
+       exit 2
 fi
 
 multieject=`awk '$1 == "multieject" {print $2}' $ourconf 2>/dev/null`
 if [ -z "$multieject" ]; then
-       echo "Note: setting multieject to a default of zero" >> $logfile
+       echo `_ 'Note: setting multieject to a default of zero'` >> $logfile
        multieject=0
 fi
 
 ejectdelay=`awk '$1 == "ejectdelay" {print $2}' $ourconf 2>/dev/null`
 if [ -z "$ejectdelay" ]; then
-       echo "Note: setting ejectdelay to a default of zero" >> $logfile
+       echo `_ 'Note: setting ejectdelay to a default of zero'` >> $logfile
        ejectdelay=0
 fi
 
 posteject=`awk '$1 == "posteject" {print $2}' $ourconf 2>/dev/null`
 if [ -z "$posteject" ]; then
-       echo "Note: setting posteject to a default of \"true\"" >> $logfile
+       echo `_ 'Note: setting posteject to a default of "true"'` >> $logfile
        posteject=true
 fi
 
 ourstate=`awk '$1 == "statefile" {print $2}' $ourconf 2>/dev/null`
 if [ -z "$ourstate" ]; then
-       answer="<none> $pname: statefile not specified in $ourconf"
-       code=2
-       echo "Exit -> $answer" >> $logfile
-       echo "$answer"
-       exit $code
+       answer=`_ '<none> %s: statefile not specified in %s' "$pname" "$ourconf"`
+       echo `_ 'Exit ->'` $answer >> $logfile
+       echo $answer
+       exit 2
 fi
 
 # needeject and multieject are incompatible
 if [ $needeject -eq 1 ] && [ $multieject -eq 1 ] ; then
-       answer="<none> $pname: needeject and multieject cannot be both enabled in $ourconf"
-       code=2
-       echo "Exit -> $answer" >> $logfile
-       echo "$answer"
-       exit $code
+       answer=`_ '<none> %s: needeject and multieject cannot be both enabled in %s' "$pname" "$ourconf"`
+       echo `_ 'Exit ->'` $answer >> $logfile
+       echo $answer
+       exit 2
 fi
 
 # read in state: only curslot and curloaded at the present time
@@ -190,10 +163,9 @@ case "$command" in
 
        backwards=`$EXPR 1 - $gravity`
        answer="$curslot $nslots $backwards"
-       code=0
-       echo "Exit -> $answer" >> $logfile
-       echo "$answer"
-       exit $code
+       echo `_ 'Exit ->'` $answer >> $logfile
+       echo $answer
+       exit 0
        ;;
 
 -reset) # reset changer. Actually, we only reset changer state. We
@@ -224,18 +196,16 @@ case "$command" in
            ([ $gravity -eq 0 ] || [ $curslot -ne $lastslot ]) ; then 
                # Can't do this: if we eject, the stacker is going to
                # load the next tape, and our state will be botched
-               answer="$curslot $pname: Cannot use -eject with multieject/nogravity/notlastslot"
-               code=1
-               echo "Exit -> $answer" >> $logfile
-               echo "$answer"
-               exit $code
+               answer=`_ '%s %s: Cannot use -eject with multieject/nogravity/notlastslot' "$curslot" "$pname"`
+               echo `_ 'Exit ->'` $answer >> $logfile
+               echo $answer
+               exit 1
        fi    
        if [ $curloaded -eq 0 ]; then
-               answer="$curslot $pname: slot already empty"
-               code=1
-               echo "Exit -> $answer" >> $logfile
-               echo "$answer"
-               exit $code
+               answer=`_ '%s %s: slot already empty' "$curslot" "$pname"`
+               echo `_ 'Exit ->'` $answer >> $logfile
+               echo $answer
+               exit 1
        fi
        ;;
 
@@ -250,11 +220,10 @@ case "$command" in
                newslot=$slotparm
                if [ $newslot -gt $lastslot ] || \
                     [ $newslot -lt $firstslot ] ; then
-                       answer="$newslot $pname: no slot $newslot: legal range is $firstslot ... $lastslot"
-                       code=1
-                       echo "Exit -> $answer" >> $logfile
-                       echo "$answer"
-                       exit $code
+                       answer=`_ '%s %s: no slot %s: legal range is %s ... %s' "$newslot" "$pname" "$newslot" "$firstslot" "$lastslot"`
+                       echo `_ 'Exit ->'` $answer >> $logfile
+                       echo $answer
+                       exit 1
                fi
                ;;
        current)
@@ -282,11 +251,10 @@ case "$command" in
                fi
                ;;
        *)
-               answer="<none> $pname: bad slot name \"$slotparm\""
-               code=1
-               echo "Exit -> $answer" >> $logfile
-               echo "$answer"
-               exit $code
+               answer=`_ '<none> %s: bad slot name "%s"' "$pname" "$slotparm"`
+               echo `_ 'Exit ->'` $answer >> $logfile
+               echo $answer
+               exit 1
                ;;
        esac
        ;;
@@ -297,11 +265,10 @@ esac
 
 
 if [ $usage -eq 1 ]; then
-       answer="<none> usage: $pname {-reset | -slot [<slot-number>|current|next|prev|advance] | -info | -eject}"
-       code=2
-       echo "Exit -> $answer" >> $logfile
-       echo "$answer"
-       exit $code
+       answer=`_ '<none> usage: %s {-reset | -slot [<slot-number>|current|next|prev|advance] | -info | -eject}' "$pname"`
+       echo `_ 'Exit ->'` $answer >> $logfile
+       echo $answer
+       exit 2
 fi
 
 
@@ -309,11 +276,10 @@ fi
 
 if [ $checkgravity -eq 1 ] && [ $gravity -ne 0 ] ; then
        if [ $newslot -lt $curslot ] || [ "$slotparm" = "prev" ] ; then
-               answer="$newslot $pname: cannot go backwards in gravity stacker"
-               code=1
-               echo "Exit -> $answer" >> $logfile
-               echo "$answer"
-               exit $code
+               answer=`_ '%s %s: cannot go backwards in gravity stacker' "$newslot" "$pname"`
+               echo `_ 'Exit ->'` $answer >> $logfile
+               echo $answer
+               exit 1
        fi
 fi
 
@@ -328,31 +294,27 @@ then
        while [ $curslot -ne $newslot ]; do
            device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
            if [ "$device" = "" ]; then
-               answer="$curslot $pname: slot $curslot device not specified in $ourconf"
-               code=2
-               echo "Exit -> $answer" >> $logfile
-               echo "$answer"
-               exit $code
+               answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
+               echo `_ 'Exit ->'` $answer >> $logfile
+               echo $answer
+               exit 2
            fi
-           echo "     -> offline $device" >> $logfile
-           $MT $MTF $device offline >> $logfile 2>&1
-           if [ $? -ne 0 ]; then
-               answer="$newslot $pname: $device: unable to change to slot $curslot"
-               code=2
-               echo "Exit -> $answer" >> $logfile
-               echo "$answer"
-               exit $code
+           echo `_ '     -> offline'` "$device" >> $logfile
+            if ! try_eject_device $device; then
+               answer=`_ '%s %s: %s: unable to change to slot %s' "$newslot" "$pname" "$device" "$curslot"`
+               echo `_ 'Exit ->'` $answer >> $logfile
+               echo $answer
+               exit 2
            fi
            [ $ejectdelay -gt 0 ] && sleep $ejectdelay
-           echo "     -> running $posteject $device" >> $logfile
+           echo `_ '     -> running'` $posteject $device >> $logfile
            $posteject $device >> $logfile 2>&1
            status=$?
            if [ $status -ne 0 ]; then
-               answer="$newslot $pname: $posteject $device failed: $status"
-               code=2
-               echo "Exit -> $answer" >> $logfile
-               echo "$answer"
-               exit $code
+               answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
+               echo `_ 'Exit ->'` $answer >> $logfile
+               echo $answer
+               exit 2
            fi
            curslot=`$EXPR $curslot + 1`
            if [ $curslot -gt $lastslot ] ; then
@@ -375,14 +337,13 @@ then
        # try to unload the current device
        device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
        if [ "$device" = "" ]; then
-               answer="$curslot $pname: slot $curslot device not specified in $ourconf"
-               code=2
-               echo "Exit -> $answer" >> $logfile
-               echo "$answer"
-               exit $code
+               answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
+               echo `_ 'Exit ->'` $answer >> $logfile
+               echo $answer
+               exit 2
        fi
-       echo "     -> offline $device" >> $logfile
-       $MT $MTF $device offline >> $logfile 2>&1
+       echo `_ '     -> offline'` $device >> $logfile
+        try_eject_device $device
        if [ $? -ne 0 ]; then
                #
                # XXX if the changer-specific eject command can distinguish
@@ -393,15 +354,14 @@ then
                slotempty=1
        else
                [ $ejectonly -eq 0 ] && [ $ejectdelay -gt 0 ] && sleep $ejectdelay
-               echo "     -> running $posteject $device" >> $logfile
+               echo `_ '     -> running '` $posteject $device >> $logfile
                $posteject $device >> $logfile 2>&1
                status=$?
                if [ $status -ne 0 ]; then
-                       answer="$newslot $pname: $posteject $device failed: $status"
-                       code=2
-                       echo "Exit -> $answer" >> $logfile
-                       echo "$answer"
-                       exit $code
+                       answer=`_ '%s %s: %s %s failed: %s' "$newslot" "$pname" "$posteject" "$device" "$status"`
+                       echo `_ 'Exit ->'` $answer >> $logfile
+                       echo $answer
+                       exit 2
                fi
        fi
 fi
@@ -417,14 +377,12 @@ if [ $loadslot -eq 1 ]; then      # load the tape from the slot
        # try to rewind the device
        device=`awk '$1 == "slot" && $2 == '$curslot' {print $3}' $ourconf 2>/dev/null`
        if [ "$device" = "" ]; then
-               answer="$curslot $pname: slot $curslot device not specified in $ourconf"
-               code=2
-               echo "Exit -> $answer" >> $logfile
-               echo "$answer"
-               exit $code
+               answer=`_ '%s %s: slot %s device not specified in %s' "$curslot" "$pname" "$curslot" "$ourconf"`
+               echo `_ 'Exit ->'` $answer >> $logfile
+               echo $answer
+               exit 2
        fi
-       echo "     -> rewind $device" >> $logfile
-       $MT $MTF $device rewind >> $logfile 2>&1
+        amdevcheck_status $device
        if [ $? -ne 0 ]; then
                #
                # XXX if the changer-specific load command can distinguish
@@ -439,18 +397,17 @@ fi
 
 # update state
 
-echo "# multi-changer state cache: DO NOT EDIT!" >  $ourstate
+echo `_ '# multi-changer state cache: DO NOT EDIT!'` >  $ourstate
 echo curslot $newslot                           >> $ourstate
 echo curloaded $curloaded                       >> $ourstate
 
 # return slot info
 
 if [ $slotempty -eq 1 ]; then
-       answer="$newslot $pname: slot is empty"
-       code=1
-       echo "Exit -> $answer" >> $logfile
-       echo "$answer"
-       exit $code
+       answer=`_ '%s %s: slot is empty' "$newslot" "$pname"`
+       echo `_ 'Exit ->'` $answer >> $logfile
+       echo $answer
+       exit 1
 fi
 
 if [ "$command" = -slot -a "$slotparm" = advance ]; then
@@ -458,7 +415,6 @@ if [ "$command" = -slot -a "$slotparm" = advance ]; then
 fi
 
 answer="$newslot $device"
-code=0
-echo "Exit -> $answer" >> $logfile
-echo "$answer"
-exit $code
+echo `_ 'Exit ->'` $answer >> $logfile
+echo $answer
+exit 0