#!/bin/sh

# PROVIDE: rmm_updater
# REQUIRE: NETWORKING rmm_agent
# KEYWORD: shutdown

. /etc/rc.subr

name="rmm_updater"
rcvar="${name}_enable"
command="/usr/local/rmm/rmm-updater"
pidfile="/var/run/${name}.pid"
command_args=""

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

rmm_updater_start() {
    if [ -f "$pidfile" ]; then
        if kill -0 $(cat "$pidfile") 2>/dev/null; then
            echo "${name} already running"
            return 0
        fi
        rm -f "$pidfile"
    fi

    echo "Starting ${name}."
    /usr/sbin/daemon -f -p "$pidfile" "$command" $command_args
}

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

rmm_updater_status() {
    if [ -f "$pidfile" ]; then
        pid=$(cat "$pidfile")
        if kill -0 "$pid" 2>/dev/null; then
            echo "${name} is running as pid $pid"
            return 0
        fi
    fi
    echo "${name} is not running"
    return 1
}

load_rc_config $name
: ${rmm_updater_enable:="NO"}
run_rc_command "$1"
