#!/bin/sh

# PROVIDE: qwfwd
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf or /etc/rc.conf.local to
# enable qwfwd:
# qwfwd_(instance_)?enable (bool):	Set to "NO" by default.
#					Set it to "YES" to enable qwfwd.
# qwfwd_(instance_)?args (str):		Custom additional arguments to be passed
#					to qwfwd (default empty).
# qwfwd_(instance_)?user (str):		User to run qwfwd as. Default to
#					"qwfwd" created by the port.
# qwfwd_(instance_)?log (path):		Console log file (default
#					/var/log/${name}(_instance)?.log).
# qwfwd_(instance_)?configdir (path):	Path to config file qwfwd.cfg
#					(default "/usr/local/etc/qwfwd").
# qwfwd_instances (str):		Set to "" by default.
#					If defined, list of instances to enable.

. /etc/rc.subr

case $0 in
/etc/rc*)
	# during boot (shutdown) $0 is /etc/rc (/etc/rc.shutdown),
	# so get the name of the script from $_file
	name=$_file
        ;;
*)
	name=$0
	;;
esac

name=${name##*/}
rcvar="${name}_enable"

load_rc_config "${name}"

eval "${rcvar}=\${${rcvar}:-'NO'}"
eval "__args=\${${name}_args:-''}"
eval "__user=\${${name}_user:-'qwfwd'}"
eval "__log=\${${name}_log:-/var/log/${name}.log}"
eval "__configdir=\${${name}_configdir:-'/usr/local/etc/qwfwd'}"
eval "${name}_chdir=${__configdir}"
eval "__instances=\${${name}_instances:-''}"

pidfiledir="/var/run"
pidfile="${pidfiledir}/${name}.pid"

if [ -n "$2" ]; then
        instance="$2"
	load_rc_config ${name}_${instance}
	case "${__instances}" in
	"$2 "*|*" $2 "*|*" $2"|"$2")
		eval "__args=\${${name}_${instance}_args:-${__args}}"
		eval "__user=\${${name}_${instance}_user:-${__user}}"
		eval "__log=\${${name}_${instance}_log:-/var/log/${name}_${instance}.log}"
		eval "__configdir=\${${name}_${instance}_configdir:-${__configdir}}"
		eval "${name}_chdir=${__configdir}"
		pidfile="${pidfiledir}/${name}_${instance}.pid"
	;;
	*)
		err 1 "$2 not found in ${name}_instances" ;;
	esac
else
	if [ -n "${__instances}" -a -n "$1" ]; then
		for instance in ${__instances}; do
			eval "_enable=\${${name}_${instance}_enable}"
			eval "__enable=\${_enable:-\${${name}_enable}}"
			case "${__enable}" in
			[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
				continue
			;;
			[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
			;;
			*)
				if [ -z "${_enable}" ]; then
					_var=${name}_enable
				else
					_var=${name}_${instance}_enable
				fi
				warn "Bad value '${__enable}' for ${_var}. " \
					"Instance ${instance} skipped."
				continue
			;;
			esac
			echo "===> ${name} instance: ${instance}"
			/usr/local/etc/rc.d/${name} $1 ${instance}
			retcode="$?"
			if [ "0${retcode}" -eq 0 ]; then
				success="${instance} ${success}"
			else
				failed="${instance} (retcode=${retcode}) ${failed}"
			fi
		done
		echo "===> ${name} instances success: ${success}"
		echo "===> ${name} instances failed: ${failed}"
		exit 0
	fi
fi

command="/usr/sbin/daemon"
command_args="-P ${pidfile} -u ${__user} -R 5 -f -H -o ${__log} -m 3 /usr/local/bin/qwfwd ${__args}"

run_rc_command "$1"
