#!/bin/sh

. /etc/functions.sh

set_static_route() {
        local ENABLE
        local NET
        local MASK
        local GATEWAY
        local DEV
		local ITF
		local ifout
		
		config_get_bool ENABLE  $1      enable  0
        if [ $ENABLE -eq 1 ]; then
                config_get NET  $1      net
                config_get MASK $1      mask
                config_get GATEWAY      $1      gateway
                config_get DEV  $1      interface
				[ $DEV == "LAN" ] && ITF="lan"
				[ $DEV == "WAN" ] && ITF="wan"
				[ $DEV == "VoIP_WAN" ] && ITF="voipwan"
				[ $DEV == "IPTV_WAN" ] && ITF="iptvwan"
				[ $DEV == "MGMT_WAN" ] && ITF="mngtwan"
                ifout=$(uci -P /var/state/ -q get network.$ITF.ifname)
				#logger "route add -net $NET netmask $MASK ${GATEWAY:+gw [$GATEWAY]} ${ifout:+dev $ifout}"
                route add -net $NET netmask $MASK ${GATEWAY:+gw $GATEWAY} ${ifout:+dev $ifout}
		fi
} 

if [ ifup = "$ACTION" ]; then
	cat /etc/config/s_route > /dev/null 2>&1 || exit
	config_load /etc/config/s_route
	config_foreach set_static_route static-route
fi
