#!/bin/sh

# $Id$

#Obtain a list of tapes with more mounts that the manufacturer's recommened
# limit for the type of tape.

#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
output_file=$inventory_dir/QUOTA_ALERT
temp_file=$output_file.tmp


#Get the host the Enstore database is running on.
dbhost=`enstore conf $timeout --show database dbhost`
if [ $? -ne 0 ]; then
    echo "Unable to obtain database host information."
    exit 1
fi

#Get the port the Enstore database is running on.
dbport=`enstore conf $timeout --show database dbport`
if [ $? -ne 0 ]; then
    echo "Unable to obtain database port information."
    exit 1
fi

#Get the name of the Enstore database.
dbname=`enstore conf $timeout --show database dbname`
if [ $? -ne 0 ]; then
    echo "Unable to obtain database name information."
    exit 1
fi

#Get the owner/user of the Enstore database.
dbuser=`enstore conf $timeout --show database dbuser`
if [ $? -ne 0 ]; then
    echo "Unable to obtain database user information."
    exit 1
fi

#If we write to a temp file, and swap in it when we are done, there will
# not any time when the page is empty becuase the scipt is still writing
# the file.
rm -f $temp_file

echo Quota Alert Report: `date ` >> $temp_file 2>&1
echo -e Brought to You by: `basename $0` "\n" >> $temp_file 2>&1
psql -p $dbport -h $dbhost -U $dbuser $dbname -c "select * from quota_alert();" >> $temp_file 2>&1

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