#!/bin/sh
# =============================================================================
# Trident HTTP Cache Proxy - FreeBSD rc.d service script
#
# PROVIDE: trident
# REQUIRE: LOGIN DAEMON NETWORKING
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable trident:
#   trident_enable="YES"
#
# Optional rc.conf variables:
#   trident_config="/usr/local/etc/trident/config.toml"
#   trident_user="trident"
#   trident_group="trident"
#   trident_flags=""
# =============================================================================

. /etc/rc.subr

name="trident"
rcvar="trident_enable"

load_rc_config $name

: ${trident_enable:="NO"}
: ${trident_config:="/usr/local/etc/trident/config.toml"}
: ${trident_user:="trident"}
: ${trident_group:="trident"}
: ${trident_flags:=""}
: ${trident_pidfile:="/var/run/trident.pid"}

pidfile="${trident_pidfile}"
command="/usr/local/bin/trident"
command_args="--config ${trident_config} ${trident_flags} &"

start_precmd="trident_precmd"
reload_cmd="trident_reload"
extra_commands="reload"

trident_precmd()
{
    # Verify config file exists
    if [ ! -f "${trident_config}" ]; then
        echo "trident: config file ${trident_config} not found"
        return 1
    fi

    # Ensure runtime directories exist
    install -d -o ${trident_user} -g ${trident_group} -m 0750 /var/db/trident
    install -d -o ${trident_user} -g ${trident_group} -m 0750 /var/log/trident
}

trident_reload()
{
    if [ -f "${pidfile}" ]; then
        echo "Reloading ${name} configuration."
        kill -HUP $(cat "${pidfile}")
    else
        echo "${name} is not running."
        return 1
    fi
}

run_rc_command "$1"
