Imported Upstream version 2.5.1
[debian/amanda] / changer-src / chg-mtx.sh.in
1 #!@SHELL@ 
2 #
3 # Exit Status:
4 # 0 Alles Ok
5 # 1 Illegal Request
6 # 2 Fatal Error
7 #
8
9 # try to hit all the possibilities here
10 prefix=@prefix@
11 exec_prefix=@exec_prefix@
12 sbindir=@sbindir@
13 libexecdir=@libexecdir@
14  
15 PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb:/usr/local/bin
16 export PATH
17
18 if [ -d "@AMANDA_DBGDIR@" ]; then
19         logfile=@AMANDA_DBGDIR@/changer.debug
20 else
21         logfile=/dev/null
22 fi
23
24 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
25 if test "$USE_VERSION_SUFFIXES" = "yes"; then
26         SUF="-@VERSION@"
27 else
28         SUF=
29 fi
30
31 myname=$0
32
33 tape=`amgetconf$SUF tapedev`
34 TAPE=`amgetconf$SUF changerdev`; export TAPE # for mtx command
35 if [ "$tape" = "/dev/null" -o "$TAPE" = "/dev/null" ]; then
36   echo "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$SUF 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     code=0
101     echo "Exit -> $answer" >> $logfile
102     echo "$answer"
103     exit $code
104   else
105     answer="<none> $myname: Drive was not loaded"
106     code=1
107     echo "Exit -> $answer" >> $logfile
108     echo "$answer"
109     exit $code
110   fi
111 }
112
113 reset() {
114   readstatus
115   if [ $used -gt 0 ];then
116     $MTX -u $used
117   fi
118   res=`$MTX -l 1`
119   if [ $? -eq 0 ];then
120     answer="1 $tape"
121     code=0
122     echo "Exit -> $answer" >> $logfile
123     echo "$answer"
124     exit $code
125   else
126     answer="1 $res"
127     code=1
128     echo "Exit -> $answer" >> $logfile
129     echo "$answer"
130     exit $code
131   fi
132 }
133 #
134 #
135 loadslot() {
136   readstatus
137   echo "     -> loaded $used" >> $logfile
138   whichslot=$1
139   case $whichslot in
140     current)
141              if [ $used -lt 0 ];then
142                $MTX -l 1
143                used=1
144              fi
145              answer="$used $tape"
146              code=0
147              echo "Exit -> $answer" >> $logfile
148              echo "$answer"
149              exit $code
150              ;;
151     next|advance)
152           load=`expr $used + 1`
153           [ $load -gt $lastslot ] && load=$firstslot
154           ;;
155     prev)
156           load=`expr $used - 1`
157           [ $load -lt $firstslot ] && load=$lastslot
158           ;;
159     first)
160           load=$firstslot
161           ;;
162     last)
163           load=$lastslot
164           ;;
165     [$firstslot-$lastslot])
166           load=$1
167           ;;
168     clean)
169           load=$cleanslot
170           ;;
171     *)
172        answer="<none> $myname: illegal request: \"$whichslot\""
173        code=1
174        echo "Exit -> $answer" >> $logfile
175        echo "$answer"
176        exit $code
177        ;;
178     esac
179
180     if [ $load = $used ]; then
181         answer="$used $tape"
182         code=0
183         echo "Exit -> $answer" >> $logfile
184         echo "$answer"
185         exit $code
186     fi
187
188     if [ $load = $cleanslot ]; then
189         expr $cleancount + 1 > $cleanfile
190         echo 0 > $accessfile
191     else
192         expr $accesscount + 1 > $accessfile
193         if [ $accesscount -gt 9 ]; then
194                 $myname -slot clean >/dev/null
195         fi
196     fi
197
198     # Slot 6 might contain an ordinary tape rather than a cleaning
199     # tape. A cleaning tape auto-ejects; an ordinary tape does not.
200     # We therefore have to read the status again to check what
201     # actually happened.
202     readstatus
203         
204
205     if [ $used -gt 0 ];then
206       echo "     -> unload $used" >> $logfile
207       res=`$MTX -u $used`
208       status=$?
209       echo "     -> status $status" >> $logfile
210       echo "     -> res    $res" >> $logfile
211       if [ $status -ne 0 ];then
212         answer="<none> $myname: $res"
213         code=2
214         echo "Exit -> $answer" >> $logfile
215         echo "$answer"
216         exit $code
217       fi
218     fi
219     if [ $whichslot = advance ];then
220       answer="$load /dev/null"
221       code=0
222       echo "Exit -> $answer" >> $logfile
223       echo "$answer"
224       exit $code
225     fi
226     echo "     -> load   $load" >> $logfile
227     res=`$MTX -l $load`
228     status=$?
229     echo "     -> status $status" >> $logfile
230     echo "     -> res    $res" >> $logfile
231     if [ $status -eq 0 ];then
232       echo "     -> rew $load" >> $logfile
233       $MT $MTF $tape rewind
234       $DD if=$tape bs=32k count=1 >> $logfile 2>&1
235       answer="$load $tape"
236       code=0
237     else
238       answer="$load $res"
239       code=2
240     fi
241     echo "Exit -> $answer" >> $logfile
242     echo "$answer"
243     exit $code
244 }
245 #
246 info() {
247   readstatus
248   echo "     -> info   $used" >> $logfile
249   if [ $used -lt 0 ];then
250     used=0
251   fi
252   answer="$used $lastslot 1"
253   code=0
254   echo "Exit -> $answer" >> $logfile
255   echo "$answer"
256   exit $code
257 }
258 #
259 echo Args "->" "$@" >> $logfile
260 while [ $# -ge 1 ];do
261   case $1 in
262     -slot)
263            shift
264            loadslot $*
265            ;;
266     -info)
267            shift
268            info
269            ;;
270     -reset)
271             shift
272             reset
273             ;;
274     -eject)
275             shift
276             eject
277             ;;
278     *)
279        answer="<none> $myname: Unknown option $1"
280        code=2
281        echo "Exit -> $answer" >> $logfile
282        echo "$answer"
283        exit $code
284        ;;
285   esac
286 done