#!/bin/sh
#
# start.sh
#
# Start online analysis of science mode data.

# Shourov K. Chatterji
# shourov@ligo.caltech.edu

# $Id: start.sh,v 1.8 2007/06/09 12:27:05 shourov Exp $

# change to directory containing this script
cd `dirname $0`

# path to Q transform installation
Q=../..

# setup environment
. ${Q}/bin/qsetup.sh

# current directory
currentDirectory=`pwd`

# temporary crontab file
crontabFile="crontab.txt"

# initialize temporary crontab file
rm -f ${crontabFile}
touch ${crontabFile}

# determine offest from UTC
currentTime=`tconvert now`
utcOffset=`tconvert -l -f %z ${currentTime} | \
           sed -e 's|00$||' -e 's|\+||' -e 's|^0||'`
if [ ${utcOffset} -lt 0 ]; then
  utcOffset=`expr 24 + ${utcOffset}`
fi

# UTC hour of update
utcHour=0

# determine local hour of update
localHour=`expr \( ${utcHour} + ${utcOffset} \) % 24`

# intialize log file
rm -f log.txt
touch log.txt

# create temporary crontab file
echo 'MAILTO=""' >>${crontabFile}
echo 'SHELL=/bin/sh' >>${crontabFile}
echo '00 * * * * '${currentDirectory}'/process.sh minutes >>'${currentDirectory}'/log.txt 2>&1' >>${crontabFile}
echo '10 * * * * '${currentDirectory}'/process.sh minutes >>'${currentDirectory}'/log.txt 2>&1' >>${crontabFile}
echo '20 * * * * '${currentDirectory}'/process.sh minutes >>'${currentDirectory}'/log.txt 2>&1' >>${crontabFile}
echo '30 * * * * '${currentDirectory}'/process.sh minutes >>'${currentDirectory}'/log.txt 2>&1' >>${crontabFile}
echo '40 * * * * '${currentDirectory}'/process.sh minutes >>'${currentDirectory}'/log.txt 2>&1' >>${crontabFile}
echo '50 * * * * '${currentDirectory}'/process.sh minutes >>'${currentDirectory}'/log.txt 2>&1' >>${crontabFile}
echo '25 * * * * '${currentDirectory}'/process.sh hour    >>'${currentDirectory}'/log.txt 2>&1' >>${crontabFile}
echo '45 '${localHour}' * * * '${currentDirectory}'/process.sh day     >>'${currentDirectory}'/log.txt 2>&1' >>${crontabFile}

# retrieve existing crontab
crontab -l 2>/dev/null | \
  sed -e "\|${currentDirectory}|d" -e "/SHELL/d" -e "/MAILTO/d" \
  >>${crontabFile}

# start crontab
crontab ${crontabFile}

# remove temporary crontab file
rm ${crontabFile}

