#!/bin/sh
#
# start.sh
#
# Script to submit a QPipeline analysis

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

# $Id: start.sh,v 1.1 2006/10/05 20:53:12 shourov Exp $

# change to analysis directory
cd `dirname $0`

# if analysis was run previously
if [ -f start.log ]; then
  echo "ERROR: There are previous logs.  Run the clean up script first." 1>&2
  exit 1
# end test for previous run
fi

# name of dag file
dagFile=`/bin/ls -1 *.dag [0-9]*/*.dag 2>/dev/null | \
         sed -e 's|^.*/||' | tail -1`

# test for dag file
if [ -z "${dagFile}" ]; then
  echo "ERROR: There is no dag file." 1>&2
  exit 1
fi
if [ ! -f "${dagFile}" ]; then
  echo "ERROR: The analysis is already complete." 1>&2
  exit 1
fi

# condor submit file name
condorSubmitFile=`basename ${dagFile} .dag`.sub

# condor log file name
condorLogFile=`grep '^log' ${condorSubmitFile} | sed -e 's|^.*=[ ]*||'`

# setup condor environment
CONDOR_LOCATION=/ldcg/condor
export CONDOR_LOCATION
CONDOR_CONFIG=${CONDOR_LOCATION}/etc/condor_config
export CONDOR_CONFIG
PATH=${CONDOR_LOCATION}/bin:${CONDOR_LOCATION}/sbin:${PATH}
export PATH

# test for condor
condor_q 0 >/dev/null 2>&1
if [ $? -ne 0 ]; then
  echo "ERROR: Condor is not available." 1>&2
  exit 1
fi

# create directory for condor log file
mkdir -p `dirname ${condorLogFile}`

# create local link to condor log file
ln -s ${condorLogFile} .

# create local log file structure
mkdir -p log/stdout
mkdir -p log/stderr
mkdir -p log/status
mkdir -p log/locate
mkdir -p results

# submit the analysis
condor_submit_dag -maxidle 20 -notification Never ${dagFile} >start.log 2>&1

# return condor_submit_dag exit status
exit $?

