#!/sbin/openrc-run
# shellcheck shell=sh

instance=${RC_SVCNAME#*.}

description="Anubis HTTP defense proxy (instance ${instance})"
supervisor="supervise-daemon"
command="/usr/bin/anubis"
command_args="\
	-bind ${ANUBIS_BIND_PORT:-/run/anubis_${instance?}/anubis.sock -bind-network unix} \
	-metrics-bind ${ANUBIS_METRICS_BIND_PORT:-/run/anubis_${instance?}/metrics.sock -metrics-bind-network unix} \
	-target ${ANUBIS_TARGET:-http://localhost:3923} \
	-difficulty ${ANUBIS_DIFFICULTY:-4} \
	${ANUBIS_OPTS}
"
command_background=1
pidfile="/run/anubis_${instance?}/anubis.pid"

: "${command_user:=anubis:anubis}"

depend() {
	use net firewall
}

start_pre() {
	if [ "${instance?}" = "${RC_SVCNAME?}" ]; then
		eerror "${RC_SVCNAME?} cannot be started directly. You must create"
		eerror "symbolic links to it for the services you want to start"
		eerror "and add those to the appropriate runlevels."
		return 1
	fi

	checkpath -d -o "${command_user?}" "/run/anubis_${instance?}"
}
