#!/bin/sh
#
# PROVIDE: kea_sync_from_db
# REQUIRE: NETWORKING mysql
# KEYWORD: shutdown
#
# Installer vers /usr/local/etc/rc.d/kea_sync_from_db
# chmod +x /usr/local/etc/rc.d/kea_sync_from_db
# Activer dans /etc/rc.conf :
#   kea_sync_from_db_enable="YES"

. /etc/rc.subr

name="kea_sync_from_db"
rcvar="kea_sync_from_db_enable"

: ${kea_sync_from_db_python:="/usr/local/bin/python3.11"}
: ${kea_sync_from_db_script:="/usr/local/sbin/kea-sync-from-db.py"}
: ${kea_sync_from_db_config:="/usr/local/etc/kea/kea-sync-from-db.conf"}
: ${kea_sync_from_db_logfile:="/var/log/kea-sync-from-db.log"}
: ${kea_sync_from_db_pidfile:="/var/run/kea-sync-from-db.pid"}

pidfile="${kea_sync_from_db_pidfile}"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
status_cmd="${name}_status"

kea_sync_from_db_start()
{
    echo "Starting ${name}."
    touch "${kea_sync_from_db_logfile}"
    SYNC_SERVER_CONF="${kea_sync_from_db_config}" \
        /usr/sbin/daemon -r \
            -o "${kea_sync_from_db_logfile}" \
            -P "${kea_sync_from_db_pidfile}" \
            "${kea_sync_from_db_python}" "${kea_sync_from_db_script}"
}

kea_sync_from_db_stop()
{
    if [ ! -f "${kea_sync_from_db_pidfile}" ]; then
        echo "${name} not running."
        return 0
    fi
    _pid=$(cat "${kea_sync_from_db_pidfile}")
    if kill -0 "${_pid}" 2>/dev/null; then
        echo "Stopping ${name} (PID ${_pid})."
        kill "${_pid}"
        rm -f "${kea_sync_from_db_pidfile}"
    else
        echo "${name} not running (stale pidfile)."
        rm -f "${kea_sync_from_db_pidfile}"
    fi
}

kea_sync_from_db_status()
{
    if [ ! -f "${kea_sync_from_db_pidfile}" ]; then
        echo "${name} is not running."
        return 1
    fi
    _pid=$(cat "${kea_sync_from_db_pidfile}")
    if kill -0 "${_pid}" 2>/dev/null; then
        echo "${name} is running (PID ${_pid})."
        return 0
    else
        echo "${name} is not running (stale pidfile)."
        return 1
    fi
}

load_rc_config $name
run_rc_command "$1"
