#!/sbin/openrc-run
# Copyright 1999-2019 Gentoo Authors
# 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/${RC_SVCNAME}}
: ${SYSLOG_NG_STATEFILE:=${SYSLOG_NG_STATEFILE_DIR}/${RC_SVCNAME}.persist}
: ${SYSLOG_NG_PIDFILE_DIR:=/run}
: ${SYSLOG_NG_PIDFILE:=${SYSLOG_NG_PIDFILE_DIR}/${RC_SVCNAME}.pid}
: ${SYSLOG_NG_CONTROLFILE:=${SYSLOG_NG_PIDFILE_DIR}/${RC_SVCNAME}.ctl}

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

command="/usr/sbin/syslog-ng"
command_args="--cfgfile \"${SYSLOG_NG_CONFIGFILE}\" --control \"${SYSLOG_NG_CONTROLFILE}\" --persist-file \"${SYSLOG_NG_STATEFILE}\" --pidfile \"${SYSLOG_NG_PIDFILE}\" ${SYSLOG_NG_OPTS}"
command_user="${SYSLOG_NG_USER}:${SYSLOG_NG_GROUP}"
extra_commands="checkconfig"
extra_started_commands="reload"
pidfile="${SYSLOG_NG_PIDFILE}"
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 $?
}
