#!/bin/sh

# PROVIDE: amnezia
# REQUIRE: NETWORKING
# KEYWORD: shutdown
#
# amnezia_enable (bool):	Set to "YES" to enable amnezia.
#							(default: "NO")
#
# amnezia_interfaces (str): List of interfaces to bring up/down
#							on start/stop. (eg: "amn0 amn1")
#							(default: "")
# amnezia_env (str):		Environment variables for the userspace
#							implementation. (eg: "LOG_LEVEL=debug")
#
# amnezia_kmod (str):		Kernel module to load.
#							(default: "if_amn", "" - no module)
#
# amnezia_confdirs (str):	Directory to store the configuration files.
#							(default: "%%ETCDIR%%")

. /etc/rc.subr

name=amnezia
rcvar=amnezia_enable
extra_commands="reload status"

start_cmd="${name}_start"
stop_cmd="${name}_stop"
reload_cmd="${name}_reload"
status_cmd="${name}_status"

amnezia_start()
{
	kmod=${amnezia_kmod:-if_amn}
	${amnezia_env:+eval export $amnezia_env}
	[ -n "${kmod}" ] && kldstat -q -n ${kmod} || kldload -n ${kmod}

	for interface in ${amnezia_interfaces}; do
		daemon %%PREFIX%%/bin/awg-quick up ${interface}
	done
}

amnezia_stop()
{
	for interface in ${amnezia_interfaces}; do
		%%PREFIX%%/bin/awg-quick down ${interface}
	done
}

amnezia_reload()
{
	${amnezia_env:+eval export $amnezia_env}

	for interface in ${amnezia_interfaces}; do
		%%PREFIX%%/bin/awg-quick reload ${interface}
	done
}

amnezia_status()
{
	${amnezia_env:+eval export $amnezia_env}
	amnezia_status="0"

	for interface in ${amnezia_interfaces}; do
		%%PREFIX%%/bin/awg show ${interface} || amnezia_status="1"
	done

	return ${amnezia_status}
}

load_rc_config $name

: ${amnezia_enable="NO"}
: ${amnezia_interfaces=""}
: ${amnezia_env=""}
: ${amnezia_kmod="if_amn"}
: ${amnezia_confdirs="%%ETCDIR%%"}

${amnezia_confdirs:+eval export AWG_QUICK_CONFIG_SEARCH_PATHS="$amnezia_confdirs"}

run_rc_command "$1"
