#!/bin/sh

# PROVIDE: nuster
# REQUIRE: DAEMON LOGIN
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable nuster:
#
# nuster_enable (bool):	default: "NO"
#				Set to "YES" to enable nuster
# nuster_pidfile (str):	default: /var/run/nuster.pid
#				Set to the full path of the pid file
# nuster_config (str):		default: /usr/local/etc/nuster.conf
#				Set to the full path of the config file
# nuster_flags (str):		default: Autogenerated using pidfile and config options
#				Set to override with your own options
# nuster_profiles (str):	default: empty
# Set to space-separated list of profiles: for each profile separate nuster
# process will be spawned, with nuster-${profile}.conf config file.
# You can override default pidfile and config file for each profile with
# nuster_${profile}_config and nuster_${profile}_pidfile.

. /etc/rc.subr

name="nuster"
rcvar=nuster_enable
command="/usr/local/sbin/nuster"
extra_commands="reload configtest hardstop hardreload"
reload_cmd="nuster_reload"
hardreload_cmd="nuster_reload"
hardreload_precmd="def_hardreload_option"
stop_cmd="nuster_stop"
hardstop_cmd="nuster_stop"
hardstop_precmd="def_hardstop_signal"

: ${nuster_enable:="NO"}
: ${nuster_config:="/usr/local/etc/${name}.conf"}
pidfile=${nuster_pidfile:-"/var/run/nuster.pid"}

def_hardreload_option()
{
    reload_opt="-st"
}

def_hardstop_signal()
{
    sig_stop="TERM"
}

load_rc_config $name

is_valid_profile() {
    local profile
    for profile in $nuster_profiles; do
        if [ "$profile" = "$1" ]; then
            return 0
        fi
    done
    return 1
}

if [ -n "$2" ]; then
    profile=$2
    if ! is_valid_profile $profile; then
        echo "$0: no such profile ($profile) defined in ${name}_profiles."
        exit 1
    fi
    eval nuster_config="\${nuster_${profile}_config:-/usr/local/etc/nuster-${profile}.conf}"
    eval pidfile="\${nuster_${profile}_pidfile:-/var/run/nuster-${profile}.pid}"
else
    if [ "x${nuster_profiles}" != "x" -a "x$1" != "x" ]; then
        for profile in ${nuster_profiles}; do
            echo "===> ${name} profile: ${profile}"
            /usr/local/etc/rc.d/nuster $1 ${profile}
            retcode="$?"
            if [ ${retcode} -ne 0 ]; then
                failed="${profile} (${retcode}) ${failed:-}"
            else
                success="${profile} ${success:-}"
            fi
        done
        exit 0
    fi
fi

: ${nuster_flags:="-q -f ${nuster_config} -p ${pidfile}"}
configtest_cmd="$command -c -f $nuster_config"
start_precmd="$command -q -c -f $nuster_config"
required_files=$nuster_config
sig_stop=SIGUSR1
reload_opt="-sf"

nuster_reload()
{
	${command} -q -c -f ${nuster_config}
	if [ $? -ne 0 ]; then
	    err 1 "Error found in ${nuster_config} - not reloading current process!"
	fi
	rc_pid=$(check_pidfile ${pidfile} ${command})
	if [ $rc_pid ]; then
		${command} ${nuster_flags} $reload_opt $(cat ${pidfile})
	else
		_run_rc_notrunning
		return 1
	fi
}

nuster_stop()
{
	rc_pid=$(check_pidfile ${pidfile} ${command})
	if [ $rc_pid ]; then
		rc_pid=$(cat ${pidfile})
		kill -$sig_stop $rc_pid
		wait_for_pids $rc_pid
	else
		_run_rc_notrunning
		return 1
	fi
}

run_rc_command "$1"
