#!/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-2006
#
#
# ========================================================================
#
# This script zips the incoming rawfiles. 
#

source `dirname $0`/sourcefile
printprocesslog "INFO starting $0"
program=zipscript

set -C

rawdatapath=$datapath/rawfiles
files=`find $rawdatapath -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 "zipping $file ..." >> $scriptlog 2>&1
   if ssh pegasus nice -n 19 gzip -1f $file >> $scriptlog 2>&1
   then 
      chmod a-w $file >> $scriptlog 2>&1
   fi
done

finish >> $scriptlog 2>&1



# old code for running script locally

#rawdatapath=$datapath/rawfiles
##h:     0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#pnos=( 2 2 2 2 2 2 2 2 2 2  2  2  2  2  2  2  2  2  2  2  2  2  2  2 )
#sl1=30 #sleeping time before new directory
#sl2=60 #sleeping time before new process check
#
#dirs=`find $rawdatapath -type d | grep -v timecorrlogs | sort`
##dirs=`find $rawdatapath -type d | grep -v timecorrlogs | sort -r`
#
#for dir in ${dirs[@]}
#do
#   cont=`echo $dir | cut -d/ -f7`
#   if [ "$cont" == "" ]
#   then
#      continue
#   fi
#
#   if ! ls $dir/*.raw >/dev/null 2>&1
#   then
#      echo "no unzipped rawfiles in $dir left"
#      continue
#   fi
#
#   echo "sleeping $sl1..."
#   sleep $sl1
#
#   h=`date +%k`
#   count=`ps aux | grep gzip | grep -v grep | wc -l`
#   echo "count: $count - h: $h - pno: ${pnos[$h]}"
#   while [ "$count" -ge "${pnos[$h]}" ]
#   do
#      echo "sleeping $sl2..."
#      sleep $sl2
#      h=`date +%k`
#      count=`ps aux | grep gzip | grep -v grep | wc -l`
#      echo "count: $count - h: $h - pno: ${pnos[$h]}"
#   done
#   echo $dir
#   du -hs $dir
#   echo "gzip -1f $dir/*.raw &"
#   gzip -1f $dir/*.raw &
#done
#


# old code for running script with condor

#rawdatapath=$datapath/rawfiles
##h:     0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#pnos=( 2 2 2 2 2 2 2 2 2 2  2  2  2  2  2  2  2  2  2  2  2  2  2  2 )
#sl1=30 #sleeping time before new directory
#sl2=60 #sleeping time before new process check
#
#dirs=`find $rawdatapath -type d | grep -v timecorrlogs | sort`
##dirs=`find $rawdatapath -type d | grep -v timecorrlogs | sort -r`
#
#for dir in ${dirs[@]}
#do
#   cont=`echo $dir | cut -d/ -f7`
#   if [ "$cont" == "" ]
#   then
#      continue
#   fi
#
#   if ! ls $dir/*.raw >/dev/null 2>&1
#   then
#      echo "no unzipped rawfiles in $dir left"
#      continue
#   fi
#
#   echo "sleeping $sl1..."
#   sleep $sl1
#
#   h=`date +%k`
#   count=`grep gzip .condor_run.* | wc -l`
#   echo "count: $count - h: $h - pno: ${pnos[$h]}"
#   while [ "$count" -ge "${pnos[$h]}" ]
#   do
#      echo "sleeping $sl2..."
#      sleep $sl2
#      h=`date +%k`
#      count=`grep gzip .condor_run.* | wc -l`
#      echo "count: $count - h: $h - pno: ${pnos[$h]}"
#   done
#   echo $dir
#   du -hs $dir
#   condor_run gzip -1f $dir/*.raw &
#
#done
#

