#!/bin/sh /etc/rc.common
# Copyright (C) 2018 Dengfeng Liu

START=99

USE_PROCD=1
NAME=wifidogx
PROG=/usr/bin/${NAME}
CONFIGFILE=/tmp/wifidogx.conf

prepare_wifidog_conf() {

	[ -f ${CONFIGFILE} ] && rm -f ${CONFIGFILE}

	uci_validate_section ${NAME} ${NAME} common \
		'enabled:bool:0' \
		'log_level:integer:7' \
		'gateway_id:string' \
		'gateway_interface:string:br-lan' \
		'auth_server_hostname:string' \
		'auth_server_port:port:443' \
		'auth_server_path:string:/wifidog/' \
		'check_interval:integer:60' \
		'client_timeout:integer:5' \
		'wired_passed:bool:1' \
		'apple_cna:bool:0' \
		'channel_path:string' \
		'trusted_domains:list(host)' \
		'trusted_wildcard_domains:list(string)' \
		'trusted_macs:list(string)' \
		'app_white_list:list(string)' \
		'mac_white_list:list(string)' \
		'wildcard_white_list:list(string)' \
		'enable_dns_forward:bool:1' \
		'enable_websocket:bool:1' \
		'js_filter:bool:1' 

	# if gateway_id is not set, get it from br-lan
	if [ -z "$gateway_id" ]; then
		gateway_id=$(sed -e 's/://g' /sys/class/net/${gateway_interface}/address)
		# convert to upper case
		gateway_id=$(echo $gateway_id | tr '[a-z]' '[A-Z]')
		# uci add gateway_id to config file
		uci set ${NAME}.common.gateway_id=$gateway_id
		uci commit ${NAME}
	fi
	
	# if channel_path is not set, set it to apfree
	if [ -z "$channel_path" ]; then
		channel_path=apfree
		uci set ${NAME}.common.channel_path=$channel_path
		uci commit ${NAME}
	fi

	if [ ! -z "$app_white_list" ]; then
		# iterate app_white_list and find the corresponding domain according to the item
		for group in $app_white_list; do
			group_domain_list=$(uci get wifidogx.$group.domain_name)
			# if the domain list is not empty, add it to trusted_domains
			if [ ! -z "$group_domain_list" ]; then
				trusted_domains="$trusted_domains $group_domain_list"
			fi
		done
	fi

	if [ ! -z "$mac_white_list" ]; then
		# iterate mac_white_list and find the corresponding mac according to the item
		for group in $mac_white_list; do
			group_mac_list=$(uci get wifidogx.$group.mac_address)
			# if the mac list is not empty, add it to trusted_macs
			if [ ! -z "$group_mac_list" ]; then
				trusted_macs="$trusted_macs $group_mac_list"
			fi
		done
	fi

	if [ ! -z "$wildcard_white_list" ]; then
		# iterate wildcard_white_list and find the corresponding domain according to the item
		for group in $wildcard_white_list; do
			group_wildcard_list=$(uci get wifidogx.$group.wildcard_domain)
			if [ ! -z "$group_wildcard_list" ]; then
				trusted_wildcard_domains="$trusted_wildcard_domains $group_wildcard_list"
			fi
		done
	fi

	# set above variables to config file
	echo "GatewayID $gateway_id" > ${CONFIGFILE}
	echo "GatewayInterface $gateway_interface" >> ${CONFIGFILE}
	echo "AuthServer {
	Hostname $auth_server_hostname
	HTTPPort $auth_server_port
	Path $auth_server_path
}" >> ${CONFIGFILE}
	echo "CheckInterval $check_interval" >> ${CONFIGFILE}
	echo "ClientTimeout $client_timeout" >> ${CONFIGFILE}
	echo "JsFilter $js_filter" >> ${CONFIGFILE}
	echo "WiredPassed $wired_passed" >> ${CONFIGFILE}
	echo "BypassAppleCNA $apple_cna" >> ${CONFIGFILE}
	echo "EnableDNSForward $enable_dns_forward" >> ${CONFIGFILE}
	echo "EnableWS $enable_websocket" >> ${CONFIGFILE}
	# if has trusted_domains, parse the list to a string with ',' as separator and add it to config file
	if [ ! -z "$trusted_domains" ]; then
		trusted_domains=$(echo $trusted_domains | tr ' ' ',')
		echo "TrustedDomains $trusted_domains" >> ${CONFIGFILE}
	fi
	# if has trusted_macs, add it to config file
	if [ ! -z "$trusted_macs" ]; then
		trusted_macs=$(echo $trusted_macs | tr ' ' ',')
		echo "TrustedMACList $trusted_macs" >> ${CONFIGFILE}
	fi
	# if has trusted_wildcard_domains, add it to config file
	if [ ! -z "$trusted_wildcard_domains" ]; then
		trusted_wildcard_domains=$(echo $trusted_wildcard_domains | tr ' ' ',')
		echo "TrustedPanDomains $trusted_wildcard_domains" >> ${CONFIGFILE}
	fi
}

start_service() {
	config_load $NAME

	prepare_wifidog_conf

	[ "$enabled" -eq 0 ] && {
		echo "wifidogx is disabled, exit..." >&2
		return
	}

	procd_open_instance
	# -f: run in foreground
	procd_set_param command $PROG -c $CONFIGFILE -s -f -d $log_level
	procd_set_param respawn # respawn automatically if something died
	procd_set_param file /etc/config/wifidogx
	procd_close_instance
}

status_service() {
	/usr/bin/wdctlx status
}

reload_service() {
	stop 
	start
}

service_triggers() {
	procd_add_reload_trigger "${NAME}"
}
