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