#!/bin/sh

# Salt Proxy startup script
#
# PROVIDE: salt_proxy
# REQUIRE: LOGIN
# KEYWORD: shutdown

# Add the following to /etc/rc.conf[.local] to enable this service
#
# salt_proxy_enable (bool):        Set to NO by default.
#               Set it to YES to enable salt_proxy.
# salt_proxy_paths (string):       Set to "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin" by default.
#               Default $PATH for Salt
# salt_proxy_eggcache (string):    Set to "/tmp" by default.
#               Allows defining egg cache directory to fix runtime on diskless systems.
# salt_proxy_list (string):        Set to "" by default.
#               Space separated list of proxies.
#

. /etc/rc.subr

name=salt_proxy
rcvar=salt_proxy_enable

load_rc_config ${name}

: ${salt_proxy_enable:=NO}
: ${salt_proxy_paths=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin}
: ${salt_proxy_eggcache=/tmp}

start_cmd=salt_proxy_start

command="/usr/local/bin/salt-proxy"
command_interpreter="/usr/local/bin/python3.8"
required_files="/usr/local/etc/salt"
command_args="-c ${required_files} -d"

export PATH="${salt_proxy_paths}"
export PYTHON_EGG_CACHE="${salt_proxy_eggcache}"

salt_proxy_start()
{
    if [ ! -n "${salt_proxy_list}" ]; then
        echo "${salt_proxy_list} is undefined"
        return 1
    fi

    local _proxy

    for _proxy in ${salt_proxy_list}; do
        echo "Starting salt-proxy: ${_proxy}"
        ${command_interpreter} ${command} --proxyid ${_proxy} ${command_args}
    done
}

run_rc_command "$1"
