#!/bin/sh
#
# PROVIDE: cyclingbot
# REQUIRE: NETWORKING
# KEYWORD: shutdown
#
# Add the following to /etc/rc.conf inside your jail to enable:
#   cyclingbot_enable="YES"

. /etc/rc.subr

name="cyclingbot"
rcvar="cyclingbot_enable"

desc="Cycling Discord Bot"
command="/usr/local/bin/cycling-bot"
pidfile="/var/run/${name}.pid"

cyclingbot_enable="${cyclingbot_enable:-NO}"
cyclingbot_user="cyclingbot"
cyclingbot_chdir="/var/db/cyclingbot"

# Use daemon(8) to daemonize the process and write a pidfile
start_cmd="${name}_start"
stop_cmd="${name}_stop"

cyclingbot_start()
{
    echo "Starting ${desc}."
    /usr/sbin/daemon \
        -u "${cyclingbot_user}" \
        -p "${pidfile}" \
        -o /var/log/cyclingbot.log \
        /bin/sh -c "cd ${cyclingbot_chdir} && exec ${command}"
}

cyclingbot_stop()
{
    echo "Stopping ${desc}."
    if [ ! -f "${pidfile}" ]; then
        echo "${name} is not running."
        return 0
    fi
    pid=$(cat "${pidfile}")
    kill -TERM "${pid}" 2>/dev/null
    wait_for_pids "${pid}"
    rm -f "${pidfile}"
}

load_rc_config "${name}"
run_rc_command "$1"
