#!/bin/sh

. /lib/functions.sh
. /usr/share/libubox/jshn.sh

NETWORK_CONFIG="/tmp/cache/network/network.conf"
NETWORK_STATUS="/tmp/cache/network/network.status"
ETH_INFO="/tmp/cache/systeminfo/ethinfo.info"

json_load "`cat $ETH_INFO`"
json_get_vars wan_net_if lan_net_if ppp_net_if mppp_net_if wan_net_section lan_net_section ppp_net_section mppp_net_section wanv6_net_section pppv6_net_if

network_info_init()
{
# WAN
	WAN1_PROTO="$(uci get network.$wan_net_section.proto)"
	[ "$?" = "1" ] && WAN1_PROTO=
	WAN1P_PROTO="$(uci get network.$ppp_net_section.proto)"
	[ "$?" = "1" ] && WAN1P_PROTO=

	if [ -n "$WAN1P_PROTO" ]; then
		WAN_PROTO="$WAN1P_PROTO"
		WAN_IFACE="$ppp_net_section"
		NETWORK_IFACE="$ppp_net_if"
		[ "$WAN_PROTO" = "l2tp" -o "$WAN_PROTO" = "pptp" ] && INTRANET_IFACE="$wan_net_if"
		[ "$WAN_PROTO" = "mulpppoe" -a -n "$(uci get network.$mppp_net_section)" ] && {
			WAN2_IFACE="$mppp_net_section"
			NETWORK2_IFACE="$mppp_net_if"
			SESSION2_AREA="`uci get network.$mppp_net_section.area`"
		}
	elif [ -n "$WAN1_PROTO" ]; then
		WAN_PROTO="$WAN1_PROTO"
		WAN_IFACE="$wan_net_section"
		NETWORK_IFACE="$wan_net_if"
	else
		WAN_PROTO="unknown"
	fi
	WAN_UPDOWN="down"
	if [ "$WAN_PROTO" = "pppoe" -o "$WAN_PROTO" = "pptp" -o  "$WAN_PROTO" = "l2tp" ]; then
		if [ -n "$(ubus call network.interface.$WAN_IFACE status | grep "\"up\": true")" -a "$?" = "0" ]; then
			[ "$(cat /etc/ppp/ppp-status)" = "1" ] && WAN_UPDOWN="up"
		fi
	elif [ "$WAN_PROTO" = "mulpppoe" ]; then
		WAN_UPDOWN="up"
		[ "$(cat /etc/ppp/pppoe1-status)" = "0" -o ! -f /etc/ppp/pppoe1-status ] && s1_sta=0 || s2_sta=1
		[ "$(cat /etc/ppp/pppoe2-status)" = "0" -o ! -f /etc/ppp/pppoe2-status ] && s2_sta=0 || s2_sta=1
		[ "$s1_sta" = "0" -a "$s2_sta" = "0" ] && WAN_UPDOWN="down"
	elif [ -n "$(ubus -S list | sed -ne 's/^network\.interface\.//p' | grep "$WAN_IFACE")" ]; then
		if [ -n "$(ubus call network.interface.$WAN_IFACE status | grep "\"up\": true")" -a "$?" = "0" ]; then
			WAN_UPDOWN="up"
		fi
	fi

	[ "$WAN_UPDOWN" = "up" ] && WAN_UPTIME="$(cat /proc/uptime | cut -d " " -f 1 )"
# LAN
	LAN_PROTO="$(uci get network.$lan_net_section.proto)"
	[ "$INTERFACE" = "$lan_net_section" -a "$ACTION" = "ifup" ] && LAN_UPDOWN="up" || LAN_UPDOWN="down"
	lan_ipaddr="$(uci get network.$lan_net_section.ipaddr)"
	lan_netmask="$(uci get network.$lan_net_section.netmask)"
	lan_gateway="$(uci get network.$lan_net_section.gateway)"
	[ "$lan_gateway" = "" ] && lan_gateway=$lan_ipaddr

# Guest Network
	guest_ipaddr="$(uci get network.guest.ipaddr)"
	guest_netmask="$(uci get network.guest.netmask)"

# OP-MODE
	OP_MODE="$(uci get opmode.operate.mode)"
# IPV6
	IPV6_PROTO=$(uci get network.${wanv6_net_section}.ipv6_type)
	[ "$?" = "1" ] && IPV6_PROTO=
	if [ -n "$IPV6_PROTO" ]; then
		if [ "$IPV6_PROTO" = "autodetect" ]; then
			wanv6_D=`ifstatus ${wanv6_net_section}_D`
			if [ "$?" = "1" ]; then
				json_load "$wanv6_D"
				json_get_var IPV6_PROTO proto
			fi
		fi
		case "$IPV6_PROTO" in
			"pppoe")
				if [ "$(uci get network.${wanv6_net_section}.proto)" = "static" ]; then
					IPV6_WAN_IFACE=$ppp_net_if
					IPV6_LAN_IFACE=$lan_net_if
				else
					IPV6_WAN_IFACE=$pppv6_net_if
					IPV6_LAN_IFACE=$lan_net_if
				fi
				;;
			"dhcpv6" | "autoconfig" | "static" | "autodetect")
				IPV6_WAN_IFACE=$wan_net_if
				IPV6_LAN_IFACE=$lan_net_if
				;;
			"6to4" | "6rd")
				IPV6_WAN_IFACE=
				IPV6_LAN_IFACE=$lan_net_if
				;;
			"pasthru")
				IPV6_WAN_IFACE=
				IPV6_LAN_IFACE=
				;;
		esac
	fi
}

dni_network_config_to_json()
{
	json_init

	[ -n "$WAN_PROTO" ] && json_add_string wan_protocol "$WAN_PROTO"
	[ -n "$WAN_IFACE" ] && json_add_string wan_interface "$WAN_IFACE"
	[ -n "$NETWORK_IFACE" ] && json_add_string wan_network_iface "$NETWORK_IFACE"
	[ -n "$INTRANET_IFACE" ] && json_add_string wan_intranet_iface "$INTRANET_IFACE"
	if [ "$WAN_PROTO" = "mulpppoe" ]; then
		[ -n "$WAN2_IFACE" ] && {
			json_add_string session2_enable "1"
			json_add_string wan2_interface "$WAN2_IFACE"
			[ -n "$NETWORK2_IFACE" ] && json_add_string wan2_network_iface "$NETWORK2_IFACE"
			json_add_string session2_area "$SESSION2_AREA"
		} || {
			json_add_string session2_enable "0"	
		} 
	fi

	[ -n "$LAN_PROTO" ] && json_add_string lan_protocol "$LAN_PROTO"
	[ -n "$lan_net_section" ] && json_add_string lan_interface "$lan_net_section"
	[ -n "$lan_net_if" ] && json_add_string lan_iface "$lan_net_if"
	[ -n "$lan_ipaddr" ] && json_add_string lan_ipaddr "$lan_ipaddr"
	[ -n "$lan_netmask" ] && json_add_string lan_netmask "$lan_netmask"
	[ -n "$lan_gateway" ] && json_add_string lan_gateway "$lan_gateway"
	[ -n "$guest_ipaddr" ] && json_add_string guest_ipaddr "$guest_ipaddr"
	[ -n "$guest_netmask" ] && json_add_string guest_netmask "$guest_netmask"

	[ -n "$OP_MODE" ] && { 
		json_add_string opmode "$OP_MODE"
		if [ "$OP_MODE" = "apmode" ]; then
			json_add_string wan_interface "$lan_net_section"
		fi
	}
	
	[ -n "$IPV6_PROTO" ] && json_add_string wanv6_proto "$IPV6_PROTO"
	[ -n "$IPV6_WAN_IFACE" ] && json_add_string ipv6_wan_iface "$IPV6_WAN_IFACE"
	[ -n "$IPV6_LAN_IFACE" ] && json_add_string ipv6_lan_iface "$IPV6_LAN_IFACE"

	json_dump -i > $NETWORK_CONFIG
}

dni_network_status_to_json(){
	json_load "`cat /tmp/cache/network/dhcp.conf`"
	json_get_vars leasetime serverid gateway

	if [ "$OP_MODE" = "apmode" ]; then
		json_init
		json_add_string wan_updown "$LAN_UPDOWN"
		if [ "$LAN_UPDOWN" = "up" -a "$INTERFACE" = "$lan_net_section" ]; then
			LAN_UPTIME="$(cat /proc/uptime | cut -d " " -f 1 )"
			json_add_string wan_uptime "$LAN_UPTIME"
			if [ "$LAN_PROTO" = "dhcp" -a "$LAN_UPDOWN" = "up" ]; then
				[ -n "$leasetime" ] && json_add_string leasetime "$leasetime"
				[ -n "$serverid" ] && json_add_string server "$serverid"
				[ -n "$gateway" ] && json_add_string gateway "$gateway"
			fi
		fi
	else
		json_init
		json_add_string wan_updown "$WAN_UPDOWN"
		[ -n "$WAN_UPTIME" ] && json_add_string wan_uptime "$WAN_UPTIME"
		if [ "$WAN_PROTO" = "dhcp" -a "$WAN_UPDOWN" = "up" ]; then
			[ -n "$leasetime" ] && json_add_string leasetime "$leasetime"
			[ -n "$serverid" ] && json_add_string server "$serverid"
			[ -n "$gateway" ] && json_add_string gateway "$gateway"
		fi
	fi

	#IPV6
	json_add_object ipv6
	if [ -n "$IPV6_PROTO" ]; then
		if [ -n "$IPV6_WAN_IFACE" ]; then
				wan_ip6addr=$(LANG=C ip -f inet6 -- addr show dev "$IPV6_WAN_IFACE" 2> /dev/null | sed -n -e 's/^  *inet6 \([A-Fa-f0-9.:]*[/][0-9]*\) scope '"global"'.*$/\1/p')
				json_add_object wan6addr
				json_add_array global
				for ip6addr in $wan_ip6addr
				do
					json_add_string "" $ip6addr 
				done
				json_close_array
				wan_ip6addr=$(LANG=C ip -f inet6 -- addr show dev "$IPV6_WAN_IFACE" 2> /dev/null | sed -n -e 's/^  *inet6 \([A-Fa-f0-9.:]*[/][0-9]*\) scope '"link"'.*$/\1/p')
				json_add_array link
				json_add_string "" $wan_ip6addr
				json_close_array
				json_close_object
		fi
		if [ -n "$IPV6_LAN_IFACE" ]; then
				lan_ip6addr=$(LANG=C ip -f inet6 -- addr show dev "$IPV6_LAN_IFACE" 2> /dev/null | sed -n -e 's/^  *inet6 \([A-Fa-f0-9.:]*[/][0-9]*\) scope '"global"'.*$/\1/p')
				json_add_object lan6addr
				json_add_array global
				json_add_string "" $lan_ip6addr
				json_close_array
				lan_ip6addr=$(LANG=C ip -f inet6 -- addr show dev "$IPV6_LAN_IFACE" 2> /dev/null | sed -n -e 's/^  *inet6 \([A-Fa-f0-9.:]*[/][0-9]*\) scope '"link"'.*$/\1/p')
				json_add_array link
				json_add_string "" $lan_ip6addr
				json_close_array
				json_close_object
		fi
	fi
	json_close_object

	json_dump -i > $NETWORK_STATUS
}

[ ! -d /tmp/cache/network ] && mkdir -p /tmp/cache/network 
[ "$INTERFACE" = "guest" ] && exit

network_info_init

if [ "$1" = "config" ]; then
	echo "Show network configure."
	dni_network_config_to_json
else
	echo "Show network status."
	dni_network_config_to_json
	dni_network_status_to_json
fi
