#!/bin/sh

# $Id$

#Dump the the summary information for the last 7 days to the weekly summary.
#An old version of this would send e-mail to a hardcoded list of people.

#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 weekly summary directory does not exist, create it.
# Note: generated_html/weekly_summary must match the WEEKLY_SUMMARY_SUBDIR
#       value in enstore_constants.py.
weekly_summary_dir=$html_dir/generated_html/weekly_summary
if [ ! -d "$weekly_summary_dir" ]; then
    mkdir -p $weekly_summary_dir
    if [ $? -ne 0 ]; then
        echo Unable to make ${weekly_summary_dir}.
        exit 1
    fi
fi

#Obtain the Enstore DB port number.
EDB_PORT=`enstore conf $timeout --show database dbport`
if [ -z "$EDB_PORT" ]; then
    echo "dbport not found in configuration."
    exit 1
fi

#Obtain the Enstore DB name.
EDB_NAME=`enstore conf $timeout --show database dbname`
if [ -z "$EDB_NAME" ]; then
    echo "dbname not found in configuration."
    exit 1
fi

#Obtain the Enstore DB host.
EDB_HOST=`enstore conf $timeout --show database dbhost`
if [ -z "$EDB_HOST" ]; then
    echo "dbhost not found in configuration."
    exit 1
fi

#Obtain the Enstore DB user/role.
EDB_USER=`enstore conf $timeout --show database dbuser_reader`   #dbuser_reader?
if [ -z "$EDB_USER" ]; then
    echo "dbuser not found in configuration."
    exit 1
fi

#Obtain the Accounting DB port number.
ADB_PORT=`enstore conf $timeout --show accounting_server dbport`
if [ -z "$ADB_PORT" ]; then
    echo "dbport not found in configuration."
    exit 1
fi

#Obtain the Accounting DB name.
ADB_NAME=`enstore conf $timeout --show accounting_server dbname`
if [ -z "$ADB_NAME" ]; then
    echo "dbname not found in configuration."
    exit 1
fi

#Obtain the Accounting DB host.
ADB_HOST=`enstore conf $timeout --show accounting_server dbhost`
if [ -z "$ADB_HOST" ]; then
    echo "dbhost not found in configuration."
    exit 1
fi

#Obtain the Accounting DB user/role.
ADB_USER=`enstore conf $timeout --show accounting_server dbuser`   #dbuser_reader?
if [ -z "$ADB_USER" ]; then
    echo "dbuser not found in configuration."
    exit 1
fi

#Get the temporary directory to use.
temp_dir=`enstore conf $timeout --show crons tmp_dir`
if [ ! -d "$temp_dir" ]; then
    temp_dir=/tmp
fi

#Extract the beginning timestamp for seven days ago in human readable format.
t1=`psql -p $ADB_PORT -h $ADB_HOST -U $ADB_USER $ADB_NAME -c "
select to_char(current_date - interval '7 days', 'Dy Mon DD HH24:MI:SS YYYY');
" | sed -n '3p'`
#Extract the last second of yesterday in human readable format.
t2=`psql -p $ADB_PORT -h $ADB_HOST -U $ADB_USER $ADB_NAME -c "
select to_char(current_date - interval '1 second', 'Dy Mon DD HH24:MI:SS YYYY');
" | sed -n '3p'`
#Extract the start date.
start_date=`psql -p $ADB_PORT -h $ADB_HOST -U $ADB_USER $ADB_NAME -c "
select to_char(current_date - interval '7 days', 'MM-DD-YYYY');
" | sed -n '3p' | tr -d " "`

output_basename=WEEKLY_SUMMARY_STARTING_${start_date}
output_file=$weekly_summary_dir/$output_basename
temp_file=$temp_dir/$output_basename.tmp

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


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

echo  >> $temp_file 2>&1
echo Reporting period: $t1 -- $t2  >> $temp_file 2>&1
echo  >> $temp_file 2>&1

echo                            >> $temp_file 2>&1
echo "========================" >> $temp_file 2>&1
echo "Transfers in last 7 days" >> $temp_file 2>&1
echo "========================" >> $temp_file 2>&1
echo                            >> $temp_file 2>&1

echo "Creating Transfers in last 7 days" `date`

psql -p $ADB_PORT -h $ADB_HOST -U $ADB_USER $ADB_NAME -c "
select * from data_transfer_last_7days() order by storage_group;
" >> $temp_file 2>&1

echo                                 >> $temp_file 2>&1
echo "=============================" >> $temp_file 2>&1
echo "Tapes recycled in last 7 days" >> $temp_file 2>&1
echo "=============================" >> $temp_file 2>&1
echo                                 >> $temp_file 2>&1

echo "Creating Tapes recycled in last 7 days" `date`

psql -p $EDB_PORT -h $EDB_HOST -U $EDB_USER $EDB_NAME -c "
select * from tapes_recycled_last_7days() order by media_type;
" >> $temp_file 2>&1

echo                                 >> $temp_file 2>&1
echo "=============================" >> $temp_file 2>&1
echo "Bytes recycled in last 7 days" >> $temp_file 2>&1
echo "=============================" >> $temp_file 2>&1
echo                                 >> $temp_file 2>&1

echo "Creating Bytes recycled in last 7 days" `date`

psql -p $EDB_PORT -h $EDB_HOST -U $EDB_USER $EDB_NAME -c "
select v.storage_group, sum(v.deleted_bytes+v.unknown_bytes+v.active_bytes) as recycled_bytes from state s, \
volume v where v.id=s.volume and s.value='RECYCLED' and s.time>=current_date-8 and s.time<current_date group by v.storage_group"
 >> $temp_file 2>&1

echo                    >> $temp_file 2>&1
echo "================" >> $temp_file 2>&1
echo "Remaining blanks" >> $temp_file 2>&1
echo "================" >> $temp_file 2>&1
echo                    >> $temp_file 2>&1

echo "Creating Remaining blanks" `date`

psql -p $EDB_PORT -h $EDB_HOST -U $EDB_USER $EDB_NAME -c "
select * from remaining_blanks order by media_type;
" >> $temp_file 2>&1

echo                               >> $temp_file 2>&1
echo "===========================" >> $temp_file 2>&1
echo "Blanks drawn in last 7 days" >> $temp_file 2>&1
echo "===========================" >> $temp_file 2>&1
echo                               >> $temp_file 2>&1

echo "Creating Blanks drawn in last 7 days" `date`

psql -p $ADB_PORT -h $ADB_HOST -U $ADB_USER $ADB_NAME -c "
select * from blanks_drawn_last_7days() order by media_type;
" >> $temp_file 2>&1


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