#!/bin/sh
# 
# @DEC_COPYRIGHT@
#
# HISTORY
# $Log: evmmklib.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.21.1  2002/01/23  20:43:24  Anthony_Hoffman
#       QAR 86415 - customer specific template files were not being recognized
#       during a rolling upgrade
#
# Revision 1.1.15.1  2000/10/27  14:43:15  Anthony_Hoffman
# 	QARs 79077, 79078, 80646 fix return status for -N, allow for output redirect
#
# Revision 1.1.13.1  2000/05/18  19:33:59  Anthony_Hoffman
# 	QAR 78713 fixes for problems with rolling upgrades
#
# Revision 1.1.4.12  1999/05/11  13:51:11  Bruce_Gayliard
# 	Fix for QAR 71355.  Links to files were not being processed.  Error reporting
# 		required supporting changes to not indicate directories would not be
# 		processed.  Search for permission check had incorrect sense on type.
# 	[1999/05/10  17:23:43  Bruce_Gayliard]
#
# Revision 1.1.4.11  1999/03/26  20:57:34  Bruce_Gayliard
# 	Final mass drop from Event Management shared sandbox to steelos.
# 	[1999/03/25  20:09:39  Bruce_Gayliard]
# 
# Revision 1.1.6.12  1999/03/24  13:44:13  Bruce_Gayliard
# 	Fix for QAR 69504 - user template area was not scanned.
# 	[1999/03/24  13:43:32  Bruce_Gayliard]
# 
# Revision 1.1.6.11  1998/11/30  19:03:58  Jem_Treadwell
# 	QAR 65769 - improved handling of invalid files
# 	[1998/11/30  19:03:29  Jem_Treadwell]
# 
# Revision 1.1.6.10  1998/11/06  14:49:52  Bruce_Gayliard
# 	Support new /var/evm directory structure.
# 	[1998/11/06  14:49:00  Bruce_Gayliard]
# 
# Revision 1.1.6.9  1998/07/17  18:19:17  Bruce_Gayliard
# 	Fix permissions on created files.
# 	[1998/07/15  19:51:34  Bruce_Gayliard]
# 
# Revision 1.1.6.8  1998/05/21  23:35:25  Jem_Treadwell
# 	Fixed errlog date for Y2K, and consolidated errlog handling
# 	[1998/05/21  23:34:53  Jem_Treadwell]
# 
# Revision 1.1.6.7  1998/01/06  14:26:53  Bruce_Gayliard
# 	Enhanced error handling, abort reconfig on bad conf files.
# 	[1998/01/06  14:25:29  Bruce_Gayliard]
# 
# Revision 1.1.6.6  1997/09/08  19:04:40  Bruce_Gayliard
# 	Fixes for LINUX / portability.
# 	[1997/09/08  19:01:21  Bruce_Gayliard]
# 
# Revision 1.1.6.5  1997/09/05  13:07:59  Bruce_Gayliard
# 	Embellish error reporting for CLI.
# 	[1997/09/05  13:03:51  Bruce_Gayliard]
# 
# Revision 1.1.6.4  1997/08/15  15:25:12  Bruce_Gayliard
# 	Fixes for root and bin users and find problems.
# 	[1997/08/15  14:34:51  Bruce_Gayliard]
# 
# Revision 1.1.6.3  1997/07/18  17:21:10  Bruce_Gayliard
# 	Cosmetics and user assist.
# 	[1997/07/18  17:17:44  Bruce_Gayliard]
# 
# Revision 1.1.6.2  1997/07/15  17:29:15  Bruce_Gayliard
# 	Support for synchronizers, Insight mods, and prep for BL 12 drop.
# 	[1997/07/15  17:21:47  Bruce_Gayliard]
# 
# Revision 1.1.4.3  1997/05/30  14:26:16  Bruce_Gayliard
# 	BL 11 drop of Event Management - cleanup and startup, remove tclx.
# 	[1997/05/28  20:55:37  Bruce_Gayliard]
# 
# Revision 1.1.2.4  1997/05/28  11:02:51  Bruce_Gayliard
# 	Cleanup of pathnames to utilities.
# 	[1997/05/28  11:00:12  Bruce_Gayliard]
# 
# Revision 1.1.2.3  1997/05/22  17:24:22  Bruce_Gayliard
# 	Add initial support for fallback templates.
# 	[1997/05/22  17:23:28  Bruce_Gayliard]
# 
# Revision 1.1.2.2  1997/05/19  04:41:03  Bruce_Gayliard
# 	Place in better position in source tree.
# 	[1997/05/19  04:38:33  Bruce_Gayliard]
# 
# Revision 1.1.2.6  1997/05/16  16:14:14  Bruce_Gayliard
# 	Initial version of template merge capability.
# 	[1997/05/16  16:12:40  Bruce_Gayliard]
# 
# Revision 1.1.2.5  1997/05/15  22:15:35  Bruce_Gayliard
# 	Intermediate submit, progressed template code.
# 	[1997/05/15  22:14:16  Bruce_Gayliard]
# 
# Revision 1.1.2.4  1997/05/15  14:47:39  Bruce_Gayliard
# 	Remove binary event file for now, forcing a full recompilation.
# 	[1997/05/15  14:46:47  Bruce_Gayliard]
# 
# Revision 1.1.2.3  1997/05/14  22:08:57  Bruce_Gayliard
# 	Intermediate submit for build sync purposes.
# 	[1997/05/14  22:07:04  Bruce_Gayliard]
# 
# Revision 1.1.2.2  1997/05/08  19:40:14  Bruce_Gayliard
# 	LINUX fixes, config, and start of merge lib.
# 	[1997/05/08  19:25:14  Bruce_Gayliard]
# 
# $EndLog$
# 
# @(#)$RCSfile: evmmklib.sh,v $ $Revision: 1.1.1.1 $ (DEC) $Date: 2003/12/11 15:40:52 $
# 

# The environment variables expected in this script are:
# - EVM_TEMPLATE_DIR : Base directory containing event template sources.
# - EVM_BINARY_EVENT_FILE : File path of the binary event library.
# - EVM_DAEMON_LOG_FILE : File path of the log file for the daemon process.
# - EVM_SOURCE_PERMS* : Permissions of source files (must be exact).
# - EVM_PARSER : Program used to parse sources.
# - EVM_SOURCE_SUFFIX : Must be suffix to be considered valid source. 
# Execution of this script will:
# - Test the existence and access for the log file EVM_DAEMON_LOG_FILE.
#    If file does not exist, touch it and test for existence again.
#    If access denied, print message to console (/dev/console).
# - Test accessibility of the binary event file.
# - Use find(1) to traverse the files in the EVM_TEMPLATE_DIR and pass each
#   event source into the compilation program.
# - Append the output of the compilation program into the EVM_BINARY_EVENT_FILE.
# - If an error is detected, 
#   - Append an error indication to the log file.



# Set the internal defaults for the environment variables.

. /usr/sbin/evmcommandset

EVM_TEMPLATE_DIR_DEFAULT="/usr/share/evm/templates"
EVM_BINARY_EVENT_FILE_DEFAULT="/var/evm/sockets/evmbinlib"
EVM_DAEMON_LOG_FILE_DEFAULT="/var/evm/adm/logfiles/evmdaemon.log"
EVM_DAEMON_TEMP_FILE_DEFAULT="/var/evm/sockets/evmProcessEventSource"
EVM_SOURCE_PERMS_DEFAULTA="0640"
EVM_SOURCE_PERMS_DEFAULTB="0440"
EVM_SOURCE_PERMS_DEFAULTC="0600"
EVM_SOURCE_PERMS_DEFAULTD="0400"
EVM_PARSER_DEFAULT="/usr/sbin/evmmkbin"
EVM_PARSER_OPTIONS_DEFAULT=
EVM_SOURCE_SUFFIX_DEFAULT="evt"
EVM_IGNORE_SUFFIX_DEFAULT="evt_PRE*"
EVM_SOURCE_OWNERA_DEFAULT="root"
EVM_SOURCE_OWNERB_DEFAULT="bin"

CONSOLE=/dev/console
NULL=/dev/null

# This function prints a timestamp and identifier to the error log,
# prior to a message being written.
write_errlog_timestamp()
{   ${ECHO} `date +"%Y-%m-%d %T"`'  evmmklib: \c'
}

STATUS=0
# Need to export this to pass up from a "for" loop
export STATUS

# Set each environment value to default value if it is not already set.

EVM_TEMPLATE_DIR=${EVM_TEMPLATE_DIR:=${EVM_TEMPLATE_DIR_DEFAULT}}
EVM_BINARY_EVENT_FILE=${EVM_BINARY_EVENT_FILE:=${EVM_BINARY_EVENT_FILE_DEFAULT}}
EVM_DAEMON_LOG_FILE=${EVM_DAEMON_LOG_FILE:=${EVM_DAEMON_LOG_FILE_DEFAULT}}
EVM_DAEMON_TEMP_FILE=${EVM_DAEMON_TEMP_FILE:=${EVM_DAEMON_TEMP_FILE_DEFAULT}}
EVM_SOURCE_PERMSA=${EVM_SOURCE_PERMSA:=${EVM_SOURCE_PERMS_DEFAULTA}}
EVM_SOURCE_PERMSB=${EVM_SOURCE_PERMSB:=${EVM_SOURCE_PERMS_DEFAULTB}}
EVM_SOURCE_PERMSC=${EVM_SOURCE_PERMSC:=${EVM_SOURCE_PERMS_DEFAULTC}}
EVM_SOURCE_PERMSD=${EVM_SOURCE_PERMSD:=${EVM_SOURCE_PERMS_DEFAULTD}}
EVM_PARSER=${EVM_PARSER:=${EVM_PARSER_DEFAULT}}
EVM_PARSER_OPTIONS=${EVM_PARSER_OPTIONS:=${EVM_PARSER_OPTIONS_DEFAULT}}
EVM_SOURCE_SUFFIX=${EVM_SOURCE_SUFFIX:=${EVM_SOURCE_SUFFIX_DEFAULT}}
EVM_SOURCE_OWNERA=${EVM_SOURCE_OWNERA:=${EVM_SOURCE_OWNERA_DEFAULT}}
EVM_SOURCE_OWNERB=${EVM_SOURCE_OWNERB:=${EVM_SOURCE_OWNERB_DEFAULT}}
EVM_IGNORE_SUFFIX=${EVM_IGNORE_SUFFIX:=${EVM_IGNORE_SUFFIX_DEFAULT}}

OWNER_PATTERN="-user ${EVM_SOURCE_OWNERA} -o -user ${EVM_SOURCE_OWNERB}"
PERMS_PATTERN="-perm ${EVM_SOURCE_PERMSA} -o -perm ${EVM_SOURCE_PERMSB} -o \
    -perm ${EVM_SOURCE_PERMSC} -o -perm ${EVM_SOURCE_PERMSD}"

# Check if we should echo output to stdout or if we should just log
# everything to a log file.  Only the -v option is sought, all others
# input on the command line is ignored since users are not expected to
# call this script directly

vflag=
TEE="( tee -a ${EVM_DAEMON_LOG_FILE} > /dev/null )"

if [ "$1" = "-v" ]
then
  vflag=1
fi

if [ ! -z "$vflag" ]
then
  TEE="( tee -a ${EVM_DAEMON_LOG_FILE} )"
fi

# Make sure EVM_DAEMON_LOG_FILE is a regular file.  It is no longer allowed
# to be /dev/tty.  Output will be redirected to the log file and stdout
# unless the -v flag is missing.  

if [ ! -f ${EVM_DAEMON_LOG_FILE} ]
then
 EVM_DAEMON_LOG_FILE=${EVM_DAEMON_LOG_FILE_DEFAUL}
fi

# Make sure we have a place to log errors if they occur.
# Check the specified daemon log file for existence and write accessibility.
# If we cannot access the log file, try to touch it and set the mode.
# If we still can't get to it, set the log to go to the system console.

if [ ! -w ${EVM_DAEMON_LOG_FILE} ]
then
   ${TOUCH} ${EVM_DAEMON_LOG_FILE} 2> $NULL
   ${CHMOD} ${EVM_SOURCE_PERMSC} ${EVM_DAEMON_LOG_FILE} 2> $NULL
fi

if [ ! -w ${EVM_DAEMON_LOG_FILE} ]
then
   MSG="${EVM_DAEMON_LOG_FILE} is inaccessible"
   EVM_DAEMON_LOG_FILE=$CONSOLE
   ( write_errlog_timestamp; ${ECHO} $MSG ) | eval ${TEE}
fi



# If we can't get to the binary event file, doesn't make much sense to continue.
# First, make sure we get rid of any old version of the file.
# FUTURE: don't remove file; modify it by defuncting overwritten events, ...
#         right now, the following line forces a complete recompile.
${RM} -f ${EVM_BINARY_EVENT_FILE} 2> $NULL

if [ -w ${EVM_BINARY_EVENT_FILE} ]
then
   EVM_SOURCE_TIME_CHECK_OPTION="-newer ${EVM_BINARY_EVENT_FILE}"
else
   EVM_SOURCE_TIME_CHECK_OPTION=""
   ${TOUCH} ${EVM_BINARY_EVENT_FILE} 2> $NULL
   ${CHMOD} ${EVM_SOURCE_PERMSC} ${EVM_BINARY_EVENT_FILE} 2> $NULL
fi

if [ ! -w ${EVM_BINARY_EVENT_FILE} ]
then
   MSG="${EVM_BINARY_EVENT_FILE} is inaccessible"
   ( write_errlog_timestamp; ${ECHO} $MSG ) | eval ${TEE}
   exit 1
fi

if [ ! -d ${EVM_TEMPLATE_DIR} ]
then
   MSG="${EVM_TEMPLATE_DIR} does not exist"
   ( write_errlog_timestamp; ${ECHO} $MSG ) | eval ${TEE}
   exit 1
fi

# Determine if we are in a cluster doing a rolling upgrade and are on a
# member still running the old version

CLU_ROLL_INSTALL=0
case  "${OSTYPE}" in
Linux | linux-gnu | linux)
 ;;
*)
 ${CLU_GET_INFO} -q && \
 CLU_ROLL_INSTALL=`${SYSCONFIG} -q generic rolls_ver_lookup | ${AWK} '/rolls_ver_lookup/ {print $3}'`
;;
esac

if [ ${CLU_ROLL_INSTALL} = 1 ]
then
  ${CLU_UPGRADE} -q || CLU_ROLL_INSTALL=0
fi

# Set filter if in rolling upgrade so we will not report .Old.. files invalid
# if not then set it to cat to pass through the data

if [ ${CLU_ROLL_INSTALL} = 1 ]
then
	ROLL_FILTER="grep -v .Old.."
else
	ROLL_FILTER="cat"
fi

# Find and report any files with invalid names:
${FIND} ${EVM_TEMPLATE_DIR} -follow \( -type f -o -type l \) \
        ! -name "*.${EVM_SOURCE_SUFFIX}" \
		-a ! -name "*.${EVM_IGNORE_SUFFIX}" \
	-print  2>> ${EVM_DAEMON_LOG_FILE} | ${ROLL_FILTER} |
    while read FILE
    do
	REALTYPE=`ls -lLd ${FILE} | cut -c 1`
	if [ "${REALTYPE}" != "d" ]
	then
        	write_errlog_timestamp
		$ECHO Warning: ${FILE} ignored - name does not end with \
			\"\.${EVM_SOURCE_SUFFIX}\"
	fi
    done | eval ${TEE}

# if we are in a rolling upgrade on a member running tagged files then we
# need to find all instances of .Old.. files, strip off the .Old.. and use
# those files to build the template library.  If we do not then we may pick
# up new template files that we are not ready for.  
# But only do this for the default system directories, that is any system
# directory that is not local.  Process the local
# directory as we would for a system not in a roll.
# Otherwise find the valid template files as before.

if [ ${CLU_ROLL_INSTALL} = 1 ]
then
    ${RM} -f ${EVM_DAEMON_TEMP_FILE}
    for i in `${LS} ${EVM_TEMPLATE_DIR}`
	do
	if [ $i != "local" ]
	then
    ${FIND} ${EVM_TEMPLATE_DIR}/$i -follow -name ".Old..*.${EVM_SOURCE_SUFFIX}" \
                \( -type f -o -type l \)  \
		    \( \( ${OWNER_PATTERN} \) -a \( ${PERMS_PATTERN} \) \) \
		    -print  | sed -e 's/\.Old\.\.//' \
		    2>> ${EVM_DAEMON_LOG_FILE} 1>>${EVM_DAEMON_TEMP_FILE}
        fi
        done

  ${FIND} ${EVM_TEMPLATE_DIR}/local -follow -name "*.${EVM_SOURCE_SUFFIX}" \
                \( -type f -o -type l \) ${EVM_SOURCE_TIME_CHECK_OPTION} \
                \( \( ${OWNER_PATTERN} \) -a \( ${PERMS_PATTERN} \) \) \
		-print \
                2>> ${EVM_DAEMON_LOG_FILE} 1>>${EVM_DAEMON_TEMP_FILE}
else
# Now find the valid templates as done before
  ${FIND} ${EVM_TEMPLATE_DIR} -follow -name "*.${EVM_SOURCE_SUFFIX}" \
                \( -type f -o -type l \) ${EVM_SOURCE_TIME_CHECK_OPTION} \
                \( \( ${OWNER_PATTERN} \) -a \( ${PERMS_PATTERN} \) \) \
		-print \
                2>> ${EVM_DAEMON_LOG_FILE} 1>${EVM_DAEMON_TEMP_FILE}
fi

# Find and report any files with valid names but invalid
# ownership or permissions.

if [ ${CLU_ROLL_INSTALL} = 1 ]
then
  ${CAT} ${EVM_DAEMON_TEMP_FILE} | while read ea
  do
   ${FIND} $ea -follow \( -type f -o -type l \) \
	-name "*.${EVM_SOURCE_SUFFIX}" \
		-a ! -name "*.${EVM_IGNORE_SUFFIX}" \
        \( ! \( ${OWNER_PATTERN} \) -o ! \( ${PERMS_PATTERN} \) \) \
	-print  2>> ${EVM_DAEMON_LOG_FILE} |
    while read FILE
    do
        write_errlog_timestamp
	$ECHO Warning: ${FILE} ignored - incorrect owner or permissions
    done >> $EVM_DAEMON_LOG_FILE
 done
else
   ${FIND} ${EVM_TEMPLATE_DIR} -follow \( -type f -o -type l \) \
	-name "*.${EVM_SOURCE_SUFFIX}" \
		-a ! -name "*.${EVM_IGNORE_SUFFIX}" \
        \( ! \( ${OWNER_PATTERN} \) -o ! \( ${PERMS_PATTERN} \) \) \
	-print  2>> ${EVM_DAEMON_LOG_FILE} |
    while read FILE
    do
        write_errlog_timestamp
	$ECHO Warning: ${FILE} ignored - incorrect owner or permissions
    done >> $EVM_DAEMON_LOG_FILE
fi

# Process the templates:
for ea in `${CAT} ${EVM_DAEMON_TEMP_FILE}`
do
	${EVM_PARSER} ${EVM_PARSER_OPTIONS} ${EVM_BINARY_EVENT_FILE} ${ea} \
					2>> ${EVM_DAEMON_LOG_FILE}
	if [ ! "$?" = "0" ]
	then
		STATUS=2
	fi
done

${RM} ${EVM_DAEMON_TEMP_FILE}

exit ${STATUS}

