#!/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 12/2007 <mailto:snruegam@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2007
#
#
# ========================================================================
#
# This script is removing the switching noise out of raw files.
# Corresponding information will be inserted into the database. The noise
# removal is being done by the script 'compmux'.
#

source `dirname $0`/sourcefile
printprocesslog "INFO starting $0"
echo "This script has not been adapted to the new file structure (MAGIC II). "
echo "Please adapt it before using it."
exit

program=compmux
column=fCompmux

set -C

scriptlog=/magic/datacenter/compmux/$program-$datetime.log
date >> $scriptlog 2>&1
todofile=$listpath/ToDo-$table-$column.txt

if [ -e $todofile ]
then 
   echo "$program is already running -> exit" >> $scriptlog 2>&1
   exit
fi

#get todo list
getdolist >> $scriptlog 2>&1

#retrieving runs from todo file
files=(`cat $listpath/ToDo-$table-$column.txt`)
if [ "$files" = "" ]
then 
   echo "nothing to do -> exit" >> $scriptlog 2>&1
   finish >> $scriptlog 2>&1
fi

for file in ${files[@]}
do
   echo -e "\ngetting path for run $file..." >> $scriptlog 2>&1
   fullpath=`find $datapath/rawfiles/2007/0[2-9] -name *${file}_[DCPS]_*`
   
   lockfile=$lockpath/lock-$program-$file.txt
   
   #check lock file and stop for this run if already being processed
   checklock continue >> $scriptlog 2>&1
   
   primvar=$file
   setstatus "start" >> $scriptlog 2>&1
   
   cd $mars
   
   #cut the slices
   gzip -cd $fullpath | $mars/datacenter/compmux | gzip -1c > /tmp/temp${file}.gz
   check1=$?
   touch -r $fullpath /tmp/temp${file}.gz
   
   #check success and insert information into the database
   case $check1 in
      0)   echo " check1=$check1 -> everything ok -> compmux succeeded, overwriting old file for run $file" >> $scriptlog 2>&1
           mv -f /tmp/temp${file}.gz $fullpath >> $scriptlog 2>&1
           check2=$?
           chmod a=r $fullpath
           case $check2 in
              0)   echo " check2=$check2 -> everything ok -> mv done, setting db status for run $file" >> $scriptlog 2>&1
                   rm -v $lockfile >> $scriptlog 2>&1
                   ;;
              *)   echo " check2=$check2 -> ERROR - moving of run $file failed -> continue" >> $scriptlog 2>&1
                   continue
                   ;;
           esac
           ;;
      *)   echo " check1=$check1 -> ERROR - $program failed for run $file" >> $scriptlog 2>&1
           com=$FCompmux
           check=$check1
           ;;
   esac
   
   setstatus "stop" >> $scriptlog 2>&1
done
