#!/bin/sh

# $Id$

#Dump the volume information for each tape library (robot) for the
# tape inventory summary.

#Make sure to set a timeout.
timeout='--timeout 10 --retries 3'

#First obtain the directory to write the output.
html_dir=`enstore conf $timeout --show crons html_dir`
if [ ! -d "$html_dir" ]; then
    echo HTML directory $html_dir not found.
    exit 1
fi
#If the inventory directory does not exist, create it.
inventory_dir=$html_dir/tape_inventory
if [ ! -d "$inventory_dir" ]; then
    mkdir -p $inventory_dir
fi

#Loop over all the media changers (remember to delete the header line).
enstore conf $timeout --list-media-changers | sed '1d' |
while read line; do
    #Skip Manual and RDD (aka null) media changers.
    echo $line | egrep "Manual|RDD" > /dev/null 2> /dev/null
    if [ $? -eq 0 ]; then
        continue
    fi

    mc=`echo $line | awk '{print $1}'`

    #Get the name of the tape library.
    tape_library=`enstore conf $timeout --show $mc tape_library 2> /dev/null`
    if [ $? -ne 0 ]; then
        #Use the entire line from the --list-media-changers command if
	# the tape_library was not found in the media_changer configuration.
        tape_library=$line
    fi

    #Use just the short name of the media changer converted to upper case.
    tl_name=`echo $mc | tr "." " " | cut -f 1 -d " " | tr '[a-z]' '[A-Z]'`
    #Create the variables that point to the files to output to.
    fname=${tl_name}-VOLUMES
    old_output_file=$inventory_dir/$fname
    output_file=$inventory_dir/$fname.html
    temp_file=$inventory_dir/.$fname$$.temp
    temp_file_html=$inventory_dir/.$fname$$.temp.html
    trap "rm -f $temp_file $temp_file_html; exit 1" HUP INT QUIT ABRT ALRM TERM
    
    echo Starting to making output file for $tl_name.  `date`

    #Make sure we know how up-to-date this is.
    echo -e $tl_name Volume List Report: `date` > "$temp_file" 2>&1
    echo -e Brought to You by: `basename $0` "\n" >> "$temp_file" 2>&1
    #Get the volumes!
    enstore media $timeout --list-volumes $mc >> "$temp_file" 2>&1
    echo -e "\n" >> "$temp_file" 2>&1
    #Give and end time so we know how long it took.
    echo -e $fname Report Completed: `date` "\n" >> "$temp_file" 2>&1

    echo Done making output file for $tl_name.  `date`

    #For backward compatiblity make an html file.
    echo Starting to make html file for $tl_name.  `date`
    cat << EOF > "$temp_file_html"
<html> <head> <title>$tl_name Volume Status Page</title> </head>
<body>
<body bgcolor="#ffffff" text=#000000">
<meta http-equiv="Refresh" content="900">
<pre>
EOF

    cat "$temp_file" >> "$temp_file_html"

    cat << EOF >> "$temp_file_html"
</pre>
</body>
EOF

    #Move the temporary file into place.
    mv "$temp_file_html" "$output_file"

    #End of making the html file.
    echo Done making html file for $tl_name.  `date`

    #Remove the temp file.
    rm -f "$temp_file" "$temp_file_html"
    #At one time there used to be a text file and an html file.  The admins
    #complained that there were two files on the inventory summary.  So,
    #now we have just the html file.  If the old text output file is still
    #there delete it.
    rm -f "$old_output_file"

done
