improve bash completions and related delivery mechanism
[debian/mtx] / contrib / tapeload.pl
1 !/usr/bin/perl
2 ##########################  tapeload   ###########################
3 # This script uses mtx 1.2.9pre2 to load a tape based 
4 # on its volume tag.  You can
5 # specify a tape drive by number, but if you don`t, it puts the
6 # tape in the first available drive and returns the number of that drive,
7 # both from the standard output and as the exit value.
8 # A negative exit value indicates an error.
9 # If volume tags are missing from any full slot, bar codes are rescanned
10 # automatically.  
11 #
12 # usage: 
13 # tapeload TAPE_LABEL_1    # Loads tape with label TAPE_LABEL_1 into a drive
14 # or
15 # tapeload TAPE_LABEL_1 1  # Loads tape with label TAPE_LABEL_1 into drive #1
16 #
17
18 # Set this variable to your mtx binary and proper scsi library device.
19 $MTXBIN="/usr/local/bin/mtx -f /dev/sga" ;  
20
21 # Additions and corrections are welcome.
22 # This software comes with absolutely no warranty and every other imaginable
23 # disclaimer.
24 #   -- Frank Samuelson sam@milagro.lanl.gov
25 ##################################################################
26
27 @wt= &mdo("status");  #slurp in the list of slots
28
29 # Check to be certain that every full slot has a volume tag
30 for ($i=0; $i< $#wt; $i++) {  # look through every line
31     if ( $wt[$i] =~ /Full/  && $wt[$i] !~ /VolumeTag/ ) {
32         # if the element is full, but there is no volume tag, do inventory
33         @wt= &mdo("inventory status");
34         break;
35     }
36 }
37
38 #try to find our tape
39 $slot=-1;
40 for ($i=0; $i< $#wt; $i++) {  # look through every line
41     if ($wt[$i] =~ / *Storage Element (d*):Full :VolumeTag=(.*)/ ) {
42         if ($ARGV[0] eq  $2) { # We found the tape
43             $slot=$1;          # set the slot number
44             break;             # stop reading the rest of the file.
45         }
46     }
47 }
48
49 if ( $slot>0) {         # we found the tape you wanted.
50
51     $drivefound=-1;          # set flag to bad value
52     for ($i=0; $i< $#wt; $i++) {  # look through every line
53         # if this is a tape drive
54         if ($wt[$i] =~ / *Data Transfer Element (d*):(.*)/ ) { #parse the line
55             $drive=$1;
56             $state=$2;
57 #           print STDERR "$wt[$i] $drive $state";
58             if ($state =~ /Full/) {   # This drive is full.
59                 # if we are looking for a particular drive and this is it
60                 if ( $#ARGV==1 && $drive == $ARGV[1]) { 
61                     print STDERR " ERROR: Specified drive $ARGV[1] is full.";
62                     print STDERR @wt;
63                     exit(-6);
64                 }
65             } elsif ($state =~ /Empty/) { #This is a tape drive and it`s empty.
66                 if ( $#ARGV==1 ) {          # If we want a particular drive
67                     if ($drive == $ARGV[1]) {   # and this is it,
68                         $drivefound=$drive;      # mark it so.
69                         break;
70                     }
71                 } else {              # If any old drive will do
72                     $drivefound=$drive;    # Mark it.
73                     break;
74                 }
75             } else {         # This is a tape drive, but what the heck is it?
76                 print STDERR " Cannot assess drive status in line";
77                 print STDERR $wt[$i];
78                 exit(-7);
79             }
80         }
81     }
82
83     if ( $drivefound < 0 ) {  # specified drive was not found
84         print STDERR "Error: Specified drive $ARGV[1] was not found";
85         print STDERR @wt;
86         exit(-8);
87     }
88     # Now we actually load the tape.
89     @dump=&mdo(" load $slot $drivefound ");
90     print "$drivefound";
91     exit($drivefound);
92     # The end.
93
94     
95 } else {
96     print STDERR " Ug. Tape $ARGV[0] is not in the library.";
97     print STDERR @wt;
98     exit(-4);
99 }
100
101
102 sub mdo             # a subroutine to call mtx ;
103 {
104 #    print STDERR "$_[0]";
105     if (!open(FD,"$MTXBIN $_[0] |")) {    #call mtx function 
106         print STDERR " ERRKK.  Could not start mtx ";
107         exit (-9);
108     }
109
110     @twt= <FD>;        # slurp in the output
111
112     if (! close(FD)) {        # if mtx exited with a nonzero value...
113         print STDERR " Mtx gave an error. Tapeload is exiting... ";
114         exit (-10);
115     }
116
117     @twt;
118 }