#!/sbin/openrc-run

: ${user:="arti"}
: ${graceful_timeout:="${GRACEFUL_TIMEOUT:-60}"}

command="/usr/bin/arti"
: ${conffile:="/etc/arti/arti.toml"}
: ${command_args:="proxy --config $conffile"}
command_background="yes"
start_stop_daemon_args="--chdir /var/lib/arti --user $user"
pidfile="/run/${RC_SVCNAME}.pid"

extra_started_commands="gracefulstop reload"

description="Anonymizing overlay network for TCP written in Rust"
description_reload="Reload the configuration."
description_gracefulstop="Gracefully stop (wait $graceful_timeout until all connections are properly closed)."

depend() {
	need net
	after firewall
}

checkconfig() {
	if [ ! -f "${conffile}" ]; then
		eerror "Configuration file ${conffile} not found"
		eerror "An example file is provided by the arti-doc package:"
		eerror "  /usr/share/doc/arti-example-config.toml"
		return 1
	fi
}

start_pre() {
	checkconfig || return 1
}

gracefulstop() {
	ebegin "Gracefully stopping Arti (Tor), this can take up to $graceful_timeout seconds"
	start-stop-daemon --stop \
		--progress \
		--signal INT \
		--retry $graceful_timeout \
		--pidfile "$pidfile" \
		--exec $command -- $command_args
	eend $?
}

reload() {
	ebegin "Reloading Arti (Tor) configuration"
	start-stop-daemon --signal HUP --pidfile "$pidfile"
	eend $?
}
