#!/bin/bash
#  
#   First we need to get into the main directory
#
if [[  ! -d ./bin  ]]; then
    cd ../
    if [[ ! -d ./bin ]]; then
	echo "Error: survey must be executed from the main, or bin directory"
	exit
    fi
fi
#
#  Now let shell know where to find important executables
#
main=`pwd`
dirbin=$main/bin/internal
# Begin Local versions
# set dirbin = /home/tstelzer/bin/MG_ME
# End Local versions 
cd SubProcesses
t=`find ./ -maxdepth 2 -name "ajob*" |wc -l`
r=`find ./ -maxdepth 2 -name "run.ajob*" |wc -l`
w=`find ./ -maxdepth 2 -name "wait.ajob*" |wc -l`
d=`find ./ -maxdepth 2 -name "done.ajob*" |wc -l`

echo "waiting while finishing jobs on cluster"
while [[  $d -lt $t  ]]; do    
    echo $w $r $d $t
    rm -f ../status
    echo $w $r $d $t > ../status
    $dirbin/gen_crossxhtml-pl $1
    sleep 10
    t=`find ./ -maxdepth 2 -name "ajob*" |wc -l`
    r=`find ./ -maxdepth 2 -name "run.ajob*" |wc -l`
    w=`find ./ -maxdepth 2 -name "wait.ajob*" |wc -l`
    d=`find ./ -maxdepth 2 -name "done.ajob*" |wc -l`
done
echo $w $r $d $t

# Extra sleep to allow time to copy files over network
sleep 20

rm -f ../running_jobs
