+###
+# Read the labelfile and scan for a particular entry.
+###
+
+read_labelfile() {
+ labelfile_entry_found=0
+ labelfile_label=
+ labelfile_barcode=
+
+ lbl_search=$1
+ bc_search=$2
+
+ line=0
+ while read lbl bc junk; do
+ line=`expr $line + 1`
+ if [ -z "$lbl" -o -z "$bc" -o -n "$junk" ]; then
+ Log `_ 'ERROR -> Line %s malformed: %s %s %s' "$line" "$lbl" "$bc" "$junk"`
+ LogAppend `_ ' -> Remove %s and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
+ Exit 2 \
+ `_ '<none>'` \
+ `_ 'Line %s malformed in %s: %s %s %s' "$line" "$labelfile" "$lbl" "$bc" "$junk"`
+ return $? # in case we are internal
+ fi
+ if [ $lbl = "$lbl_search" -o $bc = "$bc_search" ]; then
+ if [ $labelfile_entry_found -ne 0 ]; then
+ Log `_ 'ERROR -> Duplicate entries: %s line %s' "$labelfile" "$line"`
+ LogAppend `_ ' -> Remove %s and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
+ Exit 2 \
+ `_ '<none>'` \
+ `_ 'Duplicate entries: %s line %s' "$labelfile" "$line"`
+ return $? # in case we are internal
+ fi
+ labelfile_entry_found=1
+ labelfile_label=$lbl
+ labelfile_barcode=$bc
+ fi
+ done
+}
+
+lookup_label_by_barcode() {
+ [ -z "$1" ] && return
+ read_labelfile "" "$1" < $labelfile
+ echo "$labelfile_label"
+}
+
+lookup_barcode_by_label() {
+ [ -z "$1" ] && return
+ read_labelfile "$1" "" < $labelfile
+ echo "$labelfile_barcode"
+}
+
+remove_from_labelfile() {
+ labelfile=$1
+ lbl_search=$2
+ bc_search=$3
+
+ internal_remove_from_labelfile "$lbl_search" "$bc_search" < $labelfile >$labelfile.new
+ if [ $labelfile_entry_found -ne 0 ]; then
+ mv -f $labelfile.new $labelfile
+ LogAppend `_ 'Removed Entry "%s %s" from barcode database' "$labelfile_label" "$labelfile_barcode"`
+ fi
+}
+
+internal_remove_from_labelfile() {
+ labelfile_entry_found=0
+ labelfile_label=
+ labelfile_barcode=
+
+ lbl_search=$1
+ bc_search=$2
+
+ line=0
+ while read lbl bc junk; do
+ line=`expr $line + 1`
+ if [ -z "$lbl" -o -z "$bc" -o -n "$junk" ]; then
+ Log `_ 'ERROR -> Line %s malformed: %s %s %s' "$line" "$lbl" "$bc" "$junk"`
+ LogAppend `_ ' -> Remove %s and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
+ Exit 2 \
+ `_ '<none>'` \
+ `_ 'Line %s malformed in %s: %s %s %s' "$line" "$labelfile" "$lbl" "$bc" "$junk"`
+ return $? # in case we are internal
+ fi
+ if [ $lbl = "$lbl_search" -o $bc = "$bc_search" ]; then
+ if [ $labelfile_entry_found -ne 0 ]; then
+ Log `_ 'ERROR -> Duplicate entries: %s line %s' "$labelfile" "$line"`
+ LogAppend `_ ' -> Remove %s and run "%s %s update"' "$labelfile" "$sbindir/amtape" "$config"`
+ Exit 2 \
+ `_ '<none>'` \
+ `_ 'Duplicate entries: %s line %s' "$labelfile" "$line"`
+ return $? # in case we are internal
+ fi
+ labelfile_entry_found=1
+ labelfile_label=$lbl
+ labelfile_barcode=$bc
+ else
+ echo $lbl $bc
+ fi
+ done
+}
+
+###
+# Add a new slot -> label correspondance to the slotinfo file, removing any previous
+# information about that slot.
+###
+
+record_label_in_slot() {
+ [ -z "$slotinfofile" ] && return
+ newlabel="$1"
+ newslot="$2"
+
+ (
+ if [ -f "$slotinfofile" ]; then
+ grep -v "^$newslot " < "$slotinfofile"
+ fi
+ echo "$newslot $newlabel"
+ ) > "$slotinfofile~"
+ mv "$slotinfofile~" "$slotinfofile"
+}
+
+###
+# Remove a slot from the slotinfo file
+###
+
+remove_slot_from_slotinfo() {
+ [ -z "$slotinfofile" ] && return
+ emptyslot="$1"
+
+ (
+ if [ -f "$slotinfofile" ]; then
+ grep -v "^$emptyslot " < "$slotinfofile"
+ fi
+ ) > "$slotinfofile~"
+ mv "$slotinfofile~" "$slotinfofile"
+}
+
+###
+# Assuming get_mtx_status has been run,
+# - if we have barcodes, regenerate the slotinfo file completely by
+# mapping barcodes in the status into labels using the labelfile
+# - otherwise, remove all empty slots from the slotinfo file
+###
+
+regenerate_slotinfo_from_mtx() {
+ [ -z "$slotinfofile" ] && return
+ [ "$mtx_status_valid" = "1" ] || return
+
+ if [ "$havereader" = "1" ]; then
+ # rewrite slotinfo entirely based on the status, since it has barcodes
+ :> "$slotinfofile~"
+ sed -n '/.*Storage Element \([0-9][0-9]*\).*VolumeTag *= *\([^ ]*\) *$/{
+s/.*Storage Element \([0-9][0-9]*\).*VolumeTag *= *\([^ ]*\) *$/\1 \2/
+p
+}' < $mtx_status | while read newslot newbarcode; do
+ newlabel=`lookup_label_by_barcode "$newbarcode"`
+ if [ -n "$newlabel" ]; then
+ echo "$newslot $newlabel" >> "$slotinfofile~"
+ fi
+ done
+ mv "$slotinfofile~" "$slotinfofile"
+ else
+ # just remove empty slots from slotinfo
+
+ # first determine which slots are not really empty, but are
+ # loaded into a data transfer element
+loadedslots=`sed -n '/.*(Storage Element \([0-9][0-9]*\) Loaded).*/{
+s/.*(Storage Element \([0-9][0-9]*\) Loaded).*/\1/g
+p
+}' < $mtx_status`
+
+ # now look for any slots which are empty, but which aren't
+ # in the set of loaded slots
+ sed -n '/.*Storage Element \([0-9][0-9]*\): *Empty.*/{
+s/.*Storage Element \([0-9][0-9]*\): *Empty.*/\1/g
+p
+}' < $mtx_status | while read emptyslot; do
+ reallyempty=1
+ if [ -n "$loadedslots" ]; then
+ for loadedslot in $loadedslots; do
+ [ "$loadedslot" = "$emptyslot" ] && reallyempty=0
+ done
+ fi
+ if [ "$reallyempty" = "1" ]; then
+ remove_slot_from_slotinfo "$emptyslot"
+ fi
+ done
+ fi
+}
+
+DBGFILE=`amgetconf dbopen.$myname 2>/dev/null`