#!/bin/ksh
#
# ident "@(#)utcronctl.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 crontab
#
########################################################################


#
# Removes crontab comments
#
# On Linux, the crontab automatically adds some comments to the
# top of the crontab every time we update the crontab.  We need
# to remove these comments.  Otherwise, we will end up with
# duplicated comment lines.  crontab adds the following text:
#
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.??? installed on Fri Apr 16 16:19:09 2004)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
#
# Parameter:
# $1 - file for the crontab entries
RemoveComment() {
	typeset TMPFILE_COMM=$SUNWUTVAR/tmp/crontab_comment.tmp.$$
	typeset CRONFILE=$1

	if [ $# -ne 1 ]; then
		return
	fi

	(set +e; head -1 $CRONFILE | \
	    grep "^# DO NOT EDIT THIS FILE - edit the master and reinstall" \
	    > /dev/null 2>&1)
	if [ $? -eq 0 ]; then
		# comment exists, remove them
		cat /dev/null >$TMPFILE_COMM
		head -3 $CRONFILE | sed \
		-e "/^# DO NOT EDIT THIS FILE - edit the master and reinstall/d" \
		-e "/^# (\/[^ ]* installed/d" \
		-e "/^# (Cron version/d" >> $TMPFILE_COMM
		sed -n '4,$p' $CRONFILE >> $TMPFILE_COMM
		rm -f $CRONFILE
		mv $TMPFILE_COMM $CRONFILE
	fi
}

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


#
# disable()
#
disable() {
	typeset TMPFILE=${SUNWUTVAR}/tmp/crontab.tmp.$$
	typeset CRONTAB_ST
	cat /dev/null >$TMPFILE

	if [ $OS = "SunOS" ]; then
		(set +e; crontab -l root 2>/dev/null >>$TMPFILE 2> /dev/null)
		CRONTAB_ST=$?
	else
		(set +e; crontab -u root -l 2>/dev/null >>$TMPFILE 2> /dev/null)
		CRONTAB_ST=$?
		if [ $CRONTAB_ST -eq 0 ]; then
			RemoveComment $TMPFILE
		fi
	fi

	if [ $CRONTAB_ST -eq 0 ]; then
		typeset MESSAGE_LOG=""
		typeset ADMIN_LOG=""

		MESSAGE_LOG=`grep -n "$SUNWUTLIB/utlog -c -d ${SUNWUTVAR}/log/messages" \
		    $TMPFILE 2> /dev/null | awk -F: '{ printf "-e \"%sd\" ", $1}'`

		ADMIN_LOG=`grep -n "$SUNWUTLIB/utlog -c -d ${SUNWUTVAR}/log/admin_log" \
		    $TMPFILE 2> /dev/null | awk -F: '{ printf "-e \"%sd\" ", $1}'`

		if [ -n "$MESSAGE_LOG" -o -n "$ADMIN_LOG" ]; then
			typeset NEWCRONTMP=${SUNWUTVAR}/tmp/crontab.new.tmp.$$
			cat /dev/null >$NEWCRONTMP
			eval "sed $MESSAGE_LOG $ADMIN_LOG $TMPFILE >> $NEWCRONTMP"

			crontab $NEWCRONTMP
			rm -f $NEWCRONTMP
		fi
	fi
	if $VERB; then
		print "Crontab entries disabled"
	fi
	rm -f $TMPFILE
	return 0
}

#
# enable()
# NOTE: nothing will be done if the cron entries already exist.
#
enable() {
	typeset UTLOG_CMD="$SUNWUTLIB/utlog -c -d $SUNWUTVAR/log"
	typeset ENTRY_ADDED=false
	typeset TMPFILE=$SUNWUTVAR/tmp/crontab.tmp.$$

	umask 077
	cat /dev/null >$TMPFILE

	OS=`uname -s`
	if [ $OS = "SunOS" ]; then
		(set +e; crontab -l root 2>/dev/null >>$TMPFILE 2> /dev/null)
	else
		(set +e; crontab -u root -l 2>/dev/null >>$TMPFILE 2> /dev/null)
		if [[ $? -eq 0 ]]; then
			RemoveComment $TMPFILE
		fi
    	fi

	ENTRY="`sed -n \"s:$UTLOG_CMD/messages:yes:p\" $TMPFILE`"
	if [ -z "$ENTRY" ]; then
		print "3 3 * * * $UTLOG_CMD/messages 1>/dev/null 2>&1" >>$TMPFILE
		ENTRY_ADDED=true
	fi

	ENTRY="`sed -n \"s:$UTLOG_CMD/admin_log:yes:p\" $TMPFILE`"
	if [ -z "$ENTRY" ]; then
		print "33 3 * * * $UTLOG_CMD/admin_log 1>/dev/null 2>&1" >>$TMPFILE
		ENTRY_ADDED=true
	fi

	if $ENTRY_ADDED; then
		crontab $TMPFILE
	fi
	if $VERB; then
		print "Crontab entries enabled"
	fi

	rm -f $TMPFILE
	return 0
}


#########################################
# LOCAL VARIABLES
# They must defined here to resolve the some of parameter resolution
# problem
#########################################
local_variables() {
	return
}


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

#########################################
#
# variables used in the shared script utctl-shlib
#
#########################################
DESCR="sets up the crontab to rotate the Sun Ray logfiles"

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

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