commit untracked files ... confusing
[debian/amanda] / changer-src / chg-mtx.sh
1 #!@SHELL@ 
2 #
3 # Exit Status:
4 # 0 Alles Ok
5 # 1 Illegal Request
6 # 2 Fatal Error
7 #
8
9 # source utility functions and values from configure
10 prefix=@prefix@
11 exec_prefix=@exec_prefix@
12 amlibexecdir=@amlibexecdir@
13 . ${amlibexecdir}/chg-lib.sh
14
15 if [ -d "@AMANDA_DBGDIR@" ]; then
16         logfile=@AMANDA_DBGDIR@/changer.debug
17 else
18         logfile=/dev/null
19 fi
20
21 myname=$0
22
23 tape=`amgetconf tapedev`
24 if [ -z "$tape" ]; then
25   echo "<none> tapedev not specified in amanda.conf";
26   exit 2;
27 fi
28
29 TAPE=`amgetconf changerdev`; export TAPE # for mtx command
30 if [ -z "$TAPE" ]; then
31   echo "<none> changerdev not specified in amanda.conf";
32   exit 2;
33 fi
34
35 if [ "$tape" = "/dev/null" -o "$TAPE" = "/dev/null" ]; then
36   echo "<none> Both tapedev and changerdev must be specified in config file";
37   exit 2;
38 fi
39
40 MTX=@MTX@
41
42 if [ -x $sbindir/ammt$SUF ]; then
43         MT=$sbindir/ammt$SUF
44         MTF=-f
45 elif [ -x "@MT@" ]; then
46         MT=@MT@
47         MTF=@MT_FILE_FLAG@
48 else
49         answer="<none> $myname: mt program not found"
50         code=1
51         echo "Exit -> $answer" >> $logfile
52         echo "$answer"
53         exit $code
54 fi
55 echo MT "->" $MT $MTF >> $logfile
56
57 if [ -x $sbindir/amdd$SUF ]; then
58         DD=$sbindir/amdd$SUF
59 elif [ -x "@DD@" ]; then
60         DD=@DD@
61 else
62         answer="<none> $myname: dd program not found"
63         code=1
64         echo "Exit -> $answer" >> $logfile
65         echo "$answer"
66         exit $code
67 fi
68 echo DD "->" $DD >> $logfile
69
70 firstslot=1
71 lastslot=5
72 # counted from 1 !!!
73 cleanslot=6
74
75 changerfile=`amgetconf changerfile`
76
77 cleanfile=$changerfile-clean
78 accessfile=$changerfile-access
79 [ ! -f $cleanfile ] && echo 0 > $cleanfile
80 [ ! -f $accessfile ] && echo 0 > $accessfile
81 cleancount=`cat $cleanfile`
82 accesscount=`cat $accessfile`
83 #
84
85 readstatus() {
86   used=`$MTX -s |
87     sed -n 's/Drive: No tape Loaded/-1/p;s/Drive: tape \(.\) loaded/\1/p'`
88
89   if [ -z "$used" ]; then
90     used="-1";
91   fi
92 }
93
94
95 eject() {
96   readstatus 
97   if [ $used -gt 0 ];then
98     $MTX -u $used
99     answer="0 $tape"
100     echo `_ 'Exit ->'` $answer >> $logfile
101     echo $answer
102     exit 0
103   else
104     answer=`_ '<none> %s: Drive was not loaded' "$myname"`      
105     echo `_ 'Exit ->'` $answer >> $logfile
106     echo $answer
107     exit 1
108   fi
109 }
110
111 reset() {
112   readstatus
113   if [ $used -gt 0 ];then
114     $MTX -u $used
115   fi
116   res=`$MTX -l 1`
117   if [ $? -eq 0 ];then
118     answer="1 $tape"
119     echo `_ 'Exit ->'` $answer >> $logfile
120     echo $answer
121     exit 0
122   else
123     answer="1 $res"
124     echo `_ 'Exit ->'` $answer >> $logfile
125     echo $answer
126     exit 1
127   fi
128 }
129 #
130 #
131 loadslot() {
132   readstatus
133   echo "     -> loaded $used" >> $logfile
134   whichslot=$1
135   case $whichslot in
136     current)
137              if [ $used -lt 0 ];then
138                $MTX -l 1
139                used=1
140              fi
141              answer="$used $tape"
142              echo `_ 'Exit ->'` $answer >> $logfile
143              echo $answer
144              exit 0
145              ;;
146     next|advance)
147           load=`expr $used + 1`
148           [ $load -gt $lastslot ] && load=$firstslot
149           ;;
150     prev)
151           load=`expr $used - 1`
152           [ $load -lt $firstslot ] && load=$lastslot
153           ;;
154     first)
155           load=$firstslot
156           ;;
157     last)
158           load=$lastslot
159           ;;
160     [$firstslot-$lastslot])
161           load=$1
162           ;;
163     clean)
164           load=$cleanslot
165           ;;
166     *)
167        answer=`_ '<none> %s: illegal request: "%s"' "$myname" "$whichslot"`
168        echo `_ 'Exit ->'` $answer >> $logfile
169        echo $answer
170        exit 1
171        ;;
172     esac
173
174     if [ $load = $used ]; then
175         answer="$used $tape"
176         echo `_ 'Exit ->'` $answer >> $logfile
177         echo $answer
178         exit 0
179     fi
180
181     if [ $load = $cleanslot ]; then
182         expr $cleancount + 1 > $cleanfile
183         echo 0 > $accessfile
184     else
185         expr $accesscount + 1 > $accessfile
186         if [ $accesscount -gt 9 ]; then
187                 $myname -slot clean >/dev/null
188         fi
189     fi
190
191     # Slot 6 might contain an ordinary tape rather than a cleaning
192     # tape. A cleaning tape auto-ejects; an ordinary tape does not.
193     # We therefore have to read the status again to check what
194     # actually happened.
195     readstatus
196         
197
198     if [ $used -gt 0 ];then
199       echo "     -> unload $used" >> $logfile
200       res=`$MTX -u $used`
201       status=$?
202       echo "     -> status $status" >> $logfile
203       echo "     -> res    $res" >> $logfile
204       if [ $status -ne 0 ];then
205         answer=`_ '<none> %s: %s' "$myname" "$res"`
206         echo `_ 'Exit ->'` $answer >> $logfile
207         echo $answer
208         exit 2
209       fi
210     fi
211     if [ $whichslot = advance ];then
212       answer="$load /dev/null"
213       echo `_ 'Exit ->'` $answer >> $logfile
214       echo $answer
215       exit 0
216     fi
217     echo `_ '     -> load   %s' "$load"` >> $logfile
218     res=`$MTX -l $load`
219     status=$?
220     echo `_ '     -> status %s' "$status"` >> $logfile
221     echo `_ '     -> result %s' "$res"` >> $logfile
222     if [ $status -eq 0 ];then
223       amdevcheck_status $tape
224       answer="$load $tape"
225       code=0
226     else
227       answer="$load $res"
228       code=2
229     fi
230     echo `_ 'Exit ->'` $answer >> $logfile
231     echo $answer
232     exit $code
233 }
234 #
235 info() {
236   readstatus
237   echo "     -> info   $used" >> $logfile
238   if [ $used -lt 0 ];then
239     used=0
240   fi
241   answer="$used $lastslot 1"
242   echo `_ 'Exit ->'` $answer >> $logfile
243   echo $answer
244   exit 0
245 }
246 #
247 echo Args "->" "$@" >> $logfile
248 while [ $# -ge 1 ];do
249   case $1 in
250     -slot)
251            shift
252            loadslot $*
253            ;;
254     -info)
255            shift
256            info
257            ;;
258     -reset)
259             shift
260             reset
261             ;;
262     -eject)
263             shift
264             eject
265             ;;
266     *)
267        answer=`_ '<none> %s: Unknown option %s' "$myname" "$1"`
268        echo `_ 'Exit ->'` $answer >> $logfile
269        echo $answer
270        exit 2
271        ;;
272   esac
273 done