#! /bin/sh
#
# Shows the output of a Chiba BATCH run that is still running
# 
command=cat
file=OU
if [ $# -gt 0 ] ; then
  if [ $1 = "-h" ] ; then 
    echo "Displays the output of a running Chiba batch job"
    echo "Usage: chibaoutput [-f] [-e] [pbs_job_number]"
    echo "-f makes it run like tail -f, e shows stderr instead of stdout"
    exit 0
  fi
  if [ $1 = "-f" ] ; then 
    command="tail -f"
    shift
  fi
fi
if [ $# -gt 0 ] ; then
  if [ $1 = "-e" ] ; then 
    file=EU
    shift
  fi
fi
if [ $# -gt 0 ] ; then
  PBS_JOBID=$1
else 
  name=`ls -t ~/.pbs* | head -1`
  PBS_JOBID=`expr $name : '/homes/bsmith/.pbsnodefile.\([0-9]*\).*'`
fi

pbsfile=${HOME}/.pbsnodefile.${PBS_JOBID}
node=`head -2 $pbsfile.* | tail -1 | cut -d" " -f1` 
rsh $node $command /var/spool/pbs/spool/$PBS_JOBID.ccsche.$file
