#!/bin/sh

# Copyright 1996 James H. Lowe, Jr.
# Copying Policy: FSF's GPL
# $Id: shak0_errmon_u_v 1.8 1997/01/18 16:08:20 jhl Exp $
trap "" 1 2 15

# FROM parent env
USERCMDPID=$shak_usercmdpid

action=$1
shift
elapsedtime=$1
shift
#filename="$1"
#shift
USERCMDPID=$1
shift
USERCMD="$*"
tmpfilename="${shak_tmpbasename_a}.ermn_u$$"
umask 077
awk '{print}' >$tmpfilename
exitval=9

set -- "$USERCMD"
USERCMD1="$1"

	case $action in
		0)
			# no match		
			:
			;;
		###############################		
		1)
			# match on rfatal

		${SHAK_LIBPATH}/shak_kill 15 -${USERCMDPID}
		retval=$?
(
cat <<EOF
The command
"$*"
wrote a fatal message: \"${shakerrorline}\"

DATE: `date`
Elapsed Time: $elapsedtime seconds. 

The contents of stderr file are:
`cat $tmpfilename` 
---- End of stderr ---- line separator -----
Local parent process Id: host $SHAK_HOST: PID $USERCMDPID
Remote process Id:       host $SHAK_BACKOST: PID (unknown)
Backup Header SHAK_SHAKHEADER is listed on next line.
header:$SHAK_SHAKHEADER

Process PID $USERCMDPID was sent signal SIGTERM.
shak_kill returned $retval

Status of remote child processes unknown.
This message was sent by shak_errmon.
cc: root
End of message.
EOF
) |
${SHAK_LIBPATH}/shak_notify user.err "FATAL_ERROR $USERCMD"

			exitval=$?
			;;
		###############################		
		2)
			# match on rwarn
(
cat <<EOF
The command
"$*"
wrote a non-fatal message: \"${shakerrorline}\"

DATE: `date`
Elapsed Time: $elapsedtime seconds. 

The contents of stderr file are:
`cat $tmpfilename` 
---- End of stderr - line separator -----
Backup Header SHAK_SHAKHEADER is listed on next line.
header:$SHAK_SHAKHEADER
EOF
) |
${SHAK_LIBPATH}/shak_notify user.warning "WARNING $USERCMD"
			exitval=$?	
			;;
		###############################		
		3)
			# match on rbegin
(
cat <<EOF
THIS IS NOT AN ERROR NOTIFICATION
The command
"$*"
wrote the normal start message: \"${shakerrorline}\"

DATE: `date`
Elapsed Time: $elapsedtime seconds. 

The contents of stderr file are:
`cat $tmpfilename` 
---- End of stderr - line separator -----
Backup Header SHAK_SHAKHEADER is listed on next line.
header:$SHAK_SHAKHEADER
EOF
) |
${SHAK_LIBPATH}/shak_notify user.notice "__BEGIN__ $USERCMD"

			exitval=$?
			;;

		###############################		
		4)
			# match on rend
(
cat <<EOF
THIS IS NOT AN ERROR NOTIFICATION
The command
"$*"
wrote the normal end message: \"${shakerrorline}\"

DATE: `date`
Elapsed Time: $elapsedtime seconds. 

The contents of stderr file are:
`cat $tmpfilename` 
---- End of stderr - line separator -----
Backup Header SHAK_SHAKHEADER is listed on next line.
header:$SHAK_SHAKHEADER
EOF
) |
${SHAK_LIBPATH}/shak_notify user.notice "__END__ $USERCMD"

			exitval=$?
			;;
		5)
			# timeout
		${SHAK_LIBPATH}/shak_kill  15 -${USERCMDPID}
		retval=$?
(
cat <<EOF
The command
"$*"
timed out:

DATE: `date`
Elapsed Time: $elapsedtime seconds. 

The contents of stderr file are:
`cat $tmpfilename` 
---- End of stderr ---- line separator -----
Local parent process Id: host $SHAK_HOST: PID $USERCMDPID
Remote process Id:       host $SHAK_BACKOST: PID (unknown)
Backup Header SHAK_SHAKHEADER is listed on next line.
header:$SHAK_SHAKHEADER

Process PID $USERCMDPID was sent signal SIGTERM.
shak_kill returned $retval

Status of remote child processes unknown.
This message was sent by shak_errmon.
cc: root
End of message.
EOF
) |
${SHAK_LIBPATH}/shak_notify user.err "TIMEOUT $USERCMD"
			exitval=$?
		;;

		6)
			# match on rsigterm
(
cat <<EOF
THIS MAY BE AN ERROR NOTIFICATION
The command
"$*"
wrote the SIGTERM message: \"${shakerrorline}\"

DATE: `date`
Elapsed Time: $elapsedtime seconds. 

The contents of stderr file are:
`cat $tmpfilename` 
---- End of stderr - line separator -----
Backup Header SHAK_SHAKHEADER is listed on next line.
header:$SHAK_SHAKHEADER
EOF
) |
${SHAK_LIBPATH}/shak_notify user.notice "__SIGTERM__ $USERCMD"

			exitval=$?

esac
/bin/rm -f  "$tmpfilename"
exit $exitval

