#!/bin/sh
# 
# @DEC_COPYRIGHT@
#
# HISTORY
# $Log: evmreload.sh,v $
# Revision 1.1.1.1  2003/12/11 15:40:52  ajay
# Importing Evm sources.
#
# Revision 1.1.1.1  2002/09/12 15:43:53  lsn
# EVM source - Linux initial version
#
# Revision 1.1.17.1  2001/11/21  21:15:29  Jem_Treadwell
# 	Fixed mis-placed brace
# 	[2001/11/20  20:14:20  Jem_Treadwell]
#
# Merge Information:  Non-Visual Merge: User specified revision 1.1.14.1
#     User Revision:  v51asupportos_bl0:1.1.12.1 Local Ancestor: n/a
#    Merge Revision:  1.1.14.1
#   Common Ancestor:  1.1.12.1
# 	Merge tempfile changes from wildcat to wcalpha
# 	[2001/11/20  20:12:16  Jem_Treadwell]
#
# Revision 1.1.14.1  2001/05/15  11:35:07  Jem_Treadwell
# 	QAR 87148: Move tempfile to private directory
# 
# Revision 1.1.12.1  2000/10/27  14:43:13  Anthony_Hoffman
# 	QARs 79077, 79078, 80646 fix return status for -N, allow for output redirect
# 
# Revision 1.1.10.1  2000/03/15  19:40:17  Anthony_Hoffman
# 	QAR 75900
# 
# Revision 1.1.4.6  1998/10/14  23:19:26  Bruce_Gayliard
# 	First BL 20 drop from evman shared sandbox.
# 	[1998/10/13  18:40:29  Bruce_Gayliard]
# 
# Revision 1.1.6.7  1998/09/30  17:41:13  Jem_Treadwell
# 	Removed dec.unix and msg cleanup
# 	[1998/09/30  17:40:26  Jem_Treadwell]
# 
# Revision 1.1.6.6  1998/02/04  17:55:29  Bruce_Gayliard
# 	Use TMPDIR for temp directory if specified.
# 	[1998/02/04  17:54:05  Bruce_Gayliard]
# 
# Revision 1.1.6.5  1998/01/06  14:26:54  Bruce_Gayliard
# 	Enhanced error handling, abort reconfig on bad conf files.
# 	[1998/01/06  14:25:32  Bruce_Gayliard]
# 
# Revision 1.1.6.4  1997/09/24  13:42:25  Jem_Treadwell
# 	Added channel manager
# 	[1997/09/24  13:41:46  Jem_Treadwell]
# 
# Revision 1.1.6.3  1997/09/08  19:04:41  Bruce_Gayliard
# 	Fixes for LINUX / portability.
# 	[1997/09/08  19:01:24  Bruce_Gayliard]
# 
# Revision 1.1.6.2  1997/09/05  13:08:02  Bruce_Gayliard
# 	Embellish error reporting for CLI.
# 	[1997/09/05  13:03:53  Bruce_Gayliard]
# 
# Revision 1.1.4.3  1997/05/30  14:26:18  Bruce_Gayliard
# 	BL 11 drop of Event Management - cleanup and startup, remove tclx.
# 	[1997/05/28  20:55:43  Bruce_Gayliard]
# 
# Revision 1.1.2.4  1997/05/22  21:26:21  Jem_Treadwell
# 	Added -l and -d options
# 	[1997/05/22  21:25:43  Jem_Treadwell]
# 
# Revision 1.1.2.3  1997/05/18  14:59:16  Bruce_Gayliard
# 	Added daemon reconfiguration event.
# 	[1997/05/18  14:55:45  Bruce_Gayliard]
# 
# Revision 1.1.2.2  1997/05/16  21:29:44  Jem_Treadwell
# 	Initial submit
# 	[1997/05/16  21:29:06  Jem_Treadwell]
# 
# 
# $EndLog$
# 
# @(#)$RCSfile: evmreload.sh,v $ $Revision: 1.1.1.1 $ (DEC) $Date: 2003/12/11 15:40:52 $
# 

ME=`basename ${0}`
MSG_NOT_ROOT="${ME}: You must be root to execute this command"
MSG_BAD_DATA="${ME}: Reload aborted - bad data"

. /usr/sbin/evmcommandset

EVMINFO=/usr/bin/evminfo
if (${TEST} `whoami` != "root")
then
   ${ECHO} ${MSG_NOT_ROOT}
   exit 1
fi

USAGE="Usage: $0 [-d] [-a] [-n] [-l] [-c]"
CONFIG_ERROR="Error in configuration - evmreload operation not performed."
EXIT_USAGE=1

# Tell the library builder this is not the daemon.
EVM_STAND_ALONE=1
export EVM_STAND_ALONE

# Get the vendor.product string:
VP=`evminfo -vp`
EVENT_PREFIX=${VP}.evm

# Events to be posted:
DAEMON_RELOAD="event { name ${EVENT_PREFIX}.control.reload.evmdaemon }"
LOGGER_RELOAD="event { name ${EVENT_PREFIX}.control.reload.logger }"
CHANNEL_RELOAD="event { name ${EVENT_PREFIX}.control.reload.channel }"

TEMP_DIR=/var/evm/sockets

DAEMON=
LOGGER=
CHANNEL=
LOADTEST=
FORCE=
FAILED=0


# Check the start options:
if [ ! $# = 0 ]
then
	while [ $# -gt 0 ]
	do
		case $1 in
		-d)	DAEMON=yes
			shift
			;;

		-l)	LOGGER=yes
			shift
			;;

		-c)	CHANNEL=yes
			shift
			;;

		-n)	LOADTEST=yes
			shift
			;;

		-a)	FORCE=yes
			shift
			;;

		*)	${ECHO} $USAGE
			exit $EXIT_USAGE
			;;
		esac
	done
fi

if [ "$DAEMON" = "" -a "$LOGGER" = "" -a "$CHANNEL" = "" ]
then
	DAEMON=yes
	LOGGER=yes
	CHANNEL=yes
fi

# make sure all the config files are valid before we proceed

if [ "$DAEMON" != "" ]
then
	$EVMINFO -verify daemon 
	STATUS=$?
	if [ $STATUS -ne 0 ]
	then
	   echo $CONFIG_ERROR
	   exit $STATUS
	fi
fi

if [ "$LOGGER" != "" ]
then
	$EVMINFO -verify logger 
	STATUS=$?
	if [ $STATUS -ne 0 ]
	then
	   echo $CONFIG_ERROR
	   exit $STATUS
	fi
fi

if [ "$CHANNEL" != "" ]
then
	$EVMINFO -verify chmgr 
	STATUS=$?
	if [ $STATUS -ne 0 ]
	then
	   echo $CONFIG_ERROR
	   exit $STATUS
	fi
fi

# Post reload or test events as requested:
if [ "$DAEMON" != "" -o "$LOADTEST" != "" ]
then
# set up for evmmklib
# FUTURE: Multiple simultaneous evmreload commands may interfere with each other
#         We may need to come up with a lock mechanism here.
	EVM_BINARY_EVENT_FILE=${TEMP_DIR}/evmtempbin
	EVM_DAEMON_TEMP_FILE=${TEMP_DIR}/EvmDaemonTemp

	export EVM_BINARY_EVENT_FILE  EVM_DAEMON_TEMP_FILE

	# Cleanup the temporary files on any exit, including interrupt:
	trap "rm -f ${EVM_BINARY_EVENT_FILE} ${EVM_DAEMON_TEMP_FILE}" 0 2

	/usr/sbin/evmmklib -v

	LIBCHECK=$?

	if [ "$LOADTEST" = "" ]
	then
		if [ "$LIBCHECK" = "0" -o \
		     \( \( ! "$LIBCHECK" = "0" \) -a "$FORCE" = "yes" \) ]
		then
			${ECHO} $DAEMON_RELOAD | /usr/bin/evmpost
		else
			${ECHO} ${MSG_BAD_DATA}
			exit 1
		fi
	else
		exit $LIBCHECK
	fi
fi

if [ "$LOGGER" != "" -a "$LOADTEST" = "" ]
then
	${ECHO} $LOGGER_RELOAD | /usr/bin/evmpost
	if [ $? != 0 ]
	then
		exit $?
	fi
fi

if [ "$CHANNEL" != "" -a "$LOADTEST" = "" ]
then
	${ECHO} $CHANNEL_RELOAD | /usr/bin/evmpost
	if [ $? != 0 ]
	then
		exit $?
	fi
fi

