#!/bin/ksh

#/* Part of the Maestro sequencer software package.
# * Copyright (C) 2011-2015  Operations division of the Canadian Meteorological Centre
# *                          Environment Canada
# *
# * Maestro is free software; you can redistribute it and/or
# * modify it under the terms of the GNU Lesser General Public
# * License as published by the Free Software Foundation,
# * version 2.1 of the License.
# *
# * Maestro is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# * Lesser General Public License for more details.
# *
# * You should have received a copy of the GNU Lesser General Public
# * License along with this library; if not, write to the
# * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# * Boston, MA 02111-1307, USA.
# */


arguments=$*

eval `cclargs $0 \
  -n        ""                    ""                           "[Node         ]" \
  -h        ""                    ""                           "[Display help       ]" \
  -list     "0"                   "1"                          "[Show available batch files         ]" \
  -l        ""                    ""                           "[Loop argument list ]" \
  ++ $arguments`

if [ ! -d "${SEQ_EXP_HOME}" ]; then
   echo "nodebatch error: Invalid path SEQ_EXP_HOME=$SEQ_EXP_HOME"
   exit 1
fi

if [[ -n "${SEQ_BIN}" ]] ; then
   nodeinfo=${SEQ_BIN}/nodeinfo
else
   nodeinfo=$(which nodeinfo)
fi

if [[ $n == "" ]]; then
    echo "Node must be defined"
    exit 1
fi

if [[ $l != "" ]]; then
   fullnode=`$nodeinfo -n $n -l $l -f node`
   fullnode=`echo $fullnode | cut -d "=" -f2`
else
   fullnode=$n
fi

dir=${SEQ_EXP_HOME}/sequencing/batch$(dirname ${fullnode})

if [[ $list -eq 1 ]]; then
   ls -lrt $dir/$(basename $fullnode).*
else
   batchfile=$(ls -rt1 $dir/$(basename $fullnode).* | tail -1)
   if [[ -e $batchfile ]] ; then 
      cat $batchfile
   else 
      echo $fullnode 'batch file not available'
   fi
fi
