#! /bin/sh
#
# set -x
#
# Copyright 2001 Compaq Computer Corporation.  COMPAQ, the Compaq logo and the
# Digital logo are registered in the U.S. Patent and Trademark Office.  Pro-
# prietary service tool software. Valid license from Compaq or authorized
# sublicensor required for possession, use or copying. Consistent with FAR
# 12.211 and 12.212, Commercial Computer Software, Computer Software Documenta-
# tion, and Technical Data for Commercial Items are licensed to the U.S. Government
# under vendor's standard commercial license.
#
# Compaq service tool software, including associated documentation, is the property
# of and contains confidential technology of Compaq Computer Corporation.  Service
# customer is hereby licensed to use the software only for activities directly relating
# to the delivery of, and only during the term of, the applicable services delivered
# by Compaq or its authorized service provider.  Customer may not modify or reverse
# engineer, remove or transfer the software or make the software or any resultant
# diagnosis or system management data available to other parties without Compaq's or
# its authorized service provider's consent.  Upon termination of the services customer
# will, at Compaq's or its service provider's option, destroy or return the software and
# associated documentation in its possession.
#
#####################################################################################
#
# This script is invoked from DECevent external notification. It creates a text file
# containing details of the notification event and creates a Service Request (SRQ) to
# the System Initiated Call Logging (SICL) queue at the Customer Service Center. 
#
# 07-Dec-2001   Merlin Vincent      Created.
#
#####################################################################################


#####################################################################################
#
# START OF SUBROUTINES
#
#####################################################################################
# Create the notification text file in /usr/opt/DIA${VERSION}/tmp. Use the system time
# to obtain a unique filename (there may be multiple notifications in progress), and 
# loop to make sure only one such filename exists.

create_text_file()
{
  SLEEPS=0
  while true
  do
    SUFFIX=`date +"%r" | cut -f1 -d\ | tr ':' '.'`
    NOTIFY_TEXT="/usr/opt/DIA${VERSION}/tmp/ext_notify.$SUFFIX"
    echo $NOTIFY_TEXT
    if [ -f $NOTIFY_TEXT ]
    then
	sleep 1
	SLEEPS=`expr $SLEEPS + 1`
	if [ $SLEEPS -gt 2 ]; then
	    NOTIFY_TEXT=""
	    break
	fi
    else
	touch $NOTIFY_TEXT
	if [ ! -f $NOTIFY_TEXT ]; then
	    NOTIFY_TEXT=""
	fi
	break
    fi
  done
}

#####################################################################################
# Use the parameters passed in from DECevent to create the notification text message.

create_notification_text()
{
    # The file has a specific format: start with a line of 80 *'s. A second line of *'s
    # indicates the start of the customer profile. And two final lines of *'s indicates
    # end of file. The * lines are used by the SICL processors to parse the message.

    create_text_file
    [ -z $NOTIFY_TEXT ] && return

    THEORY_CODE=$8

    echo "********************************************************************************" >> $NOTIFY_TEXT
    echo "                         DECevent Notification Message " >> $NOTIFY_TEXT
    echo "                               " >> $NOTIFY_TEXT
    echo "DECevent has detected that the following device needs attention:" >> $NOTIFY_TEXT
    echo "                               " >> $NOTIFY_TEXT
    echo "   DEVICE:               $5 $6" >> $NOTIFY_TEXT
    echo "   NODE:                 ${NODE_NAME}" >> $NOTIFY_TEXT
    echo "   SYSTEM SERIAL NUMBER: ${SERNUM}   " >> $NOTIFY_TEXT
    echo "   SYSTEM TYPE:          ${SYSTYPE}  " >> $NOTIFY_TEXT
    echo "                               " >> $NOTIFY_TEXT
    echo "                               " >> $NOTIFY_TEXT
    echo "   DECevent Diagnosis Information " >> $NOTIFY_TEXT
    echo "                               " >> $NOTIFY_TEXT
    echo "           Field Service       " >> $NOTIFY_TEXT
    echo "                               " >> $NOTIFY_TEXT
    echo "DEVICE:    $5 $6" >> $NOTIFY_TEXT
    echo "                               " >> $NOTIFY_TEXT
    echo "Theory:    $8" >> $NOTIFY_TEXT
    echo "                               " >> $NOTIFY_TEXT
    echo "Evidence:  TBD" >> $NOTIFY_TEXT
    echo "                             " >> $NOTIFY_TEXT

    NOW=`date +"%y-%m-%d %H:%M:%S"`
    echo "Time:  $NOW To $NOW" >> $NOTIFY_TEXT

    echo "                             " >> $NOTIFY_TEXT
    echo "Uncorrelated event parameters: " >> $NOTIFY_TEXT
    echo "  :  <$1> <$2> <$3>          " >> $NOTIFY_TEXT
    echo "  :  <$4> <$7>               " >> $NOTIFY_TEXT
    echo "                             " >> $NOTIFY_TEXT

    # If a customer profile exists, add it to the end of the notification.
    echo "********************************************************************************" >> $NOTIFY_TEXT

    if [ -f /var/DIA/FMGPROFILE ]; then
	cat /var/DIA/FMGPROFILE >> $NOTIFY_TEXT
    else
	echo "Customer profile information is not available"  >> $NOTIFY_TEXT
    fi

    echo "********************************************************************************" >> $NOTIFY_TEXT
    echo "********************************************************************************" >> $NOTIFY_TEXT
    echo " " >> $NOTIFY_TEXT
}

#####################################################################################
# Use the parameters passed in from DECevent to create the test text message.

create_test_text()
{
    # The file has a specific format: start with a line of 80 *'s. A second line of *'s
    # indicates the start of the customer profile. And two final lines of *'s indicates
    # end of file. The * lines are used by the SICL processors to parse the message.

    create_text_file
    [ -z $NOTIFY_TEXT ] && return

    THEORY_CODE="S011F"

    echo "********************************************************************************" >> $NOTIFY_TEXT
    echo "                                                          " >> $NOTIFY_TEXT
    echo "                  DECevent SICL Test Notification Message " >> $NOTIFY_TEXT
    echo "                                                          " >> $NOTIFY_TEXT
    echo "           *** System Initiated Call Logging End to End Test *** " >> $NOTIFY_TEXT
    echo "                                                          " >> $NOTIFY_TEXT
    echo "               NODE:                    ${NODE_NAME}      " >> $NOTIFY_TEXT
    echo "               SYSTEM SERIAL NUMBER:    ${SERNUM}         " >> $NOTIFY_TEXT
    echo "               SYSTEM TYPE:             ${SYSTYPE}        " >> $NOTIFY_TEXT
    echo "                                                          " >> $NOTIFY_TEXT

    # If a customer profile exists, add it to the end of the notification.
    echo "********************************************************************************" >> $NOTIFY_TEXT

    if [ -f /var/DIA/FMGPROFILE ]; then
	cat /var/DIA/FMGPROFILE >> $NOTIFY_TEXT
    else
        echo "Customer profile information is not available"  >> $NOTIFY_TEXT
    fi

    echo "********************************************************************************" >> $NOTIFY_TEXT
    echo "********************************************************************************" >> $NOTIFY_TEXT
    echo " " >> $NOTIFY_TEXT
}

#####################################################################################
# Retrieve the system info from the wwnotify_sysinfo file.

retrieve_sys_info ()
{
    NODE_NAME=`/usr/bin/uname -n 2> /dev/null`
    [ -z "$NODE_NAME" ] && NODE_NAME="UNKNOWN"

    if [ -f /usr/opt/DIA${VERSION}/tmp/WWNotify_SysInfo.txt ]
    then
	SERNUM=`grep SERIAL_NUMBER /usr/opt/DIA${VERSION}/tmp/WWNotify_SysInfo.txt 2> /dev/null | cut -f2 -d\=`
	SYSTYPE=`grep SYSTEM_TYPE /usr/opt/DIA${VERSION}/tmp/WWNotify_SysInfo.txt 2> /dev/null | cut -f2 -d\=`
    fi

    [ -z "$SERNUM" ] && SERNUM="UNKNOWN"

    if [ -z "$SYSTYPE" ]; then
	SYSTYPE=`/usr/sbin/sizer -c 2> /dev/null | cut -f2 -d\"`
	[ -z "$SYSTYPE" ] && SYSTYPE="UNKNOWN"
    else
	SYSTYPE=`echo $SYSTYPE | sed s/\"//g`
    fi
}

#####################################################################################
#
# START OF MAIN SCRIPT
#
#####################################################################################
# Important globals

NODE_NAME=
SYSTYPE=
SERNUM=
THEORY_CODE=
NOTIFY_TEXT=
SUFFIX=
PORT= 
VERSION=340

[ -f /etc/enp.config ] && . /etc/enp.config
[ -f $HOME/enp.config ] && . $HOME/enp.config

if [ -z "$CSG_SERVER" ]
then
   echo "   ERROR. Env variable CSG_SERVER not defined"
   exit 1
fi

retrieve_sys_info

if [ "$5" = "TEST" ]; then
   create_test_text $1 $2 $3 $4 $5 $6 $7 $8
   SUBJECT="DECevent Service Request - SICL End To End Test:[${THEORY_CODE}]"
else
   create_notification_text $1 $2 $3 $4 $5 $6 $7 $8
   SUBJECT="DECevent Service Request - Analysis:[${THEORY_CODE}]"
fi

if [ -z "$NOTIFY_TEXT" ]; then
   exit 0
fi

if [ -z "$NODE_NAME" ]; then
   exit 0
fi

if [ -n "${PORT}" ]; then 
   /usr/bin/enp post -s $CSG_SERVER -p $PORT -t "${SUBJECT}" $NODE_NAME $NOTIFY_TEXT sicl & 
else
   /usr/bin/enp post -s $CSG_SERVER -t "${SUBJECT}" $NODE_NAME $NOTIFY_TEXT sicl &
fi
