#!/bin/sh

# PROVIDE: tincd
# REQUIRE: NETWORKING SERVERS
# BEFORE: DAEMON
# KEYWORD: shutdown

#
# Add some of the following variables to /etc/rc.conf to configure tincd:
# tincd_enable (bool):		Set to "NO" by default.
#

. /etc/rc.subr

case "$0" in
/etc/rc*)
	# during boot (shutdown) $0 is /etc/rc (/etc/rc.shutdown),
	# so get the name of the script from $_file
	name="$_file"
	;;
*)
	name="$0"
	;;
esac

name="${name##*/}"
rcvar=${name}_enable

load_rc_config $name

eval ": \${${name}_enable:=\"NO\"}"
eval ": \${${name}_flags:=\"\"}"
eval ": \${${name}_interface:=\"tun9\"}"
eval ": \${${name}_pidfile:=\"/usr/local/tinc/var/run/tinc.pid\"}"

pidfile="$(eval echo \${${name}_pidfile})"
command="/usr/local/tinc/sbin/tincd"
command_args="--pidfile=${pidfile}"
interface="$(eval echo \${${name}_interface})"


stop_postcmd()
{
	/sbin/ifconfig $interface > /dev/null 2>&1
	[ $? -eq 0 ] && /sbin/ifconfig $interface destroy
	[ $program_restart ] && echo Restarting... && sleep 5
}
restart_precmd()
{
	program_restart=1
}

stop_postcmd="stop_postcmd"
restart_precmd="restart_precmd"
extra_commands="reload"

run_rc_command "$1"
