#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: syslogd
# REQUIRE: cleanvar netif
# BEFORE:  SERVERS

. /etc/rc.subr

name="syslogd"
rcvar=`set_rcvar`
pidfile="/var/run/syslog.pid"
command="/usr/sbin/${name}"
required_files="/etc/syslog.conf"
start_precmd="syslogd_precmd"
start_postcmd="syslogd_poststart"
extra_commands="reload"

sockfile="/var/run/syslogd.sockets"
evalargs="rc_flags=\"\`set_socketlist\` \$rc_flags\""
altlog_proglist="named"

syslogd_precmd()
{
	local _l _ldir

	#	Transitional symlink for old binaries
	#
	if [ ! -L /dev/log ]; then
		ln -sf /var/run/log /dev/log
	fi
	killall syslogd 2> /dev/null
	rm -f /var/run/log

	#	Create default list of syslog sockets to watch
	#
	( umask 002 ; > $sockfile )

	#	If running named(8) or ntpd(8) chrooted, added appropriate
	#	syslog socket to list of sockets to watch.
	#
	for _l in $altlog_proglist; do
		eval _ldir=\$${_l}_chrootdir
		if checkyesno `set_rcvar $_l` && [ -n "$_ldir" ]; then
			echo "${_ldir}/var/run/log" >> $sockfile
		fi
	done

	#	If other sockets have been provided, change run_rc_command()'s
	#	internal copy of $syslogd_flags to force use of specific
	#	syslogd sockets.
	#
	if [ -s $sockfile ]; then
		echo "/var/run/log" >> $sockfile
		eval $evalargs
	fi

	# Create all log files which do not already exist.
	#
	# begin Isilon
	## Choose a newsyslog.conf based on the size of the /var partition
	if [ ! -e /etc/newsyslog.conf ] ; then
		local var_size
		var_size=$(/bin/df -b /var | awk '$NF == "/var" { print $2 }')
		# threshold at 1000 base-10 MB
		if [ -n "$var_size" ] && [ "$var_size" -gt 1953125 ] ; then
			# gmirror reports 2026136 sectors = 989 MiB = 1037 MB
			ln -sf /etc/newsyslog.conf.1000MB /etc/newsyslog.conf
		else
			# imdd reports 1016286 sectors = 496 MiB = 520 MB
			ln -sf /etc/newsyslog.conf.500MB /etc/newsyslog.conf
		fi
	fi

	# Add -N for faster booting.  Let cron handle rotating
	/usr/sbin/newsyslog -CC -N
	# end Isilon

	return 0
}

syslogd_poststart()
{
	logger `/sbin/sysctl kern.boottime`
	return 0;
}

set_socketlist()
{
	local _s _socketargs

	_socketargs=
	for _s in `cat $sockfile | tr '\n' ' '` ; do
		_socketargs="-l $_s $_socketargs"
	done
	echo $_socketargs
}
load_rc_config $name
run_rc_command "$1"
