#!/bin/sh

# INPUTS:
# <system_name> <condition> <short_msg> <long_msg> [submitter user password category type item]
#
# system_name : node on which error occurred (must match in Remedy to generate workflow))
# condition   : error condition (must match in Remedy to generate work flow)
# short_msg   : error details
# long_msg    : more error details
# submitter   : submitter of ticket  ('MSS' for mass storage system)
# user        : user known to Remedy ('MSS' for mass storage system)
# password    : password to be able to access rememdy ('2p9u6c' for mass storage system)
# category    : category of ticket ('MSS' for mass storage system)
# type        : type of error within the category ('STK Enstore' or 'D0 Enstore' or 'CDF Enstore' for MSS)
# item        : item within the type
#

set -u  # force better programming and ability to use check for not set
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

# assume we are talking to the remedy server specified in the following line
remedy_server="csdserver"

. /usr/local/etc/setups.sh

rc=1

setup servicenow_soap 2>/dev/null
if [ "${SETUP_SERVICENOW_SOAP:-}" != "" ];
then
    export PYTHONPATH=$SETUPS_DIR:$PYTHONPATH
    CI=`echo $1 | awk '{ printf toupper($1) }'`
    $ENSTORE_DIR/sbin/create_remedy_ticket.py "${CI}" "$2" "$3" "$4" 2>/dev/null
    rc=$?
    if [ "${rc}" != "0" ];
    then
	echo "enstore alarm --raise --root_error 'servicenow_soap api call failed' --severity E"|at now+1min
	unsetup servicenow_soap 2>/dev/null
    else
	exit 0
    fi
else
    echo "enstore alarm --raise --root_error 'servicenow_soap product missing or not current' --severity E"|at now+1min
fi


setup remedy_soap 2>/dev/null
if [ "${SETUP_REMEDY_SOAP:-}" != "" ];
then
    export PYTHONPATH=$SETUPS_DIR:$PYTHONPATH
    CI=`echo $1 | awk '{ printf toupper($1) }'`
    $ENSTORE_DIR/sbin/create_remedy_ticket.py "${CI}" "$2" "$3" "$4"
    rc=$?
    if [ "${rc}" != "0" ];
    then
	echo "enstore alarm --raise --root_error 'remedy_soap api call failed' --severity E"|at now+1min
    else
	exit 0
    fi
else
    echo "enstore alarm --raise --root_error 'remedy_soap product missing or not current' --severity E"|at now+1min
fi

exit 0