#!/bin/sh /etc/rc.common
START=46
UCI_CONFIG=iprouting
config_load $UCI_CONFIG
#lan_changed: called by script:network
EXTRA_COMMANDS='lan_changed'
EXTRA_HELP='	lan_changed	reset ip address for specified lan'

add_iprouting() {
	config_get status $1 status
	[ "$status" = "enable" ] || return

	config_get wan_pf $1 wan_pf
	config_get lan_pf $1 lan_pf
	config_get ipaddr $1 ipaddr
	config_get mask $1 mask

	arp -i wan-$wan_pf -Ds $ipaddr wan-$wan_pf netmask $mask pub >/dev/null 2>/dev/null
}

delete_iprouting() {
	status=`uci oget $UCI_CONFIG.$1.status`
	[ "$status" = "enable" ] || return

	wan_pf=`uci oget $UCI_CONFIG.$1.wan_pf`
	lan_pf=`uci oget $UCI_CONFIG.$1.lan_pf`
	ipaddr=`uci oget $UCI_CONFIG.$1.ipaddr`
	mask=`uci oget $UCI_CONFIG.$1.mask`

	arp -i wan-$wan_pf -d $ipaddr netmask $mask pub >/dev/null 2>/dev/null
}

start(){
	config_foreach add_iprouting
}

stop(){
	config_foreach delete_iprouting
}

apply(){
	del=`uci fchanges delete $UCI_CONFIG`
	for cfg in $del; do
		delete_iprouting $cfg
	done

	add=`uci fchanges new $UCI_CONFIG`
	for cfg in $add; do
		add_iprouting $cfg
	done

	mdf=`uci fchanges modify $UCI_CONFIG`
	for cfg in $mdf; do
		delete_iprouting $cfg
		add_iprouting $cfg
	done

	uci commit $UCI_CONFIG
}

lan_changed(){
	return
}

lan_changed2(){
	return
}
