#!/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  09/2006 <mailto:dorner@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2007
#
#
# ========================================================================
#
# This script zips the incoming rawfiles. 
#

source `dirname $0`/sourcefile
printprocesslog "INFO starting $0"
# to give the possibility to run several zipscripts at the same time, 
# $1 is added
program=zipscript$1

set -C

rawdatapath=$datapath/rawfiles/
files=`find ${rawdatapath}20* -type f -name '*.raw'`

scriptlog=$runlogpath/$program-$datetime.log
date >> $scriptlog 2>&1

# check if script is already running
lockfile=$lockpath/lock-$program.txt
checklock  >> $scriptlog 2>&1


if [ "$files" == "" ]
then
   echo "no files to zip" >> $scriptlog 2>&1
   finish >> $scriptlog 2>&1
fi

for file in ${files[@]}
do
   echo "processing file $file..." >> $scriptlog 2>&1 
   runno=`basename $file | cut -d_ -f2`
   query="SELECT fSequenceFirst FROM RunData WHERE fRunNumber="$runno
   echo " sending query: $query" >> $scriptlog 2>&1 
   if ! seq=`sendquery`
   then
      echo "querying seq for run $runno from the db did not work -> continue" >> $scriptlog 2>&1
      printprocesslog "WARN query $seq for run $runno from the db did not work"
      continue
   fi
   if [ "$seq" == "" ] 
   then
      echo "no seq found for run $runno -> locking only run..." >> $scriptlog 2>&1
      seq=$runno
   fi
   echo "locking sequence $seq..."  >> $scriptlog 2>&1 
   lockfile=$lockpath/calzip$seq.txt
   checklock continue >> $scriptlog 2>&1
   query="UPDATE SequenceProcessStatus set fNotZipping=NULL WHERE fSequenceFirst=$seq"
   echo " sending query: $query" >> $scriptlog 2>&1 
   if ! sendquery 
   then
      echo "locking $seq for callisto in db did not work ..." >> $scriptlog 2>&1
      printprocesslog "WARN locking $seq for callisto in db did not work"
      rm -v $lockfile >> $scriptlog 2>&1
      continue
   fi
   echo "zipping $file ..." >> $scriptlog 2>&1
   if ssh -xn phoenix nice -n 19 gzip -1f $file >> $scriptlog 2>&1
   then 
      gzfile=$file".gz"
      chmod a-w $gzfile >> $scriptlog 2>&1
   else
      printprocesslog "WARN zipping $file did not work"
   fi
   echo "unlocking sequence $seq..."  >> $scriptlog 2>&1 
   query="UPDATE SequenceProcessStatus set fNotZipping=Now() WHERE fSequenceFirst=$seq"
   echo " sending query: $query" >> $scriptlog 2>&1 
   if ! sendquery 
   then
      echo "unlocking $seq for callisto in db did not work ..." >> $scriptlog 2>&1
      printprocesslog "ERROR unlocking $seq for callisto in db did not work"
      rm -v $lockfile >> $scriptlog 2>&1
      continue
   fi
   rm -v $lockfile >> $scriptlog 2>&1
done

lockfile=$lockpath/lock-$program.txt
finish >> $scriptlog 2>&1

