#!/bin/ksh
#
# ident "@(#)utsyslogctl.ksh	1.4	05/06/01 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

set -u
########################################################################
#
# Configures the syslog entries
#
########################################################################


########################################################################
#
# Operation entry points, one for each keyword.  This is where the
# specific work for this feature happens.
#
########################################################################


#
# disable()
#
disable() {
	typeset USER_INFO
	typeset LOCAL1_INFO

	USER_INFO=`grep -n "user\.info.*${SUNWUTVAR}/log/messages" \
		$SYSLOG_CONF 2> /dev/null | \
		awk -F: '{ printf "-e \"%sd\" ", $1}'`

	LOCAL1_INFO=`grep -n "local1\.info.*${SUNWUTVAR}/log/admin_log" \
		$SYSLOG_CONF 2> /dev/null | \
		awk -F: '{ printf "-e \"%sd\" ", $1}'`

	if [ -n "$USER_INFO" -o -n "$LOCAL1_INFO" ]; then
		typeset TMPFILE=${SUNWUTVAR}/tmp/syslog.conf.$$
		umask 077
		cat /dev/null >$TMPFILE
		eval "sed $USER_INFO $LOCAL1_INFO $SYSLOG_CONF >> $TMPFILE"
		umask 133
		cat $TMPFILE > $SYSLOG_CONF
		rm -f $TMPFILE
		pkill -HUP syslogd
		if $VERB; then
			print "syslog entries disabled"
		fi
	fi

	return 0
}

#
# enable()
# NOTE: nothing will be done if the syslog entries already exist.
#
enable() {
	typeset TMPFILE=$SUNWUTVAR/tmp/syslog.conf.$$
	typeset UPDATED=false
	typeset ENTRY

	umask 077
	cat /dev/null >$TMPFILE

	ENTRY="`sed -n \"s:user\.info.*$SUNWUTVAR/log/messages:yes:p\" \
		$SYSLOG_CONF`"
	if [ -z "$ENTRY" ]; then
	  echo "user.info		$SUNWUTVAR/log/messages" >>$TMPFILE
	fi

	ENTRY="`sed -n \"s:local1\.info.*$SUNWUTVAR/log/admin_log:yes:p\" \
		$SYSLOG_CONF`"
	if [ -z "$ENTRY" ]; then
	  echo "local1.info		$SUNWUTVAR/log/admin_log" >>$TMPFILE
	fi

	if [ -s $TMPFILE ]; then
	  cat $TMPFILE >>$SYSLOG_CONF
	  UPDATED=true
	fi

	touch $SUNWUTVAR/log/messages
	touch $SUNWUTVAR/log/admin_log
	#
	# HUP the syslod
	if $UPDATED; then
	  pkill -HUP syslogd
	  if $VERB; then
		print "syslog entries enabled"
	  fi
	fi

	rm -f $TMPFILE
	return 0
}


#########################################
# LOCAL VARIABLES
# They must defined here to resolve the some of parameter resolution
# problem
#########################################
local_variables() {
	SYSLOG_CONF=/etc/syslog.conf
}


#########################################
#
# Configurable parameter
#
#########################################
# NONE

#########################################
#
# variables used in the shared script utctl-shlib
#
#########################################
DESCR="sets up the syslog entries for Sun Ray"

########################################################################
#
# Execution starts here
#

PROGPATH=`dirname $0`
. $PROGPATH/utctl-shlib
