improve bash completions and related delivery mechanism
[debian/mtx] / contrib / tapeunload.pl
1 #!/usr/bin/perl
2 ##########################  tapeunload   ###########################
3 # This script uses mtx 1.2.9pre2 to unload a tape 
4 # based on its volume tag.  You can
5 # specify a slot into which the tape should go, but if you don`t, it puts the
6 # tape into the slot from which it was taken.  This slot number
7 # is returned 
8 # both from the standard output and as the exit value.
9 # A negative exit value indicates an error.
10 # If volume tags are missing from any full slot or drive, 
11 # bar codes are rescanned automatically.
12 # Note: This script assumes that the tape is ready to be removed from
13 # the drive.  That means you may have to unload the tape from the drive
14 # with  "mt offline" before the tape can be moved to a storage slot.
15
16
17 # usage: 
18 # tapeunload TAPE_LABEL_1  
19 # Removes tape with label TAPE_LABEL_1 from a drive and puts it
20 # back into its storage slot. Or, 
21 # tapeunload TAPE_LABEL_1 40 
22 # Removes tape with label TAPE_LABEL_1 from a drive and puts it
23 # into its storage slot 40. 
24 #
25
26 # Set this variable to your mtx binary and proper scsi library device.
27 $MTXBIN="/usr/local/bin/mtx -f /dev/sga" ;  
28
29 # Additions and corrections are welcome.
30 # This software comes with absolutely no warranty and every other imaginable
31 # disclaimer.
32 #   --  Frank Samuelson sam@milagro.lanl.gov
33
34 ##################################################################
35
36 @wt= &mdo("status");  #slurp in the list of slots
37
38 # Check to be certain that every full slot has a volume tag
39 # Rescanning probably will not help.  I haven`t seen any bar code
40 # readers that read tapes that are in the drives.  But you never know...
41 for ($i=0; $i< $#wt; $i++) {  # look through every line
42     if ( $wt[$i] =~ /Full/  && $wt[$i] !~ /VolumeTag/ ) {
43         # if the element is full, but there is no volume tag, do inventory
44         @wt= &mdo("inventory status");
45         break;
46     }
47 }
48
49 #try to find our tape
50 $drivein=-1;
51 for ($i=0; $i< $#wt; $i++) {  # look through every line
52                               # for a full tape drive 
53     if ($wt[$i] =~ / *Data Transfer Element (d*):Full (Storage Element
54 (d*) Loaded):VolumeTag = (.*)/ ){
55         if ($ARGV[0] eq  $3) { # We found our tape
56             $drivein=$1;          # set the drive number
57             $slottogo=$2;       # set the slot number
58             break;             # stop reading the rest of the file.
59         }
60     }
61 }
62
63 if ( $drivein>=0) {         # we found the tape you wanted.
64     if ($#ARGV==1) {         #If an alternative slot was requested, set it.
65         $slottogo=$ARGV[1];  # and let mtx handle the errors.
66     }
67     
68     # Now we unload the tape.
69     @dump=&mdo(" unload $slottogo $drivein ");
70     print "$slottogo";
71     exit($slottogo);
72     # The end.
73
74     
75 } else {
76     print STDERR " Ug. Tape $ARGV[0] is not in a tape drive.";
77     print STDERR @wt;
78     exit(-4);
79 }
80
81
82 sub mdo             # a subroutine to call mtx ;
83 {
84 #    print STDERR "$_[0]";
85     if (!open(FD,"$MTXBIN $_[0] |")) {    #call mtx function 
86         print STDERR " ERRKK.  Could not start mtx ";
87         exit (-9);
88     }
89
90     @twt= <FD>;        # slurp in the output
91
92     if (! close(FD)) {        # if mtx exited with a nonzero value...
93         print STDERR " Mtx gave an error. Tapeload is exiting... ";
94         exit (-10);
95     }
96
97     @twt;
98 }