Imported Upstream version 2.5.2p1
[debian/amanda] / changer-src / chg-zd-mtx.sh.in
1 #!@SHELL@ 
2 #
3 # Exit Status:
4 # 0 Alles Ok
5 # 1 Illegal Request
6 # 2 Fatal Error
7 #
8 # Contributed by Eric DOUTRELEAU <Eric.Doutreleau@int-evry.fr>
9 # This is supposed to work with Zubkoff/Dandelion version of mtx
10 #
11 # Modified by Joe Rhett <jrhett@isite.net>
12 # to work with MTX 1.2.9 by Eric Lee Green http://mtx.sourceforge.net
13 #
14 # Modified by Jason Hollinden <jhollind@sammg.com> on 13-Feb-2001
15 # to work with MTX 1.2.10, >9 slots, has barcode support, and works with
16 # multiple configs at once.
17 # NOTE:  Only tested the 2 additions with an ADIC Scalar 100.
18
19 ################################################################################
20 # Here are the things you need to do and know to configure this script:
21 #
22 #   * Figure out what the robot device name is and what the tape drive
23 #     device name is.  They will be different!
24 #
25 #     You cannot send robot commands to a tape drive and vice versa.
26 #     Both should respond to "mtx -f /dev/... inquiry".  Hopefully,
27 #     that output will make it obvious which is which.
28 #
29 #     For instance, here is what mtx has to say about my current robot:
30 #
31 #       Product Type: Medium Changer
32 #       Vendor ID: 'ATL     '
33 #       Product ID: 'ACL2640 206     '
34 #       Revision: '2A5A'
35 #       Attached Changer: No
36 #
37 #     and here is what it says about a tape drive:
38 #
39 #       Product Type: Tape Drive
40 #       Vendor ID: 'Quantum '
41 #       Product ID: 'DLT4000         '
42 #       Revision: 'CD50'
43 #       Attached Changer: No
44 #
45 #     Note the "Product Type" value makes it clear which is which.
46 #
47 #     If it is not obvious, "mf -f /dev/... rewind" should be happy when
48 #     talking to a (loaded) tape drive but the changer should give some
49 #     kind of error.  Similarly, "mtx -f /dev/... status" should show good
50 #     results with the changer but fail with a tape drive device name.
51 #
52 #     Once you have this figured out, set "changerdev" in amanda.conf
53 #     to the changer device and "tapedev" to the tape device.
54 #
55 #   * Find out what the first and last storage slots are.  Running
56 #     "mtx -f /dev/... status" should give you something like this
57 #     (although the output will vary widely based on the version of mtx
58 #     and the specifics of your robot):
59 #
60 #         Storage Changer /dev/changer:1 Drives, 9 Slots ( 0 Import/Export )
61 #       Data Transfer Element 0:Empty
62 #             Storage Element 1:Full :VolumeTag=SR0001
63 #             Storage Element 2:Full :VolumeTag=SR0002
64 #             Storage Element 3:Full :VolumeTag=SR0003
65 #             Storage Element 4:Full :VolumeTag=SR0004
66 #             Storage Element 5:Full :VolumeTag=SR0005
67 #             Storage Element 6:Full :VolumeTag=SR0006
68 #             Storage Element 7:Full :VolumeTag=SR0007
69 #             Storage Element 8:Full :VolumeTag=SR0008
70 #             Storage Element 9:Full :VolumeTag=SR0009
71 #             Storage Element 10 IMPORT/EXPORT:Full :VolumeTag=SR0009
72 #
73 #     This says the first storage slot (element) is "1" and the last
74 #     is "9".  If you allocate the entire robot to Amanda, you do not need
75 #     to set the "firstslot" or "lastslot" configuration file variables --
76 #     the script will compute these values for you.
77 #
78 #     You do not have to allocate all of the slots for Amanda use,
79 #     but whatever slots you use must be contiguous (i.e. 4 through 9
80 #     in the above would be OK but 1, 2, 5, 6, 9 would not).  The one
81 #     exception to this is that if one of the slots contains a cleaning
82 #     cartridge, it may be in any slot (Amanda will just skip over it if
83 #     it is between firstslot and lastslot).
84 #
85 #   * Speaking of cleaning cartridges, if you have a storage slot dedicated
86 #     to one, figure out what slot it is in.  That slot number will go in
87 #     the "cleanslot" variable.
88 #
89 #     Also, decide if you want the changer script to automatically run
90 #     the cleaning tape through the drive after every so many mounts,
91 #     and how many mounts you want to do between cleanings.  If you
92 #     want the script to do this, set the "autoclean" variable to 1 and
93 #     the "autocleancount" to the number of mounts between cleanings.
94 #     If you do not want to do automatic cleanings (including not having
95 #     a cleaning cartridge in the robot), set "autoclean" to 0.
96 #
97 #     Note that only a count of mounts is used to determine when it is
98 #     time to clean.  The script does not try to detect if the drive is
99 #     requesting cleaning, or how much the drive was used on a given
100 #     mount.
101 #
102 #   * If you tell Amanda about a cleaning cartridge, whether for automatic
103 #     operation or manual (amtape <config> clean), you must also tell
104 #     the script how long it takes to run the cleaning cycle.  It is
105 #     impossible for the script to determine when the cleaning operation
106 #     is done, so the "cleancycle" variable is the number of seconds
107 #     the longest cleaning operation takes (you'll just have to figure
108 #     this out by watching it a few times, or maybe finding it in a tape
109 #     drive hardware manual).  The script will sleep for this length of
110 #     time whenever the cleaning tape is referenced.  The default is 120
111 #     seconds (two minutes).
112 #
113 #   * Figure out the drive slot number.  By default, it is set to 0.
114 #     In the example above, the tape drive ("Data Transfer Element")
115 #     is in slot 0. If your drive slot is not 0, you
116 #     need to set the drive slot number with the "driveslot" variable.
117 #
118 #   * Figure out whether your robot has a barcode reader and whether
119 #     your version of mtx supports it.  If you see "VolumeTag" entries
120 #     in the "mtx -f /dev/xxx status" output you did above, you have
121 #     a reader and mtx can work with it, so you may set the "havereader"
122 #     variable to 1.  The default is 0 (do not use a reader).
123 #
124 #   * Pick any tape to load and then determine if the robot can put it
125 #     away directly or whether an "offline" must be done first.
126 #
127 #     With the tape still mounted and ready, try to put the tape away
128 #     with "mtx".  If you get some kind of error, which is the most
129 #     common response, try "mt -f /dev/... offline", wait for the drive
130 #     to unload and make sure the robot takes no action on its own to
131 #     store the tape.  Assuming it does not, try the "mtx" command again
132 #     to store the tape.
133 #
134 #     If you had to issue the "mt -f /dev/... offline" before you could
135 #     use "mtx" to store the tape, set the "offline_before_unload"
136 #     variable to 1.  If "mtx" unloaded the drive and put the tape away
137 #     all by itself, set it to 0.
138 #
139 #   * Some drives and robots require a small delay between unloading the
140 #     tape and instructing the robot to move it back to storage.
141 #     For instance, if you try to grab the tape too soon on an ATL robot
142 #     with DLT tape drives, it will rip the leader out of the drive and
143 #     require sincerely painful hardware maintenance.
144 #
145 #     If you need a little delay, set the "unloadpause" variable to
146 #     the number of seconds to wait before trying to take a tape from
147 #     a drive back to storage.  The default is 0.
148 #
149 #   * Some drives also require a short pause after loading, or the drive
150 #     will return an I/O error during a test to see if it's online (which
151 #     this script uses "mt rewind" to test).  My drives don't recover from
152 #     this, and must be reloaded before they will come online after failing
153 #     such a test.  For this reason there is an "initial_poll_delay"
154 #     variable which will pause for a certain number of seconds before
155 #     looping through the online test for the first time.  The default is 0.
156 ####
157
158 ####
159 # Now you are ready to set up the variables in the changer configuration
160 # file.
161 #
162 # All variables are in "changerfile".conf where "changerfile" is set
163 # in amanda.conf.  For example, if amanda.conf has:
164 #
165 #       changerfile="/etc/amanda/Dailyset1/CHANGER"
166 #    or changerfile="/etc/amanda/Dailyset1/CHANGER.conf"
167 #
168 # the variables must be in "/etc/amanda/Dailyset1/CHANGER.conf".
169 # The ".conf" is appended only if it's not there".
170 #
171 # If "changerfile" is a relative path, it is relative to the directory
172 # that contains amanda.conf.  That also happens to be the directory Amanda
173 # makes current before running this script.
174 #
175 # Here is a commented out example file with all the variables and showing
176 # their default value (if any):
177 ####
178 # firstslot=?               #### First storage slot (element) -- required
179 # lastslot=?                #### Last storage slot (element) -- required
180 # cleanslot=-1              #### Slot with cleaner tape -- default is "-1"
181 #                           #### Set negative to indicate no cleaner available
182 # driveslot=0               #### Drive slot number.  Defaults to 0
183 #                           #### Use the 'Data Transfer Element' you want
184 #
185 #   # Do you want to clean the drive after a certain number of accesses?
186 #   # NOTE - This is unreliable, since 'accesses' aren't 'uses', and we
187 #   #        have no reliable way to count this.  A single amcheck could
188 #   #        generate as many accesses as slots you have, plus 1.
189 #   # ALSO NOTE - many modern tape loaders handle this automatically.
190 #
191 # autoclean=0               #### Set to '1' or greater to enable
192 #
193 # autocleancount=99         #### Number of access before a clean.
194 #
195 # havereader=0              #### If you have a barcode reader, set to 1.
196 #
197 # offline_before_unload=0   #### Does your robot require an
198 #                           #### 'mt offline' before mtx unload?
199 #
200 # poll_drive_ready=NN       #### Time (seconds) between tests to see if
201 #                           #### the tape drive has gone ready (default: 3).
202 #
203 # max_drive_wait=NN         #### Maximum time (seconds) to wait for the
204 #                           #### tape drive to become ready (default: 120).
205 #
206 # initial_poll_delay=NN     #### initial delay after load before polling for
207 #                           #### readiness
208 #
209 ####
210
211 ####
212 # Now it is time to test the setup.  Do all of the following in the
213 # directory that contains the amanda.conf file, and do all of it as
214 # the Amanda user.
215 #
216 #   * Run this:
217 #
218 #       .../chg-zd-mtx -info
219 #       echo $?             #### (or "echo $status" if you use csh/tcsh)
220 #
221 #     You should get a single line from the script like this (the actual
222 #     numbers will vary):
223 #
224 #       5 9 1 1
225 #
226 #     The first number (5) is the "current" slot.  This may or may not be
227 #     the slot actually loaded at the moment (if any).  It is the slot
228 #     Amanda will try to use next.
229 #
230 #     The second number (9) is the number of slots.
231 #
232 #     The third number will always be "1" and indicates the changer is
233 #     capable of going backward.
234 #
235 #     The fourth number is optional.  If you set $havereader to 1, it
236 #     will be "1", otherwise it will not be present.
237 #
238 #     The exit code ($? or $status) should be zero.
239 #
240 #   * Run this:
241 #
242 #       .../chg-zd-mtx -reset
243 #       echo $?
244 #
245 #     The script should output a line like this:
246 #
247 #       1 /dev/rmt/0mn
248 #
249 #     The number at the first should match $firstslot.  The device name
250 #     after that should be your tape device.
251 #
252 #     The exit code ($? or $status) should be zero.
253 #
254 #   * Run this:
255 #
256 #       .../chg-zd-mtx -slot next
257 #       echo $?
258 #
259 #     The script should output a line like this:
260 #
261 #       2 /dev/rmt/0mn
262 #
263 #     The number at the first should be one higher than $firstslot.
264 #     The device name after that should be your tape device.
265 #
266 #     The exit code ($? or $status) should be zero.
267 #
268 #   * Run this:
269 #
270 #       .../chg-zd-mtx -slot current
271 #       echo $?
272 #
273 #     Assuming the tape is still loaded from the previous test, the
274 #     robot should not move and the script should report the same thing
275 #     the previous command did.
276 #
277 #   * If you continue to run "-slot next" commands, the robot should load
278 #     each tape in turn then wrap back around to the first when it
279 #     reaches $lasttape.  If $cleanslot is within the $firstslot to
280 #     $lastslot range, the script will skip over that entry.
281 #
282 #   * Finally, try some of the amtape commands and make sure they work:
283 #
284 #       amtape <config> reset
285 #       amtape <config> slot next
286 #       amtape <config> slot current
287 #
288 #   * If you set $havereader non-zero, now would be a good time to create
289 #     the initial barcode database:
290 #
291 #       amtape <config> update
292 ####
293
294 ################################################################################
295 # To debug this script, first look in @AMANDA_DBGDIR@.  The script
296 # uses one of two log files there, depending on what version of Amanda
297 # is calling it.  It may be chg-zd-mtx.YYYYMMDD*.debug, or it may be
298 # changer.debug.driveN where 'N' is the drive number.
299 #
300 # If the log file does not help, try running the script, **as the Amanda
301 # user**, in the amanda.conf directory with whatever set of args the log
302 # said were used when you had a problem.  If nothing else useful shows up
303 # in the output, try running the script with the DEBUG environment variable
304 # set non-null, e.g.:
305 #
306 #       env DEBUG=yes .../chg-zd-mtx ...
307 ################################################################################
308
309 # source utility functions and values from configure
310 exec_prefix=@exec_prefix@
311 libexecdir=@libexecdir@
312 . ${libexecdir}/chg-lib.sh
313
314 test -n "$DEBUG" && set -x
315 TMPDIR="@AMANDA_TMPDIR@"
316 DBGDIR="@AMANDA_DBGDIR@"
317
318 argv0=$0
319 myname=`expr "$argv0" : '.*/\(.*\)'`
320
321 config=`pwd 2>/dev/null`
322 config=`expr "$config" : '.*/\(.*\)'`
323
324 ###
325 # Functions to write a new log file entry and append more log information.
326 ###
327
328 ds=`date '+%H:%M:%S' 2>/dev/null`
329 if [ $? -eq 0  -a  -n "$ds" ]; then
330         logprefix=`echo "$ds" | sed 's/./ /g'`
331 else
332         logprefix=""
333 fi
334
335 LogAppend() {
336         if [ -z "$logprefix" ]; then
337                 echo "$@" >> $DBGFILE
338         else
339                 echo "$logprefix" "$@" >> $DBGFILE
340         fi
341 }
342
343 Log() {
344         if [ -z "$logprefix" ]; then
345                 echo "===" "`date`" "===" >> $DBGFILE
346                 echo "$@" >> $DBGFILE
347         else
348                 ds=`date '+%H:%M:%S' 2>/dev/null`
349                 echo "$ds" "$@" >> $DBGFILE
350         fi
351 }
352
353 ###
354 # Common exit function.
355 #
356 #   $1 = exit code
357 #   $2 = slot result
358 #   $3 = additional information (error message, tape devive, etc)
359 ###
360
361 internal_call=0
362 Exit() {
363         if [ $internal_call -gt 0 ]; then
364                 call_type=Return
365         else
366                 call_type=Exit
367         fi
368         code=$1
369         shift
370         exit_slot=$1
371         shift
372         exit_answer="$@"
373         Log $call_type "($code) -> $exit_slot $@"
374         echo "$exit_slot" "$@"
375         if [ $call_type = Return ]; then
376                 return $code
377         fi
378         amgetconf$SUF dbclose.$argv0:$DBGFILE > /dev/null 2>&1
379         exit $code
380 }
381
382 ###
383 # Function to run another command and log it.
384 ###
385
386 Run() {
387         Log `_ 'Running: %s' "$@"`
388         rm -f $stdout $stderr
389         "$@" > $stdout 2> $stderr
390         exitcode=$?
391         Log `_ 'Exit code: %s' "$exitcode"`
392         if [ -s $stdout ]
393         then
394                 LogAppend Stdout:
395                 cat $stdout >> $DBGFILE
396         fi
397         if [ -s $stderr ]
398         then
399                 LogAppend Stderr:
400                 cat $stderr >> $DBGFILE
401         fi
402         cat $stdout
403         cat $stderr 1>&2
404         return $exitcode
405 }
406
407 ###
408 # Return success if the arg is numeric.
409 ###
410
411 IsNumeric() {
412         test -z "$1" && return 1
413         x="`expr "$1" : '\([-0-9][0-9]*\)' 2>/dev/null`"
414         return `expr X"$1" != X"$x"`
415 }
416
417 ###
418 # Run $MTX status unless the previous output is still valid.
419 ###
420
421 mtx_status_valid=0
422 get_mtx_status() {
423         test -n "$DEBUG" && set -x
424         if [ $mtx_status_valid -ne 0 ]; then
425                 return 0
426         fi
427         rm -f $mtx_status
428         Run $MTX status > $mtx_status 2>&1
429         status=$?
430         if [ $status -eq 0 ]; then
431                 mtx_status_valid=1
432         fi
433         return $status
434 }
435
436 ###
437 # Determine the slot currently loaded.  Set $loadedslot to the slot
438 # currently loaded, or "-1", and $loadedbarcode to the corresponding
439 # barcode (or nothing).
440 ###
441
442 get_loaded_info() {
443         test -n "$DEBUG" && set -x
444         get_mtx_status
445
446         set x `sed -n '
447 /^Data Transfer Element:Empty/                          {
448     s/.*/-1/p
449     q
450 }
451 /^Data Transfer Element '$driveslot':Empty/             {
452     s/.*/-1/p
453     q
454 }
455 /^Data Transfer Element:Full (Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^     ]*\)/               {
456     s/.*(Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^     ]*\)/\1 \2/p
457     q
458 }
459 /^Data Transfer Element '$driveslot':Full (Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^     ]*\)/  {
460     s/.*(Storage Element \([0-9][0-9]*\) Loaded):VolumeTag *= *\([^     ]*\)/\1 \2/p
461     q
462 }
463 /^Data Transfer Element '$driveslot':Full (Unknown Storage Element Loaded):VolumeTag *= *\([^     ]*\)/ {
464     s/.*:VolumeTag *= *\([^     ]*\)/-2 \1/p
465     q
466 }
467 /^Data Transfer Element:Full (Storage Element \([0-9][0-9]*\) Loaded)/                           {
468     s/.*(Storage Element \([0-9][0-9]*\) Loaded).*/\1/p
469     q
470 }
471 /^Data Transfer Element '$driveslot':Full (Storage Element \([0-9][0-9]*\) Loaded)/              {
472     s/.*Storage Element \([0-9][0-9]*\) Loaded.*/\1/p
473     q
474 }
475 /^Data Transfer Element '$driveslot':Full (Unknown Storage Element Loaded)/     {
476     s/.*/-2/p
477     q
478 }
479 ' < $mtx_status 2>&1`
480         shift                                   # get rid of the "x"
481         loadedslot=$1
482         loadedbarcode=$2
483         if [ -z "$loadedslot" ]; then
484                 Exit 2 \
485                     `_ '<none>'` \
486                     "could not determine current slot, are you sure your drive slot is $driveslot"
487                 return $?                       # in case we are internal
488         fi
489
490         #Use the current slot if it's empty and we don't know which slot is loaded'
491         if [ $loadedslot -eq -2 ]; then
492                 set x `sed -n '
493 {
494     /^.*Storage Element '$currentslot':Empty/ {
495         s/.*Storage Element \([0-9][0-9]*\):Empty/\1/p
496         q
497     }
498     /^.*Storage Element '$currentslot':Full/ {
499         s/.*Storage Element \([0-9][0-9]*\):Full/-2/p
500         q
501     }
502     /^.*Storage Element '$currentslot' IMPORT\/EXPORT:Empty/ {
503         s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Empty/\1/p
504         q
505     }
506     /^.*Storage Element '$currentslot' IMPORT\/EXPORT:Full/ {
507         s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Full/-2/p
508         q
509     }
510 }
511 ' < $mtx_status 2>& 1`
512                 shift                           # get rid of the "x"
513                 loadedslotx=$1
514                 if [ ! -z $loadedslotx ]; then
515                         loadedslot=$loadedslotx
516                 fi
517         fi
518
519         #Use the first empty slot if we don't know which slot is loaded'
520         if [ $loadedslot -eq -2 ]; then
521                 set x `sed -n '
522 {
523     /^.*Storage Element \([0-9][0-9]*\):Empty/ {
524         s/.*Storage Element \([0-9][0-9]*\):Empty/\1/p
525         q
526     }
527     /^.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Empty/ {
528         s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Empty/\1/p
529         q
530     }
531 }
532 ' < $mtx_status 2>& 1`
533                 shift                           # get rid of the "x"
534                 loadedslot=$1
535         fi
536
537         if IsNumeric "$loadedslot" ; then
538                 :
539         else
540                 Exit 2 \
541                      `_ '<none>'` \
542                      "currently loaded slot ($loadedslot) not numeric"
543                 return $?                       # in case we are internal
544         fi
545         Log       `_ 'STATUS   -> currently loaded slot = %s' "$loadedslot"`
546         LogAppend `_ '         -> currently loaded barcode = "%s"' "$loadedbarcode"`
547 }
548
549 ###
550 # Get a list of slots between $firstslot and $lastslot, if they are set.
551 # If they are not set, set them to the first and last slot seen on the
552 # assumption the entire robot is to be used (???).
553 ###
554
555 slot_list=
556 get_slot_list() {
557         test -n "$DEBUG" && set -x
558         if [ -n "$slot_list" ]; then
559                 return
560         fi
561         get_mtx_status
562         slot_list=`sed -n '
563 /^Data Transfer Element:Full (Storage Element \([0-9][0-9]*\) Loaded)/ {
564     s/.*(Storage Element \([0-9][0-9]*\) Loaded).*/\1/p
565 }
566 /^Data Transfer Element '$driveslot':Full (Storage Element \([0-9][0-9]*\) Loaded)/ {
567     s/.*Storage Element \([0-9][0-9]*\) Loaded.*/\1/p
568 }
569 /^Data Transfer Element '$driveslot':Full (Unknown Storage Element Loaded)/ {
570     : loop
571     n
572     /^.*Storage Element \([0-9][0-9]*\):Full/ {
573         s/.*Storage Element \([0-9][0-9]*\):Full.*/\1/p
574         b loop
575     }
576     /^.*Storage Element \([0-9][0-9]*\):Empty/ {
577         s/.*Storage Element \([0-9][0-9]*\):Empty/\1/p
578     }
579 }
580 /^.*Storage Element \([0-9][0-9]*\):Full/ {
581     s/.*Storage Element \([0-9][0-9]*\):Full.*/\1/p
582 }
583 /^.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Full/ {
584     s/.*Storage Element \([0-9][0-9]*\) IMPORT\/EXPORT:Full.*/\1/p
585 }
586 ' < $mtx_status 2>&1 | grep -v "^${cleanslot}\$" | sort -n`
587         slot_list=`echo $slot_list`             # remove the newlines
588         if [ $firstslot -lt 0 -o $lastslot -lt 0 ]; then
589                 last=$lastslot
590                 for slot in $slot_list; do
591                         if [ $firstslot -lt 0 ]; then
592                                 Log `_ 'SLOTLIST -> firstslot set to %s' "$slot"`
593                                 firstslot=$slot
594                         fi
595                         if [ $lastslot -lt 0 ]; then
596                                 last=$slot
597                         fi
598                 done
599                 if [ $lastslot -lt 0 -a $last -ge 0 ]; then
600                         Log `_ 'SLOTLIST -> lastslot set to %s' "$last"`
601                         lastslot=$last
602                 fi
603                 if [ $firstslot -lt 0 ]; then
604                         Exit 2 \
605                              `_ '<none>'` \
606                              `_ 'cannot determine first slot'`
607                         return $?               # in case we are internal
608                 elif [ $lastslot -lt 0 ]; then
609                         Exit 2 \
610                              `_ '<none>'` \
611                              `_ 'cannot determine last slot'`
612                         return $?               # in case we are internal
613                 fi
614         fi
615         amanda_slot_list=
616         for slot in $slot_list; do
617                 if [ $slot -ge $firstslot -a $slot -le $lastslot ]; then
618                         amanda_slot_list="$amanda_slot_list $slot"
619                 fi
620         done
621         if [ -z "$amanda_slot_list" ]; then
622                 Exit 2 \
623                      `_ '<none>'` \
624                      "no slots available"
625                 return $?                       # in case we are internal
626         fi
627         slot_list="$amanda_slot_list"
628 }
629
630 # Paths
631 prefix=@prefix@
632 exec_prefix=@exec_prefix@
633 sbindir=@sbindir@
634 libexecdir=@libexecdir@
635
636 # try to hit all the possibilities here
637 PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb:/usr/local/bin
638 export PATH
639
640 USE_VERSION_SUFFIXES="@USE_VERSION_SUFFIXES@"
641 if test "$USE_VERSION_SUFFIXES" = "yes"; then
642         SUF="-@VERSION@"
643 else
644         SUF=
645 fi
646
647 DBGFILE=`amgetconf$SUF dbopen.$argv0 2>/dev/null`
648 if [ -z "$DBGFILE" ]
649 then
650         DBGFILE=/dev/null                       # will try this again below
651 fi
652
653 changerfile=`amgetconf$SUF changerfile 2>/dev/null`
654 if [ -z "$changerfile" ]; then
655         Exit 2 \
656              `_ '<none>'` \
657              "changerfile must be specified in amanda.conf"
658 fi
659
660 tape=`amgetconf$SUF tapedev 2>/dev/null`
661 if [ -z "$tape" ]; then
662         Exit 2 \
663              `_ '<none>'` \
664              "tapedev may not be empty"
665 elif [ $tape = "/dev/null" -o `expr "$tape" : 'null:'` -eq 5 ]; then
666         Exit 2 \
667              `_ '<none>'` \
668              "tapedev ($tape) may not be the null device"
669 fi
670 TAPE=`amgetconf$SUF changerdev 2>/dev/null`
671 if [ -z "$TAPE" ]; then
672         Exit 2 \
673              `_ '<none>'` \
674              "changerdev may not be empty"
675 elif [ $TAPE = "/dev/null" ]; then
676         Exit 2 \
677              `_ '<none>'` \
678              "changerdev ($TAPE) may not be the null device"
679 fi
680 export TAPE                                     # for mtx command
681
682 CHANGER=$TAPE
683 export CHANGER                                  # for mtx command
684
685 #### Set up the various config files.
686
687 conf_match=`expr "$changerfile" : .\*\.conf\$`
688 if [ $conf_match -ge 6 ]; then
689         configfile=$changerfile
690         changerfile=`echo $changerfile | sed 's/.conf$//g'`
691 else
692         configfile=$changerfile.conf
693 fi
694
695 if [ ! -e $configfile ]; then
696         Exit 2 \
697              `_ '<none>'` \
698              "configuration file \"$configfile\" doesn't exist"
699 fi
700 if [ ! -f $configfile ]; then
701         Exit 2 \
702              `_ '<none>'` \
703              "configuration file \"$configfile\" is not a file"
704 fi
705
706 cleanfile=$changerfile-clean
707 accessfile=$changerfile-access
708 slotfile=$changerfile-slot
709 labelfile=$changerfile-barcodes
710 [ ! -s $cleanfile ] && echo 0 > $cleanfile
711 [ ! -s $accessfile ] && echo 0 > $accessfile
712 [ ! -s $slotfile ] && echo -1 > $slotfile
713 [ ! -f $labelfile ] && > $labelfile
714 cleancount=`cat $cleanfile`
715 accesscount=`cat $accessfile`
716
717 test -z "$MT" && Exit 2 "<none>" "No mt command defined"
718 test ! -f "$MT" && Exit 2 "<none>" "mt command ($MT) doesn't exist"
719 test -z "$MTX" && Exit 2 "<none>" "No mtx command defined"
720 test ! -f "$MTX" && Exit 2 "<none>" "mtx command ($MTX) doesn't exist"
721
722 #### Dig out of the config file what is needed
723
724 varlist=
725 varlist="$varlist firstslot"
726 varlist="$varlist lastslot"
727 varlist="$varlist cleanslot"
728 varlist="$varlist cleancycle"
729 varlist="$varlist OFFLINE_BEFORE_UNLOAD"        # old name
730 varlist="$varlist offline_before_unload"
731 varlist="$varlist unloadpause"
732 varlist="$varlist AUTOCLEAN"                    # old name
733 varlist="$varlist autoclean"
734 varlist="$varlist autocleancount"
735 varlist="$varlist havereader"
736 varlist="$varlist driveslot"
737 varlist="$varlist poll_drive_ready"
738 varlist="$varlist initial_poll_delay"
739 varlist="$varlist max_drive_wait"
740
741 for var in $varlist
742 do
743         val="`cat $configfile 2>/dev/null | sed -n '
744 # Ignore comment lines (anything starting with a #).
745 /^[     ]*#/d
746 # Find the first var=val line in the file, print the value and quit.
747 /^[     ]*'$var'[       ]*=[    ]*\([^  ][^     ]*\).*/ {
748         s/^[    ]*'$var'[       ]*=[    ]*\([^  ][^     ]*\).*/\1/p
749         q
750 }
751 '`"
752         eval $var=\"$val\"
753 done
754
755 # Deal with driveslot first so we can get DBGFILE set if we are still
756 # using the old amgetconf.
757
758 if [ -z "$driveslot" ]; then
759         driveslot=0;
760 fi
761
762 # Get DBGFILE set if it is not already.
763
764 if [ $DBGFILE = /dev/null ]; then
765         if [ -d "$DBGDIR" ]; then
766                 DBGFILE=$DBGDIR/changer.debug.drive$driveslot
767         else
768                 DBGFILE=/dev/null
769         fi
770         Log `_ '=== Start %s ===' "\`date\`"`
771 fi
772 if [ -z "$driveslot" ]; then
773         Exit 2 \
774              `_ '<none>'` \
775              `_ 'cannot determine drive slot from %s' "$tape"`
776 fi
777
778 stdout=$TMPDIR/$myname.1.$$
779 stderr=$TMPDIR/$myname.2.$$
780 mtx_status=$TMPDIR/$myname.status.$$
781 trap "rm -f $stdout $stderr $mtx_status" 0      # exit cleanup
782
783 Log `_ 'Using config file %s' "$configfile"`
784
785 # Log the argument list.
786
787 Log `_ "Arg info:"`
788 LogAppend "\$# = $#"
789 i=0
790 LogAppend "\$$i = \"$argv0\""
791 for arg in "$@"; do
792         i=`expr $i + 1`
793         LogAppend "\$$i = \"$arg\""
794 done
795
796 # Set the default config values for those not in the file.  Log the
797 # results and make sure each is valid (numeric).
798
799 firstslot=${firstslot:-'-1'}                            # default: mtx status
800 lastslot=${lastslot:-'-1'}                              # default: mtx status
801 cleanslot=${cleanslot:-'-1'}                            # default: -1
802 cleancycle=${cleancycle:-'120'}                         # default: two minutes
803 if [ -z "$offline_before_unload" -a -n "$OFFLINE_BEFORE_UNLOAD" ]; then
804         offline_before_unload=$OFFLINE_BEFORE_UNLOAD    # (old name)
805 fi
806 offline_before_unload=${offline_before_unload:-'0'}     # default: 0
807 unloadpause=${unloadpause:-'0'}                         # default: 0
808 if [ -z "$autoclean" -a -n "$AUTOCLEAN" ]; then
809         autoclean=$AUTOCLEAN                            # (old name)
810 fi
811 autoclean=${autoclean:-'0'}                             # default: 0
812 autocleancount=${autocleancount:-'99'}                  # default: 99
813 havereader=${havereader:-'0'}                           # default: 0
814 poll_drive_ready=${poll_drive_ready:-'3'}               # default: three seconds
815 initial_poll_delay=${initial_poll_delay:-'0'}           # default: zero zeconds
816 max_drive_wait=${max_drive_wait:-'120'}                 # default: two minutes
817
818 get_slot_list
819
820 Log `_ "Config info:"`
821 for var in $varlist; do
822         if [ $var = "OFFLINE_BEFORE_UNLOAD" ]; then
823                 continue                        # old name
824         elif [ $var = "AUTOCLEAN" ]; then
825                 continue                        # old name
826         fi
827         eval val=\"'$'$var\"
828         if [ -z "$val" ]; then
829                 Exit 2 \
830                      `_ '<none>'` \
831                      `_ '%s missing in %s' "$var" "$configfile"`
832         fi
833         if IsNumeric "$val" ; then
834                 :
835         else
836                 Exit 2 \
837                      `_ '<none>'` \
838                      `_ '%s (%s) not numeric in %s' "$var" "$val" "$configfile"`
839         fi
840         LogAppend $var = \"$val\"
841 done
842
843 # Run the rest of the config file sanity checks.
844
845 if [ $firstslot -gt $lastslot ]; then
846         Exit 2 \
847              `_ '<none>'` \
848              `_ 'firstslot (%s) greater than lastslot (%s) in %s' "$firstslot" "$lastslot" "$configfile"`
849 fi
850 if [ $autoclean -ne 0 -a $cleanslot -lt 0 ]; then
851         Exit 2 \
852              `_ '<none>'` \
853              `_ 'autoclean set but cleanslot not valid (%s)' "$cleanslot"`
854 fi
855
856 # Set up the current slot
857
858 currentslot=`cat $slotfile`
859 if IsNumeric "$currentslot" ; then
860         if [ $currentslot -lt $firstslot ]; then
861                 Log `_ 'SETUP    -> current slot %s less than %s ... resetting to %s' "$currentslot" "$firstslot" "$firstslot"`
862                 currentslot=$firstslot
863         elif [ $currentslot -gt $lastslot ]; then
864                 Log `_ 'SETUP    -> current slot %s greater than %s ... resetting to %s' "$currentslot" "$lastslot" "$lastslot"`
865                 currentslot=$lastslot
866         fi
867 else
868         Log `_ 'SETUP    -> contents of %s (%s) invalid, setting current slot to first slot (%s)' "$slotfile" "$currentslot" "$firstslot"`
869         currentslot=$firstslot
870 fi
871
872 found_current=0
873 first_slot_in_list=-1
874 next_slot_after_current=-1
875 for slot in $slot_list; do
876         if [ $first_slot_in_list -lt 0 ]; then
877                 first_slot_in_list=$slot        # in case $firstslot is missing
878         fi
879         if [ $slot -eq $currentslot ]; then
880                 found_current=1
881                 break
882         elif [ $slot -gt $currentslot ]; then
883                 next_slot_after_current=$slot   # $currentslot is missing
884                 break
885         fi
886 done
887 if [ $found_current -eq 0 ]; then
888         if [ $next_slot_after_current -lt 0 ]; then
889                 new_currentslot=$first_slot_in_list
890         else
891                 new_currentslot=$next_slot_after_current
892         fi
893         Log `_ 'WARNING  -> current slot %s not available, setting current slot to next slot (%s)' "$currentslot" "$new_currentslot"`
894         currentslot=$new_currentslot
895 fi
896
897 # More routines.
898
899 ###
900 # Eject the current tape and put it away.
901 ###
902
903 eject() {
904         test -n "$DEBUG" && set -x
905         Log `_ 'EJECT    -> ejecting tape from %s' "$tape"`
906         get_loaded_info 
907         if [ $loadedslot -gt 0 ]; then
908                 Log `_ 'EJECT    -> moving tape from drive %s to storage slot %s' "$driveslot" "$loadedslot"`
909                 if [ $offline_before_unload -ne 0 ]; then
910                         Run $MT $MTF $tape offline > /dev/null 2>&1
911                 fi
912                 sleep $unloadpause
913                 result=`Run $MTX unload $loadedslot $driveslot 2>&1`
914                 status=$?
915                 Log `_ '         -> status %s, result "%s"' "$status" "$result"`
916                 mtx_status_valid=0
917                 if [ $status -ne 0 ]; then
918                         answer="$result"
919                         code=2
920                 else
921                         answer="$tape"
922                         code=0
923                 fi
924         else
925                 answer=`_ 'Drive was not loaded'`
926                 code=1
927         fi
928         Exit $code "$loadedslot" "$answer"
929         return $?                               # in case we are internal
930 }
931
932 ###
933 # Reset the robot back to the first slot.
934 ###
935
936 reset() {
937         test -n "$DEBUG" && set -x
938         Log `_ 'RESET    -> loading tape from slot %s to drive %s (%s)' "$firstslot" "$driveslot" "$tape"`
939         # Call loadslot without doing it as an internal and let it finish
940         # things up.
941         loadslot $firstslot
942         # NOTREACHED
943         Exit 2 `_ '<none>'` `_ 'reset: should not get here'`
944         return $?                               # in case we are internal
945 }
946
947 ###
948 # Unload the current tape (if necessary) and load a new one (unless
949 # "advance").  If no tape is loaded, get the value of "current" from
950 # $slotfile.
951 ###
952
953 loadslot() {
954         test -n "$DEBUG" && set -x
955         if [ $# -lt 1 ]; then
956                 Exit 2 `_ '<none>'` `_ 'Missing -slot argument'`
957                 return $?                       # in case we are internal
958         fi
959         whichslot=$1
960         Log `_ 'LOADSLOT -> load drive %s (%s) from slot %s' "$driveslot" "$tape" "$whichslot"`
961
962         numeric=`echo $whichslot | sed 's/[^0-9]//g'`
963         case $whichslot in
964         current|prev|next|advance)
965                 find_slot=$currentslot
966                 ;;
967         first)
968                 find_slot=$firstslot
969                 ;;
970         last)
971                 find_slot=$lastslot
972                 ;;
973         $numeric)
974                 find_slot=$numeric
975                 ;;
976         clean)
977                 find_slot=$cleanslot
978                 ;;
979         *)
980                 Exit 2 `_ '<none>'` `_ 'Illegal slot: "%s"' "$whichslot"`
981                 return $?                       # in case we are internal
982                 ;;
983         esac
984
985         # Find the requested slot in the slot list.  By loading the "set"
986         # command with multiple copies, we guarantee that if the slot is
987         # found, we can look both forward and backward without running
988         # off the end.  Putting $cleanslot at the end allows us to find
989         # that slot since it is not in $slot_list.
990         get_slot_list
991         set x $slot_list $slot_list $slot_list $cleanslot
992         shift                                   # get rid of the "x"
993         prev_slot=$1
994         shift
995         while [ $# -gt 0 ]; do
996                 if [ $1 -eq $find_slot ]; then
997                         break
998                 fi
999                 prev_slot=$1
1000                 shift
1001         done
1002         if [ $# -le 0 ]; then
1003                 Exit 2 \
1004                      `_ '<none>'` \
1005                      `_ 'Cannot find slot %s in slot list (%s)' "$find_slot " "$slot_list"`
1006                 return $?                       # in case we are internal
1007         fi
1008
1009         # Determine the slot to load.
1010         case $whichslot in
1011         next|advance)
1012                 shift
1013                 loadslot=$1
1014                 ;;
1015         prev)
1016                 loadslot=$prev_slot
1017                 ;;
1018         *)
1019                 loadslot=$find_slot
1020         esac
1021
1022         # If the desired slot is already loaded, we are done.  Only update
1023         # current slot if this is not the cleaning slot.
1024         get_loaded_info
1025         if [ $loadslot = $loadedslot ]; then
1026                 if [ $loadslot -ne $cleanslot ]; then
1027                         rm -f $slotfile
1028                         echo $loadslot > $slotfile
1029                 fi
1030                 Exit 0 "$loadedslot" "$tape"
1031                 return $?                       # in case we are internal
1032         fi
1033         if [ $loadedslot -eq -2 ]; then
1034                 Exit 0 "$loadedslot" "$tape"
1035                 return $?                       # in case we are internal
1036         fi
1037
1038         # If we are loading the cleaning tape, bump the cleaning count
1039         # and reset the access count.  Otherwise, bump the access count
1040         # and see if it is time to do a cleaning.
1041         if [ $loadslot = $cleanslot ]; then
1042                 rm -f $cleanfile $accessfile
1043                 expr $cleancount + 1 > $cleanfile
1044                 echo 0 > $accessfile
1045         else
1046                 rm -f $accessfile
1047                 expr $accesscount + 1 > $accessfile
1048                 if [ $autoclean -ne 0 -a $accesscount -gt $autocleancount ]
1049                 then
1050                         internal_call=`expr $internal_call + 1`
1051                         loadslot clean > /dev/null 2>&1
1052                         status=$?
1053                         internal_call=`expr $internal_call - 1`
1054                         if [ $status -ne 0 ]; then
1055                                 Exit $status "$loadslot" "$exit_answer"
1056                                 return $?       # in case we are internal
1057                         fi
1058
1059                         # Slot $cleanslot might contain an ordinary tape
1060                         # rather than a cleaning tape.  A cleaning tape
1061                         # *MIGHT* auto-eject; an ordinary tape does not.
1062                         # We therefore have to read the status again to
1063                         # check what actually happened.
1064                         mtx_status_valid=0
1065                         get_loaded_info
1066                 fi
1067         fi
1068
1069         # Unload whatever tape is in the drive.
1070         internal_call=`expr $internal_call + 1`
1071         eject > /dev/null 2>&1
1072         status=$?
1073         internal_call=`expr $internal_call - 1`
1074         if [ $status -gt 1 ]; then
1075                 Exit $status "$exit_slot" "$exit_answer"
1076                 return $?                       # in case we are internal
1077         fi
1078
1079         # If we were doing an "advance", we are done.
1080         if [ $whichslot = advance ]; then
1081                 if [ $loadslot -ne $cleanslot ]; then
1082                         rm -f $slotfile
1083                         echo $loadslot > $slotfile
1084                 fi
1085                 Exit 0 "$loadslot" "/dev/null"
1086                 return $?                       # in case we are internal
1087         fi
1088
1089         # Load the tape, finally!
1090         Log `_ "LOADSLOT -> loading tape from slot %s to drive %s (%s)" "$loadslot" "$driveslot" "$tape"`
1091         result=`Run $MTX load $loadslot $driveslot 2>&1`
1092         status=$?
1093         Log `_ '         -> status %s, result "%s"' "$status" "$result"`
1094         mtx_status_valid=0
1095         if [ $status -ne 0 ]; then
1096                 Exit 2 "$loadslot" "$result"
1097                 return $?                       # in case we are internal
1098         fi
1099
1100         ###
1101         # Cleaning tapes never go "ready", so instead we just sit here
1102         # for "long enough" (as determined empirically by the user),
1103         # then return success.
1104         ###
1105         if [ $loadslot -eq $cleanslot ]; then
1106                 Run sleep $cleancycle
1107                 Exit 0 "$loadslot" "$tape"
1108                 return $?                       # in case we are internal
1109         fi
1110
1111         ###
1112         # Wait for the drive to go online.
1113         ###
1114         waittime=0
1115         ready=0
1116         sleep $initial_poll_delay
1117         while [ $waittime -lt $max_drive_wait ]; do
1118                 result=`Run $MT $MTF $tape rewind 2>&1`
1119                 if [ $? -eq 0 ]; then
1120                         ready=1
1121                         break
1122                 fi
1123                 sleep $poll_drive_ready
1124                 waittime=`expr $waittime + $poll_drive_ready`
1125         done
1126         if [ $ready -eq 0 ]; then
1127                 Exit 2 "$loadslot" `_ 'Drive not ready after %s seconds, rewind said "%s"' "$max_drive_wait" "$result"`
1128                 return $?                       # in case we are internal
1129         fi
1130
1131         if [ $loadslot -ne $cleanslot ]; then
1132                 rm -f $slotfile
1133                 echo $loadslot > $slotfile
1134         fi
1135         Exit 0 "$loadslot" "$tape"
1136         return $?                               # in case we are internal
1137 }
1138
1139 ###
1140 # Return information about how the changer is configured and the current
1141 # state of the robot.
1142 ###
1143
1144 info() {
1145         test -n "$DEBUG" && set -x
1146         get_loaded_info
1147         get_slot_list
1148         Log       `_ 'INFO     -> first slot: %s' "$firstslot"`
1149         LogAppend `_ '         -> current slot: %s' "$currentslot"`
1150         LogAppend `_ '         -> loaded slot: %s' "$loadedslot"`
1151         LogAppend `_ '         -> last slot: %s' "$lastslot"`
1152         LogAppend `_ '         -> slot list: %s' "$slot_list"`
1153         LogAppend `_ '         -> can go backwards: 1'`
1154         LogAppend `_ '         -> havereader: %s' "$havereader"`
1155
1156         ###
1157         # Check if a barcode reader is configured or not.  If so, it
1158         # passes the 4th item in the echo back to amtape signifying it
1159         # can search based on barcodes.
1160         ###
1161         reader=
1162         if [ $havereader -eq 1 ]; then
1163                 reader=1
1164         fi
1165
1166         if [ $currentslot -lt $firstslot -o $currentslot -gt $lastslot ]; then
1167                 currentslot=$firstslot          # what "current" will get
1168         fi
1169         numslots=`expr $lastslot - $firstslot + 1`
1170         Exit 0 "$currentslot" "$numslots 1 $reader"
1171         return $?                               # in case we are internal
1172 }
1173
1174 ###
1175 # Read the labelfile and scan for a particular entry.
1176 ###
1177
1178 read_labelfile() {
1179         labelfile_entry_found=0
1180         labelfile_label=
1181         labelfile_barcode=
1182
1183         lbl_search=$1
1184         bc_search=$2
1185
1186         line=0
1187         while read lbl bc junk; do
1188                 line=`expr $line + 1`
1189                 if [ -z "$lbl" -o -z "$bc" -o -n "$junk" ]; then
1190                         Log       `_ 'ERROR    -> Line %s malformed: %s %s %s' "$line" "$lbl" "$bc" "$junk"`
1191                         LogAppend `_ '         -> Remove %s and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
1192                         Exit 2 \
1193                              `_ '<none>'` \
1194                              `_ 'Line %s malformed in %s: %s %s %s' "$line" "$labelfile" "$lbl" "$bc" "$junk"`
1195                         return $?               # in case we are internal
1196                 fi
1197                 if [ $lbl = "$lbl_search" -o $bc = "$bc_search" ]; then
1198                         if [ $labelfile_entry_found -ne 0 ]; then
1199                                 Log       `_ 'ERROR    -> Duplicate entries: %s line %s' "$labelfile" "$line"`
1200                                 LogAppend `_ '         -> Remove $labelfile and run "$sbindir/amtape $config update"' "$labelfile" "$sbindir/amtape" "$config"`
1201                                 Exit 2 \
1202                                      `_ '<none>'` \
1203                                      `_ 'Duplicate entries: %s line %s' "$labelfile" "$line"`
1204                                 return $?       # in case we are internal
1205                         fi
1206                         labelfile_entry_found=1
1207                         labelfile_label=$lbl
1208                         labelfile_barcode=$bc
1209                 fi
1210         done
1211 }
1212
1213 ###
1214 # Adds the label and barcode for the currently loaded tape to the
1215 # barcode file.  Return an error if the database is messed up.
1216 ###
1217
1218 addlabel() {
1219         test -n "$DEBUG" && set -x
1220         if [ $# -lt 1 ]; then
1221                 Exit 2 `_ '<none>'` `_ 'Missing -label argument'`
1222                 return $?                       # in case we are internal
1223         fi
1224         tapelabel=$1
1225         if [ $havereader -eq 0 ]; then
1226                 Exit 2 `_ '<none>'` `_ 'Not configured with barcode reader'`
1227                 return $?                       # in case we are internal
1228         fi
1229         get_loaded_info
1230         if [ $loadedslot -lt 0 ]; then
1231                 Exit 1 `_ '<none>'` `_ 'No tape currently loaded'`
1232                 return $?                       # in case we are internal
1233         fi
1234         Log       `_ 'LABEL    -> Adding label "%s" with barcode "%s" for slot %s into %s' "$tapelabel" "$loadedbarcode" "$loadedslot" "$labelfile"`
1235         read_labelfile "$tapelabel" "$loadedbarcode" < $labelfile
1236         if [ $labelfile_entry_found -ne 0 ]; then
1237                 lf_val=
1238                 if [ "$labelfile_barcode" != "$loadedbarcode" ]; then
1239                         lf_type=label
1240                         lf_val=$tapelabel
1241                         val_type=barcode
1242                         old_val=$labelfile_barcode
1243                         new_val=$loadedbarcode
1244                 elif [ "$labelfile_label" != "$tapelabel" ]; then
1245                         lf_type=barcode
1246                         lf_val=$loadedbarcode
1247                         val_type=label
1248                         old_val=$labelfile_label
1249                         new_val=$tapelabel
1250                 fi
1251                 if [ -n "$lf_val" ]; then
1252                         LogAppend `_ 'ERROR    -> !!! Label database corrupted !!!'`
1253                         LogAppend `_ '         -> "%s" conflicts with new %s "%s" for %s "%s"' "$old_val" "$val_type" "$new_val" "$lf_type" "$lf_val"`
1254                         Exit 2 \
1255                              `_ '<none>'` \
1256                              `_ '%s: "%s" conflicts with new %s "%s" for %s "%s"' "$tapelabel" "$old_val" "$val_type" "$new_val" "$lf_type" "$lf_val"`
1257                         return $?               # in case we are internal
1258                 fi
1259                 LogAppend `_ "         -> already synced"`
1260         else
1261                 echo "$tapelabel $loadedbarcode" >> $labelfile
1262                 LogAppend `_ '         -> appended %s entry: %s %s' "$labelfile" "$tapelabel" "$loadedbarcode"`
1263         fi
1264         Exit 0 "$loadedslot" "$tape"
1265         return $?                               # in case we are internal
1266 }
1267
1268 ###
1269 # Look for a label in the barcode file.  If found, locate the slot it's
1270 # in by looking for the barcode in the mtx output, then load that tape.
1271 ###
1272
1273 searchtape() {
1274         test -n "$DEBUG" && set -x
1275         if [ $# -lt 1 ]; then
1276                 Exit 2 `_ '<none>'` `_ 'Missing -search argument'`
1277                 return $?                       # in case we are internal
1278         fi
1279         tapelabel=$1
1280         if [ $havereader -eq 0 ]; then
1281                 Exit 2 `_ '<none>'` `_ 'Not configured with barcode reader'`
1282                 return $?                       # in case we are internal
1283         fi
1284         Log `_ 'SEARCH   -> Hunting for label "%s"' "$tapelabel"`
1285         read_labelfile "$tapelabel" "" < $labelfile
1286         if [ $labelfile_entry_found -eq 0 ]; then
1287                 LogAppend `_ '         -> !!! label "%s" not found in %s !!!' "$tapelabel" "$labelfile"`
1288                 LogAppend `_ '         -> Remove $labelfile and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
1289                 Exit 2 \
1290                      `_ '<none>'` \
1291                      `_ '%s: label "%s" not found in %s' "$tapelabel" "$tapelabel" "$labelfile"`
1292                 return $?                       # in case we are internal
1293         fi
1294         LogAppend `_ '         -> barcode is "%s"' "$labelfile_barcode"`
1295         get_mtx_status
1296         foundslot=`sed -n '
1297 /VolumeTag *= *'$labelfile_barcode' *$/                 {
1298         s/.*Storage Element \([0-9][0-9]*\).*/\1/p
1299         q
1300 }
1301 ' < $mtx_status`
1302         LogAppend `_ '         -> foundslot is %s' "$foundslot"`
1303         if [ -z "$foundslot" ]; then
1304                 LogAppend `_ 'ERROR    -> !!! Could not find slot for barcode "%s"!!!' "$labelfile_barcode"`
1305                 LogAppend `_ '         -> Remove %s and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
1306                 Exit 2 \
1307                      `_ '<none>'` \
1308                      `_ 'barcode "%s" not found in mtx status output' "$labelfile_barcode"`
1309                 return $?                       # in case we are internal
1310         fi
1311         # Call loadslot without doing it as an internal and let it finish
1312         # things up.
1313         loadslot $foundslot
1314         # NOTREACHED
1315         Exit 2 `_ '<none>'` `_ 'searchtape: should not get here'`
1316         return $?                               # in case we are internal
1317 }
1318
1319 ###
1320 # Program invocation begins here
1321 ###
1322
1323 if [ $# -lt 1 ]; then
1324         Exit 2 `_ '<none>'` `_ 'Usage: %s -command args' "$myname"`
1325 fi
1326 cmd=$1
1327 shift
1328 case "$cmd" in
1329 -slot)
1330         loadslot "$@"
1331         ;;
1332 -info)
1333         info "$@"
1334         ;;
1335 -reset)
1336         reset "$@"
1337         ;;
1338 -eject)
1339         eject "$@"
1340         ;;
1341 -label) 
1342         addlabel "$@"
1343         ;;
1344 -search)
1345         searchtape "$@"
1346         ;;
1347 -clean)
1348         loadslot clean
1349         ;;
1350 *)
1351         Exit 2 `_ '<none>'` `_ 'unknown option: %s' "$cmd"`
1352         ;;
1353 esac
1354
1355 Exit 2 `_ '<none>'` `_ '%s: should not get here' "$myname"`