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

START=99
USE_PROCD=1
INDEX=0

append_bool() {
	local section="$1"
	local option="$2"
	local value="$3"
	local _val
	config_get_bool _val "$section" "$option" '0'
	[ "$_val" -gt 0 ] && append args "$3"
}

append_string() {
	local section="$1"
	local option="$2"
	local value="$3"
	local _val
	config_get _val "$section" "$option"
	[ -n "$_val" ] && append args "$3$_val"
}

service_triggers () {
	procd_open_trigger
	procd_add_reload_trigger "eoip"
	procd_add_config_trigger "config.change" "eoip" /etc/init.d/eoip restart
	procd_add_config_trigger "config.change" "/etc/config/eoip" /etc/init.d/eoip restart
	procd_close_trigger
}

start_service() {
	config_load "eoip"
	echo "" > /etc/eoip.cfg
	echo "#!/bin/sh" > /etc/afterStart.sh
	echo "chkCount=0">> /etc/afterStart.sh
	echo "while [ \$chkCount -le 10 ];do">> /etc/afterStart.sh
	echo "  chkStarted=\$(ip a | grep zeoip | wc -l)">> /etc/afterStart.sh
	echo "  if [ \$chkStarted -eq 0 ]; then">> /etc/afterStart.sh
	echo "          chkCount=\$((chkCount+1))">> /etc/afterStart.sh
	echo "          sleep 2">> /etc/afterStart.sh
	echo "  else">> /etc/afterStart.sh
	echo "          chkCount=11">> /etc/afterStart.sh
	echo "  fi">> /etc/afterStart.sh
	echo "done">> /etc/afterStart.sh
	echo "now=\$(ip a | grep \"@zeoip\" | awk '{print \$2}' | sed 's/.$//' | cut -d \"@\" -f 1)" >> /etc/afterStart.sh
	echo "IFS=\$'\n'" >> /etc/afterStart.sh
	echo "for s in \$now ; do" >> /etc/afterStart.sh
	echo "   ip link delete link dev \$s" >> /etc/afterStart.sh
	echo "done" >> /etc/afterStart.sh
	echo "rm /etc/afterStart.sh" >> /etc/afterStart.sh
	chmod +x /etc/afterStart.sh
	config_foreach start_eoip eoip
	config_foreach start_eoip_vlan eoip
	if [ "$(cat /etc/eoip.cfg | grep zeoip)" != '' ]; then
		/etc/afterStart.sh&
		procd_open_instance
		args=" /etc/eoip.cfg"
		procd_set_param command /usr/bin/eoip $args
		procd_close_instance
	else
		rm /etc/afterStart.sh
	fi
}

stop_service() {
	killall eoip
}

start_eoip() {
	local section="$1" runas_root
	config_get_bool enabled "$section" enabled
	if [ "$enabled" -gt 0 ]; then
		config_get name "$section" name
		config_get idtun "$section" idtun
		config_get dst "$section" dst
		config_get_bool dynamic "$section" dynamic 0
		if [ "${name}" != '' ] && [ "${dst}" != '' ] && [ "${idtun}" != '' ]; then
			cnt=$(cat /etc/eoip.cfg | grep "zeoip${name}" | wc -l)
			if [ $cnt -eq 0 ]; then
				echo "[zeoip${name}]" >>/etc/eoip.cfg
				echo "id=${idtun}" >>/etc/eoip.cfg
				echo "dst=${dst}" >>/etc/eoip.cfg
				[ "$dynamic" -gt 0 ] && echo "dynamic=${dynamic}" >>/etc/eoip.cfg
				INDEX=$((INDEX+1))
			else
				result=$(uci delete /etc/config/eoip.@eoip[$INDEX])
				INDEX=$((INDEX+1))
			fi 
		fi
	fi
}

start_eoip_vlan() {
	local section="$1" runas_root
	config_get name "$section" name
	config_list_foreach "$section" vlan handle_vlan ${name}
}

handle_vlan() {
	local value="$1"
	local name="$2"
	echo "ip link add link zeoip${name} name zeoip${name}.${value} type vlan id ${value}" >> /etc/afterStart.sh
}
