#!/bin/sh

# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
    set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides:          datadome-proxyserver
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: <Enter a short description of the software>
# Description:       <Enter a long description of the software>
#                    <...>
#                    <...>
### END INIT INFO

# Author: Kirill Korinsky <kirill@korins.ky>

DESC="datadome-proxyserver"
NAME="datadome-proxy"
START_ARGS="--background --make-pidfile --chuid datadome:datadome"
PIDFILE="/var/run/datadome_proxy.pid"
DAEMON=/usr/sbin/datadome_proxyserver_watchdog.py
DAEMON_ARGS="/usr/sbin/datadome_proxyserver /etc/datadome/datadome_proxyserver.conf"

# Use a custom do_stop_cmd without check a name, kill process only by PID
do_stop_cmd()
{
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
            $STOP_ARGS --pidfile ${PIDFILE}
        RETVAL="$?"
        [ "$RETVAL" = 2 ] && return 2
        # Wait for children to finish too if this is a daemon that forks
        # and if the daemon is only ever run from this initscript.
        # If the above conditions are not satisfied then add some other code
        # that waits for the process to drop all resources that could be
        # needed by services started subsequently.  A last resort is to
        # sleep for some time.
        start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 \
            $STOP_ARGS --pidfile ${PIDFILE}
        [ "$?" = 2 ] && return 2
        # Many daemons don't delete their pidfiles when they exit.
        rm -f $PIDFILE
        return $RETVAL
}
