#! /bin/sh

# $Id$

#Dump the duplication information.

#Get all the common stuff for migration, duplication and cloning reporting.
mig_com=`which migration_common 2> /dev/null`
if [ -z $mig_com ]; then
    #When run interactively, bash was picking the wrong version of
    # migration_common.  Even when 'which' was able to find the correct
    # one with the same environment.
    #
    #If 'which' didn't find it, next try the path of this script.

    mig_com=`dirname $0`/migration_common
    if [ ! -x "$mig_com" ]; then
        #If this fails, fall back to the old way.
	mig_com=migration_common
    fi
fi
source $mig_com

#Create the variables that point to the files to output to.
fname=DUPLICATION_SUMMARY
old_output_file=$inventory_dir/$fname  #ASCII and HTML were both once created.
output_file=$inventory_dir/$fname.html
temp_file=$temp_dir/$fname$$.temp

#Remove the temporary file on failure.
trap "rm -f $temp_file; exit 1" HUP INT QUIT ABRT ALRM TERM

#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 $old_output_file

#Make sure we know how up-to-date this is.
echo -e Duplication 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 "===================" >> $temp_file 2>&1
echo "Duplication Summary" >> $temp_file 2>&1
echo "===================" >> $temp_file 2>&1
echo                       >> $temp_file 2>&1

echo "Creating Duplication Summary" `date`

echo "In the following table:" >> $temp_file 2>&1
echo "migration_closed_not_shelved + migration_closed_shelved + duplication_closed_not_shelved + duplication_closed_shelved + remaining_to_scan_and_close + blank_volumes + cloning_volumes + multiple_copy_volumes = total" >> $temp_file 2>&1

summary >> $temp_file 2>&1

echo >> $temp_file 2>&1
echo >> $temp_file 2>&1

echo "The volumes counted in the following table are also included in the previous table." >> $temp_file 2>&1

summary_skipped_bad_files >> $temp_file 2>&1

echo                       >> $temp_file 2>&1
echo "===================" >> $temp_file 2>&1
echo "Duplication per Day" >> $temp_file 2>&1
echo "===================" >> $temp_file 2>&1
echo                       >> $temp_file 2>&1

echo "Creating Duplication per Day" `date`

duplication_per_day >> $temp_file 2>&1

echo                                   >> $temp_file 2>&1
echo "===============================" >> $temp_file 2>&1
echo "Duplication/Migration Remaining" >> $temp_file 2>&1
echo "===============================" >> $temp_file 2>&1
echo                                   >> $temp_file 2>&1

echo "Creating Duplication/Migration Remaining" `date`

volumes_remaining >> $temp_file 2>&1

echo                       >> $temp_file 2>&1
echo "===================" >> $temp_file 2>&1
echo "Duplication History" >> $temp_file 2>&1
echo "===================" >> $temp_file 2>&1
echo                       >> $temp_file 2>&1

echo "Creating Duplication History" `date`

duplication_history >> $temp_file 2>&1


#Turn out the html page.
make_html "$tl_name Duplication Summary Page" $temp_file $output_file $url_dir

#Remove the temp file.
rm -f $temp_file
