Merge branch 'upstream'
[debian/amanda] / changer-src / chg-lib.sh.in
1 ################################################################################
2 # You may want to customize these values
3 ################################################################################
4
5 # These are the defaults discovered by configure when Amanda was installed.
6 # They can be overridden here, or by by 'mt_binary' and 'mtx_binary', 
7 # respectively, in the changerfile (currently only for chg-zd-mtx.sh and
8 # chg-manual.sh).
9
10 MT=@MT@
11 MTX=@MTX@
12
13 # This is the flag used to specify a device filename to 'mt', usually '-f'
14 MTF=@MT_FILE_FLAG@
15
16 ################################################################################
17 # No user-level customization should be required beyond this point.
18 ################################################################################
19
20 ####
21 # Set up paths, including PATH for finding amanda executables
22
23 PATH="$PATH:@sbindir@:@libexecdir@"
24 prefix="@prefix@"
25 exec_prefix="@exec_prefix@"
26 sbindir="@sbindir@"
27 libexecdir="@libexecdir@"
28
29 ####
30 # Gettext
31
32 #+
33 # Function to:
34 #    Simplify gettext usage by allowing printf format
35 #    strings for translators, use _() identifier for
36 #    xgettext extraction similar to "C" usage and
37 #    collapsing "printf `gettext fmt` ...", which is
38 #    used everywhere, into one function.
39 #-
40 _() {
41         fmt=`gettext -d amanda "$1"`
42         shift
43         printf "$fmt" $*
44 }
45
46 ####
47 # Eponymous functions to access various amanda apps
48 # TODO: move to amanda-sh-lib.sh
49
50 if test "@USE_VERSION_SUFFIXES@" = "yes"; then
51     amgetconf() {
52         "${sbindir}/amgetconf-@VERSION@" "${@}"
53     }
54     amdevcheck() {
55         "${sbindir}/amdevcheck-@VERSION@" "${@}"
56     }
57 else
58     amgetconf() {
59         "${sbindir}/amgetconf" "${@}"
60     }
61     amdevcheck() {
62         "${sbindir}/amdevcheck" "${@}"
63     }
64 fi
65
66 # This function tries to find a useable mt binary. If a fully-qualified path
67 # was provided at configure time or via a config file, we check that it
68 # exists and is executable. If an incomplete path was specified (e.g., "mt"),
69 # we ask the shell to search the path. Returns 0 on success, 1 on failure.
70
71 try_find_mt() {
72     # Only do this once.
73     if test -n $mt_found; then
74         return 0
75     fi
76
77     if test -z $MT; then
78         MT=mt
79     fi
80
81     if "`echo $MT | dd bs=1 count=1`" = "/"; then
82         if ! test -f "${MT}"; then
83             echo `_ "mt binary at '%s' not found" "$MTX"`
84             return 1
85         fi
86         if ! test -x "${MT}"; then
87             echo `_ "mt binary at '%s' is not executable" "$MTX"`
88             return 1
89         fi
90     else
91         # try running it to see if the shell can find it
92         "$MT" >/dev/null 2>/dev/null
93         if test $? -eq 127 -o $? -eq 126; then
94             echo `_ "Could not run mt binary at '%s'" "$MTX"`
95             return 1
96         fi
97     fi
98
99     mt_found=yes
100     return 0
101 }
102
103 # This function strips the tape: from the front of device names.
104 # Capture its output with ``.
105 tape_device_filename() {
106     # Easy (?) to express this in Perl.
107     perl -e '$ARGV[0] =~ /^(?:([^:]+):)?(.*)$/;
108              print $2 if ($1 || "tape") eq "tape"' "$1"
109 }
110
111 # Runs amdevcheck to find out if a particular device is usable. Finds the
112 # config name based on pwd, so run it from the config directory. The sole
113 # argument should be the device name to check. Note that amdevcheck can FP
114 # on device status, since some devices can't differentiate between device
115 # problems and an unlabeled volume.
116 amdevcheck_status() {
117     amdevcheck_message=
118     local amdevcheck_config_dir=`pwd`
119     local amdevcheck_config_name=`basename $amdevcheck_config_dir`
120     local amdevcheck_output=`amdevcheck $amdevcheck_config_name $@`
121     local amdevcheck_status=$?
122
123     test "$amdevcheck_status" -ne 0 && return 0
124
125     # extract any messages
126     amdevcheck_message=`echo "$amdevcheck_output" | sed '/^MESSAGE /{s/^MESSAGE //;p;}; d'`
127
128     # Return 1 if it's possible that the device is offline or busy; if the device cannot
129     # distinguish this state from an error condition, then our caller will just have to
130     # time out
131     if echo "$amdevcheck_output" | $EGREP "VOLUME_MISSING" > /dev/null; then
132         return 1
133     else
134         return 0
135     fi
136 }
137
138 # This attempts to eject a device using whatever system tools are available.
139 # At the moment, that means mt for tapes, and nothing otherwise, but might
140 # be extended at some later time.
141
142 try_eject_device() {
143     if echo "$1" | grep -e '^tape:' > /dev/null; then
144         try_eject_device_tape="`echo \"$1\" | cut -b6-`"
145     elif echo "$1" | grep -v : > /dev/null; then
146         try_eject_device_tape="$1"
147     else
148         try_eject_device_tape=
149     fi
150
151     if test -n "$try_eject_device_tape"; then
152         if try_find_mt; then
153             $MT $MTF "$try_eject_device_tape" eject
154         fi
155     else
156         # Technically we failed to eject the device, but we presume that's
157         # because it doesn't require ejection.
158         return 0
159     fi
160 }