#!/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_lite $0 \
  -n        ""                    ""                           "[Node               ]" \
  -l        ""                    ""                           "[Loop arguments     ]" \
  -d        ""                    ""                           "[Datestamp          ]" \
  -v        ""                    "1"                          "[Verbose            ]" \
  -m        ""                    ""                           "[Target exec host   ]" \
  -o        ""                    ""                           "[Target file or stdout  ]" \
  -f        "0"                   "1"                          "[Evaluate full config seen at node]"\
  -h        ""                    ""                           "[Display help       ]" \
  ++ $arguments`                                                                                                                                                                                                                   

if [[ $v != "" ]] ; then  
  set -x
fi 
if [[ $d != "" ]] ; then  
  export SEQ_DATE=$d 
fi 
                                                                                                                                                                                                                                   
if [[ -n "${SEQ_BIN}" ]] ; then                                                                                                                                                                                                    
    nodeinfo=${SEQ_BIN}/nodeinfo                                                                                                                                                                                                   
else                                                                                                                                                                                                                               
    nodeinfo=$(which nodeinfo)                                                                                                                                                                                                     
fi                                                                                                                                                                                                                                 
if [[ $l != "" ]]; then largs="-l $l" ; fi                                                                                                                                                                                         

rm -f $TMPDIR/nodeinfo.$$ || ( echo "Error: unable to delete $TMPDIR/nodeinfo.$$. Exiting..." ; exit 1 ) 
$nodeinfo -n $n $largs > $TMPDIR/nodeinfo.$$
 

if [[ $m == "" ]] ; then
    mach=$(cat $TMPDIR/nodeinfo.$$ | grep node.machine | cut -d "=" -f 2) 
else
    mach=$m
fi

if [[ $f == "0" ]] ; then 
    config=$(cat $TMPDIR/nodeinfo.$$ | grep node.configpath | cut -d "/" -f 3-) 
    config="$SEQ_EXP_HOME/modules/$config"
    if [[ ! -f $config ]] ; then 
     echo "Config $config does not exist, so comparison cannot be made. Exiting..." 
     exit 1
    fi
fi

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

name=`echo $n | tr '/' '_'`

type=$(cat $TMPDIR/nodeinfo.$$ | grep node.type | cut -d "=" -f 2)

## get config variables
$SEQ_BIN/nodeinfo -n $n $loops -f var -o $TMPDIR/$name.vars$$ 1>/dev/null 
. $TMPDIR/$name.vars$$

workbase=$(${SEQ_UTILS_BIN}/nodework -n ${SEQ_NODE} -base | cut -d ':' -f 2)

# get variable and environment space from target host

echo "# Logging in to $mach. Stand by..." 

echo "   
         . /etc/profile
         . $HOME/.profile
         scp $HOST:$TMPDIR/$name.vars$$ \$TMPDIR/$name.vars$$
         . \$TMPDIR/$name.vars$$
         export SEQ_WORKBASE=$workbase
         ${SEQ_UTILS_BIN}/chaindot.py -e ${SEQ_EXP_HOME} -n ${SEQ_NODE} -o \$TMPDIR/${SEQ_NAME}${SEQ_LOOP_EXT}.chaindot$$
         if [[ "$f" == "0" ]] ; then 
             ${SEQ_UTILS_BIN}/runvars -i $config -o \$TMPDIR/${SEQ_NAME}${SEQ_LOOP_EXT}.result$$ -c \$TMPDIR/${SEQ_NAME}${SEQ_LOOP_EXT}.chaindot$$ -v $v 
         else
             ${SEQ_UTILS_BIN}/runvars -i \$TMPDIR/${SEQ_NAME}${SEQ_LOOP_EXT}.chaindot$$ -o \$TMPDIR/${SEQ_NAME}${SEQ_LOOP_EXT}.result$$ -c \$TMPDIR/${SEQ_NAME}${SEQ_LOOP_EXT}.chaindot$$ -v $v
         fi
         if [[ $type == "Task" || $type == "NpassTask" ]] ; then 
             ${SEQ_UTILS_BIN}/task_setup.ksh --dry-run -f \$TMPDIR/${SEQ_NAME}${SEQ_LOOP_EXT}.chaindot$$ | grep -v '** TASK_SETUP'  >> \$TMPDIR/${SEQ_NAME}${SEQ_LOOP_EXT}.result$$ 
         fi
         scp \$TMPDIR/${SEQ_NAME}${SEQ_LOOP_EXT}.result$$ $HOST:$TMPDIR/${SEQ_NAME}${SEQ_LOOP_EXT}.result$$ 
             " | ssh $mach 1>/dev/null 

# show results, filter out empty-lines
if [[ "$o" == "" ]] ; then  
   cat $TMPDIR/${SEQ_NAME}${SEQ_LOOP_EXT}.result$$  | sed -e '/^$/d' -e 's/\(^[a-zA-Z0-9_]*=\)\(.*\)/\1"\2"/' 
else 
   cat $TMPDIR/${SEQ_NAME}${SEQ_LOOP_EXT}.result$$  | sed -e '/^$/d' -e 's/\(^[a-zA-Z0-9_]*=\)\(.*\)/\1"\2"/'  > $o
fi

