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

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

# $Id: cache_uwm_cluster.sh,v 1.4 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="S4 S5 VSR1 WSR8"

# list of disks to process
disks="01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24"

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

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

  # create temporary directory
  rm -rf tmp
  mkdir -p tmp

  # begin loop over disks
  for disk in ${disks}; do

    # create framecache for this data set and disk
    ../bin/createframecache.pl \
      tmp/${cache}.txt.${disk} \
      /nfsdata/nfsdata${disk}/${cache} \
      >/dev/null

  # end loop over disks
  done

  # concatenate and sort frame cache data from disks
  cat tmp/${cache}.txt.* | sort >tmp/${cache}.txt

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

  # remove temporary frame cache data
  rm -rf tmp

# end loop over data sets
done

# clear lock file
rm -f ./.lock

# return to calling function
exit 0

