#!/bin/sh /etc/rc.common

START=99

get_config() {
	config_get_bool enabled $1 enabled 1
	config_get XUNYOU_IFACE "$1" interface "lan"
}

start() {
	config_load xunyou
	config_foreach get_config xunyou

	if [ $enabled == 1 ]; then
		local interface=$(
			. /lib/functions/network.sh

			network_is_up "$XUNYOU_IFACE" || exit 0
			network_get_device device "$XUNYOU_IFACE"
			printf "%s" "${device}"
		)
		[ -z "$interface" ] && interface=br-lan
		nohup sh /xunyou/xunyou_daemon.sh start -i "$interface" >/dev/null 2>&1
	else
		sh /xunyou/xunyou_daemon.sh stop >/dev/null 2>&1
	fi
}

stop() {
	sh /xunyou/xunyou_daemon.sh stop >/dev/null 2>&1
}
reload() {
	config_load xunyou
	config_foreach get_config xunyou

	if [ $enabled == 1 ]; then
		local interface=$(
			. /lib/functions/network.sh

			network_is_up "$XUNYOU_IFACE" || exit 0
			network_get_device device "$XUNYOU_IFACE"
			printf "%s" "${device}"
		)
		[ -z "$interface" ] && interface=br-lan
		nohup sh /xunyou/xunyou_daemon.sh restart -i "$interface" >/dev/null 2>&1
	else
		sh /xunyou/xunyou_daemon.sh stop >/dev/null 2>&1
	fi
}
