98b78cc8e2915a93f9eddb1f3bcc362dc576afa6
[debian/amanda] / server-src / amfreetapes.sh.in
1 #! /bin/sh
2 #
3 # ICEM internal :-)
4 #
5
6 prefix=@prefix@
7 exec_prefix=@exec_prefix@
8 sbindir=@sbindir@
9 libexecdir=@libexecdir@
10
11 ConfigDir=@CONFIG_DIR@
12
13 PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb
14 export PATH
15
16 Program=`basename $0`
17
18 log () {
19         echo 1>&2 "$@"
20         return 0
21 }
22
23 Config=$1
24 if [ "$Config" = "" ]; then
25         log "usage: ${Program} <config> <volume header>"
26         exit 1
27 fi
28 VOLHEADER=$2
29 if [ "$VOLHEADER" = "" ]; then
30         log "usage: ${Program} <config> <volume header>"
31         exit 1
32 fi
33
34 #
35 # Check if the configuration directory exists.  Make sure that the
36 # necessary files can be found, such as amanda.conf and tapelist.
37 #
38 if [ ! -d ${ConfigDir}/${Config} ]; then
39         log "${Program}: configuration directory ${ConfigDir}/${Config} does not exist."
40         exit 1
41 fi
42 (cd ${ConfigDir}/${Config} >/dev/null 2>&1) || exit $?
43 cd ${ConfigDir}/${Config}
44 if [ ! -r amanda.conf ]; then
45         log "${Program}: amanda.conf not found or is not readable in ${ConfigDir}."
46         exit 1
47 fi
48
49 # Get the location and name of the tapelist filename.  If tapelist is not
50 # specified in the amanda.conf file, then use tapelist in the config
51 # directory.
52 TapeList=`amgetconf${SUF} tapelist`
53 if [ ! "$TapeList" ]; then
54         TapeList="$ConfigDir/$Config/tapelist"
55 fi
56 if [ ! -r $TapeList ]; then
57         log "${Program}: $TapeList not found or is not readable."
58         exit 1
59 fi
60
61 AllTapeList="${TapeList}.all"
62
63 [ ! -f $AllTapeList ] \
64         && echo "no tapelist $AllTapeList found" >&2 \
65         && cat $TapeList | sed 's/^.* //' | sort -u > $AllTapeList \
66         && echo "$AllTapeList created" >&2
67
68 cat $TapeList | sed 's/^.* //' > $AllTapeList.n
69 cat $AllTapeList >> $AllTapeList.n
70 sort -u $AllTapeList.n > $AllTapeList && rm -f $AllTapeList.n
71
72 MAXTAPE=`cat $AllTapeList | sed "s/^$VOLHEADER//" | sort -n | tail -1`
73 NEXTTAPE=`expr $MAXTAPE + 1`
74 echo "last Tape is ${VOLHEADER}${MAXTAPE}" >&2
75
76 I=1
77 while [ $I -le $MAXTAPE ]; do
78         WRITTEN=`grep "${VOLHEADER}${I}\$" $TapeList`
79         EXISTS=`grep "${VOLHEADER}${I}\$" $AllTapeList`
80         if [ "$EXISTS" = "" -a "$WRITTEN" = "" ]; then
81                 echo "missing tape ${VOLHEADER}${I}" >&2
82                 echo "${VOLHEADER}${I}" >> $AllTapeList \
83                 && echo "added ${VOLHEADER}${I} to $AllTapeList" >&2
84         elif [ "$EXISTS" = "" -a "$WRITTEN" != "" ]; then
85                 echo "tape written, but not existing: ${VOLHEADER}${I}" >&2
86         elif [ "$EXISTS" != "" -a "$WRITTEN" = "" ]; then
87                 echo "free tape: ${VOLHEADER}${I}" >&2
88         fi
89         I=`expr $I + 1`
90 done
91
92 echo "next tape is ${VOLHEADER}${NEXTTAPE}" >&2
93
94 exit 0