#!/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): Daniela Dorner  10/2006 <mailto:dorner@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2006
#
#
# ========================================================================
#
# This script checks from the DB which new starfiles have been produced 
# and sends it to the person in charge for building datasets
#

#source `dirname $0`/sourcefile
#printprocesslog "INFO starting $0"
function usage()
{
   echo "Usage: $0 [options]"
   echo "options:"
   echo -n "  --sequ sequence#       "
   echo -n " giving number of the sequence starting from which "
   echo " sequences with star done are returned "
   echo -n "  --date                 "
   echo -n " giving date starting from which sequences with star "
   echo "done are returned"
   echo ""
   exit
}


while [ "$1" ]
do 
   case $1 in 
      --sequ)  shift
               seq=$1
               echo "Only sequences starting from sequence # $seq are considered"
               ;;
      --date)  shift
               date=$1
               echo "Only star starting from date $date is considered"
               ;;
          -h)  usage
               ;;
           *)  echo "unknown option $1 "
               usage
               exit
               ;;
   esac
   shift
done

alias mymysql='mysql -u MAGIC --password=d99swMT! --host=hercules MyMagic -s -e'

query="SELECT fSequenceFirst FROM SequenceProcessStatus WHERE NOT ISNULL(fStar) "
cond=""
if ! [ "$seq" = "" ]
then 
   cond=$cond" AND SequenceProcessStatus.fSequenceFirst > $seq " 
fi
if ! [ "$date" = "" ]
then
   cond=$cond" AND fStar > '$date'"
fi 
query=$query$cond

sequences=`mymysql " $query "`
if [ "$sequences" = "" ]
then 
   echo "no sequences with new star files found "
   exit
fi 
sequences2=`echo $sequences | sed -e 's/ /,/g'`
query="SELECT fSourceKEY FROM Sequences WHERE fSequenceFirst IN ($sequences2) GROUP BY fSourceKEY"
sources=`mymysql " $query "`
sources2=`echo $sources | sed -e 's/ /,/g'`
query="SELECT fSourceName FROM Source WHERE fSourceKEY IN ($sources2)"
sourcenames=`mymysql " $query "`
#echo "for the following sources new starfiles have been produced: $sourcenames"
#echo ""
for sourcename in $sourcenames
do 
   query="SELECT Sequences.fSequenceFirst FROM Sequences "
   query=$query" LEFT JOIN Source ON Source.fSourceKEY=Sequences.fSourceKEY "
   query=$query" LEFT JOIN SequenceProcessStatus ON SequenceProcessStatus.fSequenceFirst=Sequences.fSequenceFirst "
   query=$query"WHERE fSourceName='$sourcename'"
   query=$query$cond
   seqspersource=`mymysql " $query "`
   seqs=`echo $seqspersource | sed -e 's/ /,/g'`
   printf "star done for %12s the following new sequences: %s \n" $sourcename $seqs
done

for sourcename in $sourcenames
do 
   echo "searching datasets for $sourcename"
   if ! grep $sourcename /magic/test/bu.2006.10.10/datasets/*/*.txt 2>/dev/null
   then
      echo "sorry, no dataset found for $sourcename, please create a new dataset"
   fi
done


#nail -s 'found warnings in '$oldprocesslog $erradrs

#printprocesslog "INFO finished $0"

