#!/bin/sh

# PROVIDE: sw_cp_server
# REQUIRE: DAEMON
# KEYWORD: shutdown

. /etc/rc.subr

name=sw_cp_server
rcvar=`set_rcvar`

load_rc_config $name

command=/usr/local/sbin/sw-cp-serverd

CONFIGFILE=/usr/local/etc/sw-cp-server/config
command_args="-f $CONFIGFILE"
pidfile=/var/run/sw-cp-server.pid

if [ -e "$pidfile" ]; then
    if [ -z "`check_pidfile $pidfile $command`" ]; then
        # stale pidfile
        rm -f $pidfile
    fi
fi

extra_commands=reload

reload_cmd=reload_cmd
stop_postcmd="remove_stale_pid"
start_precmd="remove_stale_pid"

remove_stale_pid()
{
	if [ -e "$pidfile" ]; then
	    if [ -z "`check_pidfile $pidfile $command`" ]; then
		# stale pidfile
		rm -f $pidfile
	    fi
	fi
}

reload_cmd()
{
    if [ -e "$pidfile" -a -n "`check_pidfile $pidfile $command`" ]; then
        ( nohup "$0" impl_reload_daemon 2>&1           | logger -t sw-cp-server_init -p daemon.notice
        ) >/dev/null 2>&1 &
    else
        echo "sw-cp-server not running"
    fi
}

if [ "$1" = impl_reload_daemon ]; then
    sig_stop=INT
    run_rc_command restart
else
    run_rc_command "$1"
fi

