#!/bin/sh

# startup script for FreeBSD 6.x.
# Written by Tamas Pal <folti@balabit.hu>

# PROVIDE: syslogd
# REQUIRE: mountcritremote cleanvar newsyslog
# BEFORE: SERVERS

# Enabling syslog-ng:
# * copy this script to /etc/rc.d or /usr/local/etc/rc.d
# * Edit /etc/rc.conf and add the following lines:
#   ----- CUT HERE ----- 
# syslogd_enable="NO"
# syslogng_enable="YES"
# syslogng_flags=""
#   ----- CUT HERE ----- 
# * Add your extra flags to syslogng_flags EXCEPT the -p <pidfile> and -f
#   <conffile> flags. These are added automatically by this script. To set
#   their value, change the value of the conf_file and pidfile variables below

# BalaBit's platform independent variables. Please do not modify. 
SYSLOGNG_PREFIX=/opt/syslog-ng

. "/etc/rc.subr"


# FreeBSD rc specific variables. Please do not modify. 
name="syslogng"
rcvar=`set_rcvar`
pidfile="$SYSLOGNG_PREFIX/var/run/syslog-ng.pid"
command="$SYSLOGNG_PREFIX/sbin/syslog-ng"
required_files="$SYSLOGNG_PREFIX/etc/syslog-ng.conf"

start_precmd="syslog_ng_start_precmd"
start_postcmd="syslog_ng_start_postcmd"
stop_postcmd="syslog_ng_stop_postcmd"
reload_precmd="syslog_ng_reload_precmd"
extra_commands="reload"

# get original syslog pidfile from initscript.
_syslogd_pidfile=`sed -n -e 's/^pidfile="\([^"]*\)"/\1/p' /etc/rc.d/syslogd 2> /dev/null`
syslog_ng_start_precmd() {
	if [ ! -L /dev/log ]; then
		ln -s /var/run/log /dev/log
	fi
}

syslog_ng_reload_precmd() {
	$command --syntax-only
	_rval=$?
	if [ $_rval -ne 0 ];then
		exit $_rval
	fi
}

syslog_ng_start_postcmd() {
	if [ -n "$_syslogd_pidfile" ]; then
		# remove symlinks
		if [ -h $_syslogd_pidfile ]; then
			rm -f $_syslogd_pidfile
		fi
		if [ ! -f $_syslogd_pidfile ]; then
			ln -s "$pidfile" "$_syslogd_pidfile"
		fi
	fi
}

syslog_ng_stop_postcmd() {
	if [ -n "$_syslogd_pidfile" ]; then
		if [ -h $_syslogd_pidfile ]; then
			rm -f "$_syslogd_pidfile"
		fi
	fi
}

load_rc_config "$name"

case $1 in
	start)
		# Reportedly needed on 7.x.
		PATH=$PATH:$SYSLOGNG_PREFIX/sbin
		export PATH
		syslogng_flags="${syslog_ng_flags}"
	;;
esac

run_rc_command "$1"

# vim: ft=sh 
