#!/bin/sh
# 
# @DEC_COPYRIGHT@
#
# HISTORY
# $Log: evmstart.sh,v $
# Revision 1.1.1.1  2003/12/11 15:40:49  ajay
# Importing Evm sources.
#
# Revision 1.1.1.1  2002/09/12 15:43:40  lsn
# EVM source - Linux initial version
#
# Revision 1.1.13.1  2000/02/14  15:57:42  Anthony_Hoffman
# 	# qar 77083, up timeout value to accomodate longer startup times
#
# Revision 1.1.4.11  1998/12/04  21:30:35  Bruce_Gayliard
# 	Drop from EVM shared sandbox to steel BL 21.
# 	[1998/12/03  19:43:39  Bruce_Gayliard]
#
# Revision 1.1.6.10  1998/12/02  19:55:56  Bruce_Gayliard
# 	Increase timeout values for clusters bug.
# 	[1998/12/02  19:53:12  Bruce_Gayliard]
# 
# Revision 1.1.6.9  1998/11/10  21:20:38  Bruce_Gayliard
# 	Fixes for QAR 65573.
# 	[1998/11/10  21:20:03  Bruce_Gayliard]
# 
# Revision 1.1.6.8  1998/09/30  17:24:40  Jem_Treadwell
# 	Use evminfo for vendor.product, plus msg cleanup
# 	[1998/09/30  17:23:34  Jem_Treadwell]
# 
# Revision 1.1.6.7  1998/01/07  16:30:00  Bruce_Gayliard
# 	Add root check before executing.
# 	[1998/01/07  15:07:53  Bruce_Gayliard]
# 
# Revision 1.1.6.6  1997/09/08  19:04:37  Bruce_Gayliard
# 	Fixes for LINUX / portability.
# 	[1997/09/08  19:01:15  Bruce_Gayliard]
# 
# Revision 1.1.6.5  1997/09/05  13:07:51  Bruce_Gayliard
# 	First cut at authentication mechanism.
# 	[1997/09/05  13:05:26  Bruce_Gayliard]
# 
# Revision 1.1.6.4  1997/08/06  18:00:21  Bruce_Gayliard
# 	Remote on init 3, checksum, some cleanup.
# 	[1997/08/06  17:55:55  Bruce_Gayliard]
# 
# Revision 1.1.6.3  1997/07/15  17:28:44  Bruce_Gayliard
# 	Support for synchronizers, Insight mods, and prep for BL 12 drop.
# 	[1997/07/15  17:21:14  Bruce_Gayliard]
# 
# Revision 1.1.6.2  1997/06/04  17:54:43  Bruce_Gayliard
# 	QAR 53310 - Fix failure paths in startup; event name for startup.
# 	[1997/06/04  17:51:18  Bruce_Gayliard]
# 
# Revision 1.1.4.2  1997/05/30  14:26:06  Bruce_Gayliard
# 	BL 11 drop of Event Management - cleanup and startup, remove tclx.
# 	[1997/05/28  20:55:16  Bruce_Gayliard]
# 
# Revision 1.1.2.2  1997/05/28  11:02:45  Bruce_Gayliard
# 	Initial version of evm startup command.
# 	[1997/05/28  10:57:56  Bruce_Gayliard]
# 
# $EndLog$
# 
# @(#)$RCSfile: evmstart.sh,v $ $Revision: 1.1.1.1 $ (DEC) $Date: 2003/12/11 15:40:49 $
# 

. /usr/sbin/evmcommandset

ME=`basename ${0}`

MSG_NOT_ROOT="${ME}: You must be root to execute this command"
MSG_STARTED="${ME}: Daemon started"
MSG_FAILED_START="${ME}: Daemon failed to start properly"
MSG_FAILED_CONN="${ME}: Daemon failed to accept connection"

if (${TEST} `whoami` != "root")
then
   ${ECHO} ${MSG_NOT_ROOT}
   exit 1
fi

# Get the vendor.product string:
VP=`evminfo -vp`

START_EVENT=${VP}.evm.evmstart

EVM_START_TIMEOUT_DEFAULT=60
EVM_TEMPLATE_DIR_DEFAULT=/usr/share/evm/templates
EVM_LOCAL_DIR_DEFAULT=/var/evm
EVM_AUTHENT_DIR_DEFAULT=/var/evm/shared
EVM_DIR_PERMS_DEFAULT=755

EVM_START_TIMEOUT=${EVM_START_TIMEOUT:=${EVM_START_TIMEOUT_DEFAULT}}
EVM_TEMPLATE_DIR=${EVM_TEMPLATE_DIR:=${EVM_TEMPLATE_DIR_DEFAULT}}
EVM_LOCAL_DIR=${EVM_LOCAL_DIR:=${EVM_LOCAL_DIR_DEFAULT}}
EVM_AUTHENT_DIR=${EVM_AUTHENT_DIR:=${EVM_AUTHENT_DIR_DEFAULT}}
EVM_DIR_PERMS=${EVM_DIR_PERMS:=${EVM_DIR_PERMS_DEFAULT}}

# DEFECT: INSTALLATION : change of template directory did not account for local.
if [ ! -d ${EVM_LOCAL_DIR} ]
then
   ${MKDIR} -p ${EVM_LOCAL_DIR} 2> /dev/null
   ${CHMOD} ${EVM_DIR_PERMS} ${EVM_LOCAL_DIR} 2> /dev/null
fi

if [ ! -d ${EVM_AUTHENT_DIR} ]
then
   ${MKDIR} -p ${EVM_AUTHENT_DIR} 2> /dev/null
   ${CHMOD} ${EVM_DIR_PERMS} ${EVM_AUTHENT_DIR} 2> /dev/null
fi

# Note that the following test will create both the templates directory
# and the directory containing the templates directory (if required).
if [ ! -d ${EVM_TEMPLATE_DIR} ]
then
   ${MKDIR} -p ${EVM_TEMPLATE_DIR} 2> /dev/null
   ${CHMOD} ${EVM_DIR_PERMS} ${EVM_TEMPLATE_DIR} 2> /dev/null
   ${CHMOD} ${EVM_DIR_PERMS} ${EVM_TEMPLATE_DIR}/.. 2> /dev/null
fi

/usr/sbin/evmd $*
EVMD_KICKED_OFF=$?
if [ ! "${EVMD_KICKED_OFF}" = "0" ]
then
   ${ECHO} ${MSG_FAILED_START}
   exit 1
fi

# Give the daemon time to get started, then attempt to post 
# an event.  If the post succeeds, we're done; if not, retry
# until the retry count is exhausted.
STARTED="1"
REP_COUNT=0
while [ ${STARTED} != "0" -a ! "${EVM_START_TIMEOUT}" = "${REP_COUNT}" ]
do
   sleep 1
   ${CAT} << EOF | /usr/bin/evmpost 2>/dev/null
      event {
         name ${START_EVENT}
      }
EOF
   STARTED=$?
   REP_COUNT=`${EXPR} ${REP_COUNT} + 1`
done

if [ "${STARTED}" = "0" ]
then
   ${ECHO} ${MSG_STARTED}
else
   ${ECHO} ${MSG_FAILED_CONN}
fi

