#!/bin/sh
#
# cache_ego_cluster.sh
#
# Shell script to create frame cache files for data stored
# on the EGO computing cluster.

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

# $Id: cache_ego_cluster.sh,v 1.3 2007/08/28 12:06:17 shourov Exp $

# report current time
date +"%Y-%m-%d %H:%M:%S %Z" -u

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

# check for previously running process
if [ -f ./.lock ]; then
  lockHostname=`sed -e 's|:.*$||' ./.lock`
  lockProcess=`sed -e 's|^.*:||' ./.lock`
  if [ "${lockHostname}" != "`hostname`" ]; then
    echo "ERROR: `basename $0` is already running on ${lockHostname}" 1>&2
    exit 1
  fi
  ps -p ${lockProcess} -o pid,user,cmd 2>/dev/null | \
    grep ${lockProcess} | grep ${USER} | grep -q `basename $0`
  if [ $? -eq 0 ]; then
    echo "ERROR: `basename $0` is already running" 1>&2
    exit 1
  fi
  rm -f ./.lock
fi

# set lock file
echo `hostname`:$$ >./.lock

# list of frame cache files to process
caches="raw HrecOnline FullBW"

# begin loop over data sets
for cache in ${caches}; do

  # report status
  # echo "processing ${cache}..."

  # create framecache for this data set
  ../bin/convertfflcache.pl \
    ${cache}.txt.tmp \
    /virgoData/ffl/${cache}.ffl

  # handle errors
  if [ $? -ne 0 ]; then
    echo "ERROR: could not convert ${cache} framecache." 1>&2
    rm -f ${cache}.txt.tmp
    continue;
  fi

  # replace previous frame cache file
  mv ${cache}.txt.tmp .

# end loop over data sets
done

# clear lock file
rm -f ./.lock

# return to calling function
exit 0

