#!/sbin/openrc-run

name=$RC_SVCNAME
description="sing-box service"
supervisor="supervise-daemon"
command="/usr/bin/sing-box"
extra_started_commands="reload checkconfig"

: ${SINGBOX_CONFIG="/etc/sing-box"}

if [ -d "$SINGBOX_CONFIG" ]; then
	_config_opt="-C $SINGBOX_CONFIG"
elif [ -z "$SINGBOX_CONFIG" ]; then
	_config_opt=""
else
	_config_opt="-c $SINGBOX_CONFIG"
fi


command_args="run --disable-color
	-D ${SINGBOX_WORKDIR:-"/var/lib/sing-box"}
	$_config_opt"

depend() {
	after net dns
}

checkconfig() {
	ebegin "Checking $RC_SVCNAME configuration"
	sing-box check $_config_opt
	eend $?
}

start_pre() {
	checkconfig
}

reload() {
	ebegin "Reloading $RC_SVCNAME"
	checkconfig && $supervisor "$RC_SVCNAME" --signal HUP
	eend $?
}
