#!/bin/sh /etc/rc.common
START=12

#upgrade network version from 1 to 2
#copy dhcpd config to network
dhcpd_info_to_network() {
	local get
	local ele

	config_get get $1 physical
	uci set network.$1.dhcp_physical=$get
	config_get get $1 status
	uci set network.$1.dhcp_status=$get
	config_get get $1 rdi_pool
	uci set network.$1.dhcp_rdi_pool=$get
	config_get get $1 start_ip
	uci set network.$1.dhcp_start_ip=$get
	config_get get $1 end_ip
	uci set network.$1.dhcp_end_ip=$get
	config_get get $1 rdi_start_ip
	uci set network.$1.dhcp_rdi_start_ip=$get
	config_get get $1 rdi_end_ip
	uci set network.$1.dhcp_rdi_end_ip=$get
	config_get get $1 dns
	uci set network.$1.dhcp_dns=
	[ "$get" ] && {
		for ele in $get ; do
			uci add_list network.$1.dhcp_dns=$ele
		done
	}
	config_get get $1 routers
	uci set network.$1.dhcp_routers=$get
	config_get get $1 lease_time
	uci set network.$1.dhcp_lease_time=$get
}

#upgrade network version from 2 to 3
#Remove subnet mask bigger than 255.0.0.0
check_network_mask() {
	local get
	local ele
	local octet
	local i
	local str

	config_get get $1 physical
	[ "$get" == "eth0" ] || return #only check lan

	config_get get $1 static_netmask
	if [ "$get" ]; then
		octet=${get%%.*}
		[ $octet -lt 255 ] && uci set network.$1.static_netmask=255.0.0.0
	fi

	config_get get $1 static_2nd_subnet
	if [ "$get" ]; then
		uci set network.$1.static_2nd_subnet=
		i=0
		for ele in $get; do
			i=$(($i + 1))
			if [ $i -eq 1 ]; then
				#uci add_list network.$1.static_2nd_subnet=$ele
				str=$ele
			elif [ $i -eq 2 ]; then
				octet=${ele%%.*}
				if [ $octet -lt 255 ]; then
					#uci add_list network.$1.static_2nd_subnet=255.0.0.0
					str="${str} 255.0.0.0"
				else
					#uci add_list network.$1.static_2nd_subnet=$ele
					str="${str} $ele"
				fi
			else
				#uci add_list network.$1.static_2nd_subnet=$ele
				str="${str} $ele"
				uci add_list network.$1.static_2nd_subnet="$str"
				i=0
			fi
		done
	fi
}
check_ipobj_mask() {
	local get
	local octet
	local start

	config_get get $1 mask
	if [ "$get" ]; then
		octet=${get%%.*}
		[ $octet -lt 255 ] && uci set ip_object.$1.mask=255.0.0.0
	fi

	config_get get $1 start_ip
	start=${get%%.*}
	config_get get $1 end_ip
	if [ "$start" -a "$get" ]; then
		octet=${get%%.*}
		[ $start -lt $octet ] && uci set ip_object.$1.end_ip=${start}.255.255.255
	fi
}
check_ipsec_mask() {
	local get
	local ele
	local octet
	local netlen

	config_get get $1 rightclient
	if [ "$get" ]; then
		octet=${get%%.*}
		netlen=${get##*/}
		[ "$netlen" ] || netlen=0
		[ $netlen -lt 8 ] && uci set ipsec_policy.$1.rightclient=${octet}.0.0.0/8
	fi

	config_get get $1 more_remotesubnet
	if [ "$get" ]; then
		uci set ipsec_policy.$1.more_remotesubnet=
		for ele in $get; do
			octet=${ele%%.*}
			netlen=${ele##*/}
			[ "$netlen" ] || netlen=0
			if [ $netlen -lt 8 ]; then
				uci add_list ipsec_policy.$1.more_remotesubnet=${octet}.0.0.0/8
			else
				uci add_list ipsec_policy.$1.more_remotesubnet=$ele
			fi
		done
	fi
}

#Do config upgrade here
boot() {
	local cfg_ver

	local commit_check
	commit_check=0

	#upgrade uci config: system
	cfg_ver=`uci get system.cfg_ver`
	[ "$cfg_ver" ] || {
		uci set system.cfg_ver=check_cfg_upgrade
		commit_check=1
	}

	#upgrade uci config: network
	cfg_ver=`uci get system.cfg_ver.network`
	[ "$cfg_ver" ] || cfg_ver=1
	[ $cfg_ver -lt 2 ] && {
		config_load dhcpd
		config_foreach dhcpd_info_to_network
		uci commit network
		uci set system.cfg_ver.network=2
		commit_check=1
		echo "[cfg_upgrade] Upgrade network config to version 2" > /dev/console
	}
	[ $cfg_ver -lt 3 ] && {
		#check network.lan
		config_load network
		config_foreach check_network_mask
		uci commit network
		#check ip object
		config_load ip_object
		config_foreach check_ipobj_mask
		uci commit ip_object
		#check ipsec profile
		config_load ipsec_policy
		config_foreach check_ipsec_mask
		uci commit ipsec_policy
		uci set system.cfg_ver.network=3
		commit_check=1
		echo "[cfg_upgrade] Remove subnet mask bigger than 255.0.0.0" > /dev/console
	}

	[ $commit_check -eq 1 ] && uci commit system.cfg_ver
	exit 0
}

start() {
	exit 0
}

stop() {
	exit 0
}

apply() {
	exit 0
}
