#!/bin/ksh -p
#
# ident "@(#)utsyslogctl.ksh	1.11	12/08/01 Oracle"
#
# Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
#

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

PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH

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


#
# disable()
#
disable() {
	typeset TMPFILE=${SUNWUTVAR}/tmp/syslog.conf.$$

	umask 077
	rm -f $TMPFILE
	sed -e "\,^[ 	]*user\.info[ 	]*${SUNWUTVAR}/log/messages[ 	]*\$,d" \
		-e "\,^[ 	]*daemon\.info[ 	]*${SUNWUTVAR}/log/messages[ 	]*\$,d" \
		-e "\,^[ 	]*local1\.info[ 	]*${SUNWUTVAR}/log/admin_log[ 	]*\$,d" \
		$SYSLOG_CONF > $TMPFILE
	umask 133

	if ! cmp $TMPFILE $SYSLOG_CONF >/dev/null; then
		cp $TMPFILE $SYSLOG_CONF
		if [[ "$(uname -s)" == Linux ]]; then
			service $SYSLOGD_SVC restart
		else
			svcadm restart svc:/system/system-log:default
		fi
		if $VERB; then
			print "syslog entries disabled"
		fi
	fi
	rm -f $TMPFILE

	return 0
}

#
# enable()
# NOTE: nothing will be done if the syslog entries already exist.
#
enable() {
	typeset TMPFILE=$SUNWUTVAR/tmp/syslog.conf.$$
	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:daemon\.info[ 	].*$SUNWUTVAR/log/messages:yes:p" \
		$SYSLOG_CONF)
	if [ -z "$ENTRY" ]; then
	  echo "daemon.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
	fi

	touch $SUNWUTVAR/log/messages
	touch $SUNWUTVAR/log/admin_log
	#
	# restart the syslogd
	# Note: HUP no longer works with rsyslogd
	if [[ "$(uname -s)" == Linux ]]; then
		service $SYSLOGD_SVC restart
	else
		svcadm restart svc:/system/system-log:default
	fi
	if $VERB; then
		print "syslog entries enabled"
	fi

	rm -f $TMPFILE
	return 0
}


#########################################
# LOCAL VARIABLES
# They must defined here to resolve the some of parameter resolution
# problem
#########################################
local_variables() {
	if [ -f /etc/rsyslog.conf ]; then
		SYSLOG_CONF=/etc/rsyslog.conf
		SYSLOGD_SVC=rsyslog
	elif [ -f /etc/syslog.conf ]; then
		SYSLOG_CONF=/etc/syslog.conf
		SYSLOGD_SVC=syslog
	else
		print -u2 "No syslog config file present on system"
		return 1
	fi
	return 0
}


#########################################
#
# 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
