#!/bin/bash
echo "Please specify the tape number (e.g. 3MG005L3)."
read no
echo "Thank you. Your query is being processed. This may take some time."

checkpath=/home/lapalma/tapecont/cont/muxdata/checksums
file=/home/lapalma/tapecont/cont/muxdata/tape_${no}.md5
if [ ! -f $file ]
then
	echo "tape $file not found."
        exit
fi

date | tee ${checkpath}/tape_${no}.md5.lapalma | tee ${checkpath}/tape_${no}.md5.wue

# read the tapecont file line by line
while read line
do
	muxchk=`echo $line | cut -d/ -f1`
        # only check muxdata, not cc-, caco- or drivelog-files
        if [ "$muxchk" = "muxdata" ]
        then
		sum=($line)
                # reformat the files and checksums correctly and compare them
	        echo "${sum[1]}  ${sum[0]}" >> ${checkpath}/tape_${no}.md5.lapalma
		
         	ssh -nx phoenix nice -n 19 /opt/csw/bin/gmd5sum /magic/datacenter/fromtape/${sum[0]} | sed -e 's/\/magic\/datacenter\/fromtape\///' >> ${checkpath}/tape_${no}.md5.wue
                #md5sum /magic/datacenter/fromtape/${sum[0]} | sed -e 's/\/magic\/datacenter\/fromtape\///' >> ${checkpath}/tape_${no}.md5.wue
        fi
done < $file

date | tee -a ${checkpath}/tape_${no}.md5.lapalma | tee -a ${checkpath}/tape_${no}.md5.wue

if [ ! -f ${checkpath}/tape_${no}.md5.lapalma ] || [ ! -f ${checkpath}/tape_${no}.md5.wue ]
then
	echo "No output files written! Something went wrong..."
        exit
fi

echo "Wrote checksums to ${checkpath}/tape_${no}.md5.lapalma and ${checkpath}/tape_${no}.md5.wue. Comparing now."


# check for differences in the checksums
output=`diff ${checkpath}/tape_${no}.md5.lapalma ${checkpath}/tape_${no}.md5.wue`

if [ "$output" = "" ]
then
	echo "All checksums are ok!"
else
	echo -e "The files are not identical! diff found the following differences:\n$output"
fi

echo "program terminated."
