#!/bin/sh
# shellcheck disable=SC2034

# PROVIDE: zrepl
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf to enable this
# service:
#
# zrepl_enable (bool):  Set to YES to enable the zrepl service.
#                       Default: NO
# zrepl_config (str):   File containing zrepl configuration details.
#                       Default: /usr/local/etc/zrepl/zrepl.yml
# zrepl_user (str):     The user account used to run the zrepl daemon.
#                       Do not specifically set this to an empty string as this
#                       will cause the daemon to run as root.
#                       Default: root
# zrepl_group (str):    The group account used to run the zrepl daemon.
#                       Do not specifically set this to an empty string as this
#                       will cause the daemon to run with group wheel.
#                       Default: wheel
# zrepl_flags (str):    Extra flags passed to zrepl
#                       Default: ""
# zrepl_facility (str): Syslog facility to use
#                       Default: local0
# zrepl_priority (str): Syslog priority to use
#                       Default: alert
#
# zrepl_gracefully (bool): Set to YES to always stop the zrepl service
#                          gracefully.
#                          Default: NO

# shellcheck disable=SC1094
. /etc/rc.subr

name="zrepl"
rcvar="${name}_enable"
load_rc_config "$name"

: "${zrepl_enable:=NO}"
: "${zrepl_config:=/usr/local/etc/zrepl/zrepl.yml}"
: "${zrepl_user:=root}"
: "${zrepl_group:=wheel}"
: "${zrepl_flags:=}"
: "${zrepl_facility:=local0}"
: "${zrepl_priority:=alert}"
: "${zrepl_options:=$zrepl_flags --config $zrepl_config}"
: "${zrepl_gracefully:=NO}"

: "${zrepl_control_group:=$zrepl_group}"
: "${zrepl_control_mode:=0700}"

pidfile="/var/run/zrepl.pid"
command="/usr/sbin/daemon"
procname="/usr/local/bin/zrepl"
command_args="-p $pidfile -S -l $zrepl_facility -s $zrepl_priority -T $name \
  $procname $zrepl_options daemon"

start_precmd="zrepl_precmd"
restart_precmd="zrepl_checkconfig"
configtest_cmd="zrepl_checkconfig"
graceful_cmd="zrepl_graceful"
gracefulstop_cmd="zrepl_gracefulstop"
reload_precmd="zrepl_reload_precmd"
extra_commands="configtest graceful gracefulstop reload"

zrepl_checkconfig() {
    echo "Performing sanity check on $name configuration:"
    eval "$procname" "$zrepl_options" configcheck
}

zrepl_precmd() {
    if [ ! -d "/var/run/zrepl" ]; then
        install -d -g "$zrepl_control_group" -o "$zrepl_user" \
                -m "$zrepl_control_mode"   "/var/run/zrepl"
    fi
    zrepl_checkconfig
}

# shellcheck disable=SC2086,SC2154
zrepl_graceful() {
    local _rc_prefix _return
		( run_rc_command gracefulstop $rc_extra_args )
		( run_rc_command start $rc_extra_args )
		_return=$?
		[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
    return $_return
}

zrepl_gracefulstop() {
    if [ -z "$rc_pid" ]; then
        echo 1>&2 "$name not running? (check $pidfile)."
        return 1
    fi

    echo "Gracefully stopping $name."
    eval "$procname" signal shutdown
    wait_for_pids "$rc_pid"
}

zrepl_reload_precmd() {
    echo "Reloading $name."
}

if checkyesno zrepl_gracefully; then
    stop_cmd="zrepl_gracefulstop"
fi

run_rc_command "$1"
