#!/bin/bash

#============================================================================
# Copyright 2009-2018 ECMWF.
# This software is licensed under the terms of the Apache Licence version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
#============================================================================


if [[ $# -ne 6 ]] ; then
    echo "Error: wrong number of arguments = $# (must be 6)"
    if [[ $# -eq 1 ]] ; then
        echo "This script should only be called from within ecflow_ui."
    fi
    exit 1
fi

outFile=$1
host=$2
port=$3
nodePath=$4
nodeStatus=$5
project=$6

jiraExe="jira"
lastLines=50
pattern="--abort"
foundPattern=0
hasOutFile=0
hasLog=0
exitCode=0

#-------------------
# get node log
#-------------------
logTmp=$(mktemp)
ecflow_client --host=${host} --port=${port} --edit_history ${nodePath} > ${logTmp} 2>&1
if [[ $? -eq 0 && -s ${logTmp} ]] ; then
    hasLog=1
fi

#-------------------
# get task output
#-------------------
if [[ "${outFile}" != "_undef_" && -s ${outFile}  ]] ; then
    hasOutFile=1
    outTmp=$(mktemp)

    #line number of the last occurence of pattern in the output file
    lineNum=$(grep -n -e ${pattern}  $outFile | tail -n 1 | cut -d : -f 1)

    #extract lines preceding last occurence of pattern from out file
    if [ $? -eq 0 ] ; then
        foundPattern=1
        lineNum=$((lineNum-lastLines))
        lineNum=$((lineNum+1))
        tail -n+${lineNum} ${outFile} | head -n${lastLines} > ${outTmp}
    else
        tail -${lastLines} ${lastLines} > ${outTmp}
    fi
fi

#-------------------
# build command
#-------------------

#define issue description
desc="This report was generated automatically from ecflowUI at $(date). \\\\Server: ${host}@${port} \\\\Node: ${nodePath}"

#jobout
if [[ ${hasOutFile} -eq 1 ]] ; then

    if [ "${foundPattern}" = "1" ] ; then
        descOutFile="These are the last ${lastLines} lines preceding pattern=${pattern} from the node job output file: {code}$(cat ${outTmp}) {code}"
    else
        descOutFile="These are the last ${lastLines} lines from the node job output file:  {code}$(cat ${outTmp}) {code}"
    fi
else
    descOutFile="Could not locate job output file."
fi


#log
if [[ ${hasLog} -eq 1 ]] ; then
    descLog="These are the entries for the given node in the ecflow log: {code}$(cat ${logTmp}) {code}"
else
    descLog="No entries for the given node were found in the ecflow log"
fi

desc="${desc}\\\\ \\\\${descOutFile} \\\\${descLog}"

#-------------------
# run command
#-------------------

#create jira issue
jiraTmp=$(mktemp)

if [[ $project != "_undef_" ]] ; then

    jiraCmdStr="${jiraExe} -S \"${host}@${port}:${nodePath} status=${nodeStatus}\" -D \${desc} --justdoit +${project}"
    ${jiraExe} -S "${nodePath} status=${nodeStatus}" -D "${desc}" --justdoit +${project} > ${jiraTmp} 2>&1

else
    #if no project is defined the defaultproject is taken from the .jirarc file!
    jiraCmdStr="${jiraExe} -S \"${host}@${port}:${nodePath} status=${nodeStatus}\" -D \${desc} --justdoit +"
    ${jiraExe} -S "${nodePath} status=${nodeStatus}" -D "${desc}" --justdoit + > ${jiraTmp} 2>&1
fi

cmdFailed=$?

#the exit code is still 0 for certain errors so we check
#if the output contains the string error
if [[ $cmdFailed -eq 0  && -s ${jiraTmp} ]] ; then
    if [[  $(grep -c -e "ERROR:" ${jiraTmp}) -gt 0  ]] ; then
        cmdFailed=1
    fi
fi

#if there is an error the command stdout and stderr is written to the stderr
if [[ $cmdFailed -ne 0 ]] ; then
    echo -e "Generating JIRA issue with command: \n   ${jiraCmdStr}\nfailed!" >&2
    if [[ -s ${jiraTmp} ]] ; then
        cat ${jiraTmp} >&2
        exitCode=1
    fi
else
    echo -e "Sucessfully generated JIRA issue with command: \n   ${jiraCmdStr}\n"
fi


#delete tmp files
[[ -f ${logTmp} ]] && rm -f ${logTmp}
[[ -f ${outTmp} ]] && rm -f ${outTmp}
[[ -f ${jiraTmp} ]] && rm -f ${jiraTmp}

exit ${exitCode}
