#!/bin/bash
#
# ========================================================================
#
# *
# * 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-2008
#
#
# ========================================================================
#
# 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 [ -f ${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`
   type=`echo $line | cut -d_ -f7 | cut -c0-5`
   # only check muxdata rawfiles, not cc-, caco- or drivelog-files
   if [ "$muxchk" = "muxdata" ] && [ "$type" = "E.raw" ]
   then
      sum=($line)
      
      # check if the file has been extracted from tape; if not, don't write anything to the logfiles (so they can still be compared)
      if [ ! -f /magic/datacenter/fromtape/${sum[0]} ] && [ ! -f /data/fromtape/${sum[0]} ]
      then
      	echo "File ${sum[0]} does not exist."
        continue
      fi
      
      # reformat the files and checksums correctly and compare them
      echo "${sum[1]}  ${sum[0]}" >> ${checkpath}/tape_${id}.md5.lapalma
      
      if [ ! -f /magic/datacenter/fromtape/${sum[0]} ]
      then
         ssh -nx phoenix nice -n 19 /opt/csw/bin/gmd5sum /data/fromtape/${sum[0]} | sed -e 's/\/data\/fromtape\///' >> ${checkpath}/tape_${id}.md5.wue
      else
         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
   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! Launching movingrawfiles now."# to be included
else
   echo -e "The files are not identical! diff found the following differences:\n$output"
fi

echo "program terminated."
