#!/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.
# */


#
# name: nodetracer
#
# author: CMOIS
#
# description: saves the output of a node 
#
# usage: nodetracer -n node [-l loopargs] -type [success,abort,submission] -d datestamp -i input_file  -takes input file and places it in the right place in the experiment for node n
#        nodelister -h                                                                      -shows help

Usage="
Nodetracer  \n 
\n
\t     Saves the listing of a node onto its proper position in the experiment \n
\n
\t     Usage: nodetracer -n node [-l loopargs] [-type [success,abort,submission]] -i infile -d datestamp \n 
\n
\t\t        Where: \n
\t\t\t          node is the full node name. ex: /regional/assimilation/00/gen_cutoff \n
\t\t\t          loopargs is the comma-separated list of loop arguments. ex. outer_loop=1,inner_loop=2\n
\t\t\t          type defines whether to get the latest succesfull, aborted or submission listing. Default is success.\n
\t\t\t          infile is the input listing file.\n
\t\t\t          datestamp is the experiment datestamp value (YYYYMMDDhhmmss).\n
\n
\t     Ex: \n
\t\t       nodelister -h                                                       - shows help\n
\t\t       nodelister -n node -type [success,abort] -i infile                  - saves infile's content into the (sucessful, abort) listing directory for the node\n
\n
\t     SEQ_EXP_HOME=$SEQ_EXP_HOME\n"

arguments=$*

eval `cclargs $0 \
  -n        ""                    ""                   "[Node         ]" \
  -h        ""                    ""                   "[Display help       ]" \
  -type     "success"             "success"            "[Show latest successful, aborted or submission listing?  ]" \
  -l        ""                    ""                   "[Loop argument list ]" \
  -i        ""                    ""                   "[Input listing to file    ]" \
  -d        ""                    ""                   "[datestamp value]" \
  -c        "0"                   "1"                  "[Clean (remove) input file after save]" \
  -v        "0"                   "1"                  "[Verbose]" \
  ++ $arguments`

if [ ! -d "${SEQ_EXP_HOME}" ]; then
   echo "nodetracer 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

[[ $v == 1 ]] && set -x 

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

if [[ $d == "" ]]; then
    echo "Datestamp must be defined"
    exit 1
fi
datestamp=${d}

if [[ $l != "" ]]; then
   fullnode=`$nodeinfo -n $n -l $l -f node`
   fullnode=`echo $fullnode | cut -d "=" -f2`
   if [[ $fullnode = "" ]]; then
       echo nodeinfo reported a problem - exiting.
       exit 1
   fi
else
   fullnode=$n
fi

if [[ $l != "" ]]; then
   loopargs="-l $l"
else
   loopargs=""
fi

basedir=${SEQ_EXP_HOME}
compress=true

if [ "$type" = "abort" -o "$type" = "success" -o "$type" = "submission" ]; then
   dest=$basedir/listings/$TRUE_HOST/$fullnode.$type
else
   echo "type must be success, abort or submission"
   exit 1
fi

#create listing
dest_leaf="${dest##*/}"
dest_dir="${dest%/${dest_leaf}}"
time_dest=${dest}.${datestamp}.`date +%T`
rm -f $time_dest
mkdir -p $dest_dir
if [[ $? != 0 ]]; then
    nodelogger -n $n $loopargs -s abort -m "nodetracer error: cannot create directory $dest_dir"
    exit 1
fi

#compress listing? 
if [[ -f $HOME/.maestrorc ]] ; then 
    compress=`getdef $HOME/.maestrorc compress_listing` 2> /dev/null 
fi 

if [[ "$compress" == "false" ]] ; then
    mv $i $time_dest
    if [[ $? != 0 ]]; then
        nodelogger -n $n $loopargs -s abort -m "nodetracer error: cannot move $i to $time_dest"
        exit 1
    fi
else
    gzip -9 -c $i > $time_dest
    if [[ $? != 0 ]]; then
        nodelogger -n $n $loopargs -s abort -m "nodetracer error: cannot compress $i to $time_dest"
        exit 1
    fi
fi

#create reference to listing
latest_dir=$basedir/listings/latest/
nodeleaf="${fullnode##*/}"
nodebranch="${fullnode%/${nodeleaf}}"
mkdir -p ${latest_dir}/$nodebranch
if [[ $? != 0 ]]; then
    nodelogger -n $n $loopargs -s abort -m "nodetracer error: cannot create directory ${latest_dir}/${nodebranch}. Listing only visible here $time_dest"
    exit 1
fi
rm -f $latest_dir/${fullnode}.${datestamp}.$type@*
ln -s $time_dest $latest_dir/$fullnode.${datestamp}.$type@$TRUE_HOST
if [[ $? != 0 ]]; then
    nodelogger -n $n $loopargs -s abort -m "nodetracer error: cannot link ${latest_dir}/${fullnode}.${datestamp}.${type}@${TRUE_HOST} to $time_dest"
    exit 1
fi

[[ $c == 1 ]] && rm -f $i

if [[ "$type" != "success" && "$type" != "submission" && "$SEQ_ABORT_EMAILS" != "" ]]; then
  echo "To get the abort listing: \n1) Load maestro's ssm package\n2) Run commands:\n\texport SEQ_EXP_HOME=$SEQ_EXP_HOME \n\t$SEQ_UTILS_BIN/nodelister -f $time_dest"  |Mail -s "ABORT: nodename=$fullnode exp=$SEQ_EXP_HOME date=$SEQ_DATE" $SEQ_ABORT_EMAILS
fi

