#!/bin/bash

. /afs/usatlas.bnl.gov/scripts/setup_cmt_usatlas.sh $Release gcc

TestRelease=`ls -ort TestRelease/ | tail -1 | awk '{print $8}' | cut -d- -f2-`
cd TestRelease/TestRelease-$TestRelease/cmt

cmt broadcast cmt config
source setup.sh

cd ../run

athb=`grep  atlfast....hbook jobOptions.txt | awk '{print $3}'`
if [ -n "$athb" ] && [ -l $athb -gt 16 ] ; then

while [ -f jobOptions.lock ]
do
    sleep 1
done
touch jobOptions.lock

oc=`grep atlfast....hbook jobOptions.txt | awk '{print $3}' | cut -c10-11`
nc=`expr $oc + 1`
[ `expr length $nc` -eq 1 ] && nc=0$nc

echo `date`; echo $nc; ls -ao --full-time jobOptions.lock

hbofile=atlfast.$oc.hbook
ntofile=atlfast.$oc.ntup
ipofile=isapar.$oc.dat
ilofile=isajet.$oc.lis
hbnfile=atlfast.$nc.hbook
ntnfile=atlfast.$nc.ntup
ipnfile=isapar.$nc.dat
ilnfile=isajet.$nc.lis
[ -f $ilnfile ] && rm $ilnfile

awk '{ gsub( "'$hbofile'", "'$hbnfile'" );  gsub( "'$ntofile'", "'$ntnfile'" ); ;  gsub( "'$ipofile'", "'$ipnfile'" ); gsub( "'$ilofile'", "'$ilnfile'" ); print }' jobOptions.txt > jobOptions.txt.tmp; mv -f jobOptions.txt.tmp jobOptions.txt

(sleep 4; echo `date`;ls -ao --full-time jobOptions.lock; rm -f jobOptions.lock) &

fi

`alias athena | cut -d= -f2- | cut -d\' -f2`

cd ../../..
