#!/bin/sh
#
# ========================================================================
#
# *
# * This file is part of MARS, the MAGIC Analysis and Reconstruction
# * Software. It is distributed to you in the hope that it can be a useful
# * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
# * It is distributed WITHOUT ANY WARRANTY.
# *
# * Permission to use, copy, modify and distribute this software and its
# * documentation for any purpose is hereby granted without fee,
# * provided that the above copyright notice appear in all copies and
# * that both that copyright notice and this permission notice appear
# * in supporting documentation. It is provided "as is" without express
# * or implied warranty.
# *
#
#
#   Author(s): Stefan Ruegamer 05/2007 <mailto:snruegam@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2007
#
#
# ========================================================================
#
# This script is checking the md5 sums of files read from tape. It
# computes the checksums for the extracted files and compares them with
# the checksums from La Palma, read out from the md5 sum file which is
# transferred together with the logfile.
#
# The script will be automised by being executed after reading tapes with
# the not-yet finished script readlapalmatapes. At the moment it is being
# executed by hand.
#

echo "Please specify the tape number (e.g. 3MG005)."
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}L*.md5
L=`echo $file | cut -c49`
id=${no}L${L}

if [ ! -f $file ]
then
	echo "tape $file not found -> aborting"
        exit
fi

if [ ${checkpath}/tape_${id}.md5.lapalma ] || [ -f ${checkpath}/tape_${id}.md5.wue ]
then
	echo "Output files already existing. Are you sure you wish to continue (yes/no)?"
        read cont
        if [ ! $cont == "yes" ]
        then
        	echo "Aborting."
	        exit
        fi
fi
        
date | tee ${checkpath}/tape_${id}.md5.lapalma | tee ${checkpath}/tape_${id}.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_${id}.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_${id}.md5.wue
                #md5sum /magic/datacenter/fromtape/${sum[0]} | sed -e 's/\/magic\/datacenter\/fromtape\///' >> ${checkpath}/tape_${id}.md5.wue
        fi
done < $file

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

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

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


# check for differences in the checksums
output=`diff ${checkpath}/tape_${id}.md5.lapalma ${checkpath}/tape_${id}.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."
