#!/bin/sh

# $Id$

#Dump the cleaning tape information to the tape inventory cleaning report.

#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

#Get our variables for CLEANING filenames.
fname=CLEANING
output_file=$inventory_dir/$fname
temp_file=$inventory_dir/.$fname$$.temp
trap "rm -r $temp_file; exit 1" HUP INT QUIT ABRT ALRM TERM

#Make sure we know how up-to-date this is.
echo -e Cleaning Report: `date` >> $temp_file 2>&1
echo -e Brought to You by: `basename $0` "\n" >> $temp_file 2>&1

#Loop over all the media changers (remember to delete the header line).
enstore conf $timeout --list-media-changers | sed '1d' |
while read line; do
    mc=`echo $line | awk '{print $1}'`

    #Skip Manual and RDD (aka null) media changers.
    echo $line | egrep "Manual|RDD" > /dev/null 2> /dev/null
    if [ $? -eq 0 ]; then
        continue
    fi

    #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

    #Write out the cleaning information.
    echo -e "Tape Library:" $tape_library "\n" >> $temp_file 2>&1
    enstore media $timeout --list-clean $mc  >> $temp_file 2>&1
    echo -e "\n" >> $temp_file 2>&1
done

mv $temp_file $output_file    #Do the temp file swap.
