#!/sbin/openrc-run
# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

command=/usr/bin/tor
pidfile=/run/tor/${RC_SVCNAME}.pid
command_args="--hush -f ${torrc} --runasdaemon 1 --pidfile \"${pidfile}\""
command_user="tor:tor"
capabilities="^cap_net_bind_service"
retry=${GRACEFUL_TIMEOUT:-60}
stopsig=INT
command_progress=yes

extra_commands="checkconfig"
extra_started_commands="reload"
description="Anonymizing overlay network for TCP"
description_checkconfig="Check for valid config file"
description_reload="Reload the configuration"

checkconfig() {
	if ! ${command} --verify-config --hush -f ${torrc} --user ${command_user%%:*} ; then
		eerror "Tor configuration (${torrc}) is not valid."
		eerror "Example is in /etc/tor/torrc.sample"
		return 1
	fi
}

start_pre() {
	checkconfig || return 1
	checkpath -d -m 0755 -o tor:tor /run/tor
}

reload() {
	checkconfig || return 1
	ebegin "Reloading Tor configuration"
	start-stop-daemon -s HUP --pidfile ${pidfile}
	eend $?
}
