#!/bin/sh
# $Header: emll/lib/emlogger /main/1 2008/08/15 11:33:02 jsutton Exp $
#
# emlogger
#
# Copyright (c) 2005, 2008, Oracle. All rights reserved.
#
#    NAME
#      emlogger - OCM OS messages logging
#
#    DESCRIPTION
#      This script is invoked by OCM to write messages to the OS syslog
#      (using the logger unix utility)
#
#    EXIT CODES
#      0 - Success
#      1 - Missing an argument value
#      2 - An unknown argument
#      3 - Missing the messgae argument
#     99 - No logger utility
#--

# Constant declarations for exit values.
SUCCESS=0
ERR_MISSING_ARG_VAL=1
ERR_UNKNOWN_ARG=2
ERR_MISSING_MSG_ARG=3
ERR_NO_LOGGER=99

#
# Reset the locale to ENGLISH for command line operations.
LC_ALL=C
export LC_ALL

# Set the variables that map to explicit paths to prevent from trojan
# horse type attacks and allow for more consistent installation experience
# by eliminating the use of aliases.
_binDir=/bin
_usrBinDir=/usr/bin
_usrLocalBinDir=/usr/local/bin

# Set the arguments default values
_facility="daemon"
_level="warning"


if [ -f ${_binDir}/logger ]
then
    LOGGER=${_binDir}/logger
elif [ -f ${_usrBinDir}/logger ]
then
    LOGGER=${_usrBinDir}/logger
elif [ -f ${_usrLocalBinDir}/logger ]
then
    LOGGER=${_usrLocalBinDir}/logger
else
    exit $ERR_NO_LOGGER
fi

usage()
{
   cat <<EOF
Usage: $0 [ -f <facility> ] [ -l <level> ] -m <message>

   where:

      -f <facility>   The message facility (the default is "daemon")
      -l <level>      The message level (the default is "warning")
      -m <message>    The message to log (can not contain %) 

EOF
}
 
#
# Accept the command line form for options
while getopts ":m:l:f:" _option
do
    case "${_option}"
    in
        "f")    _facility=${OPTARG};;
        "l")    _level=${OPTARG};;
        "m")    _message=${OPTARG};;
        ":")    usage 1>&2
                exit $ERR_MISSING_ARG_VAL;;
          *)    usage 1>&2
                exit $ERR_UNKNOWN_ARG;;
    esac
done

if [ -z "${_message}" ]
then
    usage 1>&2
    exit $ERR_MISSING_MSG_ARG
fi

# Run the logger command
$LOGGER -p  ${_facility}.${_level} "${_message}"
exit 0
