#!/sbin/openrc-run
# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

: ${SYSLOG_NG_CONFIGFILE:=/etc/syslog-ng/${RC_SVCNAME}.conf}
: ${SYSLOG_NG_STATEFILE_DIR:=/var/lib/syslog-ng}
: ${SYSLOG_NG_STATEFILE:=${SYSLOG_NG_STATEFILE_DIR}/syslog-ng.persist}
: ${SYSLOG_NG_PIDFILE_DIR:=/run}
: ${SYSLOG_NG_PIDFILE:=${SYSLOG_NG_PIDFILE_DIR}/${RC_SVCNAME}.pid}

: ${SYSLOG_NG_GROUP:=root}
: ${SYSLOG_NG_USER:=root}

command="/usr/sbin/syslog-ng"
command_args="--persist-file \"${SYSLOG_NG_STATEFILE}\" --cfgfile \"${SYSLOG_NG_CONFIGFILE}\" --pidfile \"${SYSLOG_NG_PIDFILE}\" ${SYSLOG_NG_OPTS}"
extra_commands="checkconfig"
extra_started_commands="reload"
pidfile="${SYSLOG_NG_PIDFILE}"
start_stop_daemon_args="--user \"${SYSLOG_NG_USER}\":\"${SYSLOG_NG_GROUP}\""
description="Syslog-ng is a syslog replacement with advanced filtering features."
description_checkconfig="Check the configuration file that will be used by \"start\""
description_reload="Reload the configuration without exiting"
required_files="${SYSLOG_NG_CONFIGFILE}"
required_dirs="${SYSLOG_NG_PIDFILE_DIR}"

depend() {
	use clock
	need hostname localmount
	after bootmisc
	provide logger
}

checkconfig() {
	ebegin "Checking your configfile (${SYSLOG_NG_CONFIGFILE})"
	syslog-ng -s -f "${SYSLOG_NG_CONFIGFILE}"
	eend $? "Configuration error. Please fix your configfile (${SYSLOG_NG_CONFIGFILE})"
}

start_pre() {
	checkconfig || return 1
	checkpath \
		-d \
		--mode 0700 \
		--owner "${SYSLOG_NG_USER}:${SYSLOG_NG_GROUP}" \
		"${SYSLOG_NG_STATEFILE_DIR}"
}

stop_pre() {
	[ "$RC_CMD" = "restart" ] && sleep 1
	return 0
}

reload() {
	checkconfig || return 1
	ebegin "Reloading configuration and re-opening log files"
	start-stop-daemon --signal HUP --pidfile "${pidfile}"
	eend $?
}
