#!/bin/sh

# $Id$

#Make the VOLUMES and NOACCESS tape inventory pages.

set -u
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

#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
    if [ $? -ne 0 ]; then
        #Failed to make the directory?
        exit 1
    fi
fi

#Get our variables for VOLUMES filenames.
fname=VOLUMES
outfile_volumes=$inventory_dir/$fname
outfile_volumes_temp=$inventory_dir/.$fname$$.temp
trap "rm -f $outfile_volumes_temp; exit 1" HUP INT QUIT ABRT ALRM TERM


#Output the data to the VOLUMES temp file.
echo -e Volumes Report: `date` > $outfile_volumes_temp 2>&1
echo -e Brought to You by: `basename $0` "\n" >> $outfile_volumes_temp 2>&1
enstore info $timeout --vols media_type disk "!=" >> $outfile_volumes_temp 2>&1
echo >> $outfile_volumes_temp 2>&1
echo `date` >> $outfile_volumes_temp 2>&1

#Make the temp file the real VOLUMES file.
mv $outfile_volumes_temp $outfile_volumes



#Get our variables for NOACCESS filenames.
fname=NOACCESS
outfile_noaccess=$inventory_dir/$fname
outfile_noaccess_temp=$inventory_dir/.$fname$$.temp
trap "rm -f $outfile_noaccess_temp; exit 1" HUP INT QUIT ABRT ALRM TERM

#Output the data to the NOACCESS temp file.
echo `date` > $outfile_noaccess_temp 2>&1
echo -e Brought to You by: `basename $0` "\n" >> $outfile_noaccess_temp 2>&1
egrep "NOACCESS|NOTALLOWED" $outfile_volumes >> $outfile_noaccess_temp 2>&1
echo >> $outfile_noaccess_temp 2>&1
echo `date` >> $outfile_noaccess_temp 2>&1

#Make the temp file the real NOACCESS file.
mv $outfile_noaccess_temp $outfile_noaccess
