#! /bin/sh

#
#Performs a (reverse) scan of the file database.
#

#Use the current date to name the directory were all of the output from
# the scan will go.
start_date=`date "+SCAN_%m_%d_%Y"`
output_dir=$start_date  #For readablility.
if [ ! -d $output_dir ]; then
	mkdir -p $output_dir
	if [ $? -ne 0 ]; then
		echo "Unable to make scan output directory." 1>&2
		exit 1
	fi
fi

#Name the file that the status information will go to.
scan_status_file=$output_dir/reverse_scan.status
rm -f $scan_status_file
if [ $? -ne 0 ]; then
	exit 1
fi
touch $scan_status_file
if [ $? -ne 0 ]; then
	exit 1
fi

#If nothing was specified on the command line; perform a scan on all enstore
# storage groups.  Otherwise use the contents of the file(s) mentioned.
if [ -z $1 ]; then
	use_file=$output_dir/.all_storage_groups
	storage_groups > $use_file
else
	use_file=$*
fi

#Obtain the current file counts.
file_counts_file=$output_dir/.file_counts
enstore info --query "
select storage_group, sum(active_files+unknown_files+deleted_files)
from volume 
where volume.system_inhibit_0 != 'DELETED'
group by storage_group;
" 2>> $scan_status_file  | sed '1,2d' > $file_counts_file

echo "\$ENSTORE_CONFIG_HOST = $ENSTORE_CONFIG_HOST"
echo "Starting:" `date` | tee -a $scan_status_file

#Loop over all storage groups.
cat $use_file |
while read start_point
do
	echo "Starting to scan" $start_point at `date`
	#reverse scan

	#Strip out just the storage group.
	storage_group=`echo $start_point | tr -s " " " "`
	#Get the list of all of the volumes for this storage group.
	volumes=`enstore info --query "select label from volume where storage_group='$storage_group';" 2>> $scan_status_file | sed '1,2d' | awk '{print $1}' | grep -v deleted`

	#Report on the number of files this storage group has.
	file_count=`grep $storage_group $file_counts_file | awk '{print $2}'`
	if [ -z "$file_count" ]; then 
		file_count=0;
	fi
	# For groups like SDSS that have storage groups sdss and sdss_apo
	#  we need to sum up the totals.
	sum=0
	for item in `echo $file_count`; do let "sum = $sum + $item"; done
	file_count=$sum

	#Print the info to the screen.
	echo The \'$storage_group\' storage group has $file_count files to check. | tee -a $scan_status_file

	#This isn't a lock file per say, but more like an active file.
	lock_file=$output_dir/.$storage_group
	#This is the file we will be outputing into.
	output_file=$output_dir/${storage_group}_reverse

	#Start a progress monitor in the background.
	if [ "$file_count" -gt 0 ]; then
		touch $lock_file
		(sleep 3600;
		 while [ -f $lock_file ]; do 
			progress=`wc -l $output_file | awk '{print $1}'`;
			let "percent = $progress * 100 / $file_count"
			echo ${percent}% percent done with \'$storage_group\' at `date`.;
			sleep 3600; 
		 done) & 
	fi

	#Remember the volumes.  This makes debugging and restarting easier.
	volume_file=$output_dir/.volumes_${storage_group}
	echo "$volumes" > $volume_file

	#Include options for running the scan.
        scan_options=--threaded

	#Echo what we are doing.
	echo "time enstore scan --vol for storage group $storage_group" | tee -a $scan_status_file
	#Time the scan.  Send scan output to scan output file;
	# send time output to the scan.status file.
	(time echo "$volumes" | enstore scan $scan_options --vol 1> $output_file) 2>&1 | tee -a $scan_status_file
	#Echo a blank line for readability.
	echo | tee -a $scan_status_file

	#Tell the monitoring process to stop.
	rm -f $lock_file

	#Pull out just the errors and warnings.
	grep " ... ERROR$" $output_file > ${output_file}.error
	grep " ... WARNING$" $output_file > ${output_file}.warning
done

echo "Finished:" `date` | tee -a $scan_status_file

