#!/bin/sh
#*****************************************************
#dRouting.sh
#
#Author:hongqing.xu
#****************************************************

. /usr/syscfg/wan.utils.sh
DMP_WANCONNDEV="InternetGatewayDevice.WANDevice.1.WANConnectionDevice"
DMP_WANCONNDEV_ETH="InternetGatewayDevice.WANDevice.2.WANConnectionDevice"
DMP_LANDEV="InternetGatewayDevice.LANDevice.1"

quagga_run_dir="/var/run/quagga"
dync_routing_dir="/tmp/dync-routing"
zebra_conf_file="${dync_routing_dir}/zebra.conf"
ripd_conf_file="${dync_routing_dir}/ripd.conf"

g_conn_event=$1
g_conn_path=$2
g_conn_folder=`gen_wanconn_folder ${g_conn_path}`
g_is_wan_event=1
g_start_ripd=0

ZebraRestart()
{
    local hn=$1
    local pw=$2

    killall -q zebra
    
    echo "hostname ${hn}" > $zebra_conf_file
    echo "password ${pw}" >> $zebra_conf_file 
    echo "enable password ${pw}" >> $zebra_conf_file 
    echo "log file ${dync_routing_dir}/zebra.log" >> $zebra_conf_file
    
}

GetWanStatus()
{
	local wan_folder=$1
	local wan_up=`is_wan_up ${wan_folder}`
	
	if [ "${wan_up}" == "0" ]; then
		echo -n 0
		return
	fi

	if [ "${g_conn_event}" == "ifdown" -a "${g_conn_folder}" == "${wan_folder}" ]; then
		echo -n 0
		return
	fi
	
	echo -n 1
}

IsWanPath()
{
	local wan_path=$1
	echo ${wan_path} | grep LAN
	return $?
}

RipdSetWanConfigFile()
{
	local tmpf="${dync_routing_dir}/tmp_droute_file"
    local index=1
   	local line=""
    local max_line=0

    local if_path=""
    local if_ver=""
    local if_op=""
    local if_enable=""
    local if_ipmode=""
   
    local wan_folder=""
    local wan_up=""
    local wan_ifname=""
    local ret=1
    local rtv2b=0

	if [ "${g_is_wan_event}" == "0" ]; then
		return $ret
	fi

	cfgcmd get ${DMP_WANCONNDEV} | grep "RouteProtocolRx=" | sed '/RouteProtocolRx=Off/d' > $tmpf
	cfgcmd get ${DMP_WANCONNDEV_ETH} | grep "RouteProtocolRx=" | sed '/RouteProtocolRx=Off/d' >> $tmpf
    max_line=`cat $tmpf  | wc -l`

    while [ $index -le ${max_line} ]
    do
		line=`cat $tmpf | sed -n $index"p"`
        if_path=`echo "${line}" | cut -d "." -f 1-7`
        if_ipmode=`cfgcmd get ${if_path}.IPMode`

		if [ "${if_ipmode}" == "${WP_IPMODE_IPV6}" ]; then
            index=`expr $index + 1`
            continue
        fi

		wan_folder=`gen_wanconn_folder ${if_path}`
		wan_up=`GetWanStatus ${wan_folder}`
		wan_ifname=`get_wanconn_ifname ${wan_folder}`

		if [ "${wan_up}" == "0" ]; then
            index=`expr $index + 1`
            continue
        fi

		ret=0
        echo "interface $wan_ifname" >> $ripd_conf_file
        echo "router rip" >> $ripd_conf_file
        echo "redistribute kernel" >> $ripd_conf_file
        echo "redistribute connected" >> $ripd_conf_file
        echo "redistribute static" >> $ripd_conf_file

        echo "network ${wan_ifname}" >> $ripd_conf_file

        if_op=`cfgcmd get ${if_path}.RipPassive`
        if [ "${if_op}" == "1" ]; then
            echo "passive-interface $wan_ifname" >> $ripd_conf_file
        fi

        echo "interface $wan_ifname" >> $ripd_conf_file

        local ver="1 2"
        if_ver=`echo $line | cut -d "." -f 8 | cut -d "=" -f 2`
        	if [ "${if_ver}" == "RIPv1" ]; then
        		ver="1"
		elif [ "${if_ver}" == "RIPv2" ]; then
			ver="2"
        fi

        rtv2b=`cfgcmd get ${if_path}.RouteProtocolRxV2B`
        if [ "${ver}" != "1" ]; then
        	if [ "${rtv2b}" == "1" ]; then
        		echo "interface $wan_ifname" >> $zebra_conf_file
        		echo "no multicast" >> $zebra_conf_file
        	else
				echo "interface $wan_ifname" >> $zebra_conf_file
				echo "multicast" >> $zebra_conf_file
        	fi
        fi
        if [ "${if_op}" != "1" ]; then
        	echo "ip rip send version $ver" >> $ripd_conf_file
		fi
		echo "ip rip receive version $ver" >> $ripd_conf_file
		
		echo >> $ripd_conf_file

        index=`expr $index + 1`
    done
	rm -f $tmpf

	return $ret
}

RipdSetLanConfigFile()
{
	local wan_ifname="br0"
	local if_path="${DMP_LANDEV}"
	local rt_proto=`cfgcmd get ${if_path}.RouteProtocolRx`
	local rtv2b=`cfgcmd get ${if_path}.RouteProtocolRxV2B`

	if [ "${rt_proto}" == "Off" ]; then
		return 1
	fi
 
	echo "interface $wan_ifname" >> $ripd_conf_file
	echo "router rip" >> $ripd_conf_file
	echo "redistribute kernel" >> $ripd_conf_file
	echo "redistribute connected" >> $ripd_conf_file
	echo "redistribute static" >> $ripd_conf_file

	echo "network ${wan_ifname}" >> $ripd_conf_file

	if_op=`cfgcmd get ${if_path}.RipPassive`
	if [ "${if_op}" == "1" ]; then
		echo "passive-interface $wan_ifname" >> $ripd_conf_file
	fi

	echo "interface $wan_ifname" >> $ripd_conf_file

	local ver="1 2"
	if_ver="${rt_proto}"
	if [ "${if_ver}" == "RIPv1" ]; then
		ver="1"
	elif [ "${if_ver}" == "RIPv2" ]; then
		ver="2"
	fi
	if [ "${ver}" != "1" ]; then
		if [ "${rtv2b}" == "1" ]; then
			echo "interface $wan_ifname" >> $zebra_conf_file
			echo "no multicast" >> $zebra_conf_file
		else
			echo "interface $wan_ifname" >> $zebra_conf_file
			echo "multicast" >> $zebra_conf_file
		fi
	fi
	if [ "${if_op}" != "1" ]; then
		echo "ip rip send version $ver" >> $ripd_conf_file
	fi
	echo "ip rip receive version $ver" >> $ripd_conf_file
	
	echo >> $ripd_conf_file

	return 0
}

RipdSetConfigFile()
{
    local hn=$1
    local pw=$2
	local num=0
	local ret=1

    echo "hostname ${hn}" > $ripd_conf_file
    echo "password ${pw}" >> $ripd_conf_file

	# ret num 0 means func ended successfully
	RipdSetLanConfigFile && ret=0
	RipdSetWanConfigFile && ret=0

    echo "log file ${dync_routing_dir}/ripd.log" >> $ripd_conf_file
	return $ret
}

RipdRestart()
{
    local hn=$1
    local pw=$2

	killall -q ripd

    RipdSetConfigFile $hn $pw
	local ret=$?
    if [ $ret -eq 0 ]; then
	g_start_ripd=1
    fi
}


if [ ! -d "${dync_routing_dir}" ]; then
    mkdir -p "${dync_routing_dir}"
fi
if [ ! -d "${quagga_run_dir}" ]; then
    mkdir -p "${quagga_run_dir}"
fi

g_is_wan_event=`IsWanPath ${g_conn_path}`
ZebraRestart "Zebra" "zebra"
RipdRestart "Ripd" "ripd"


if [ $g_start_ripd -eq 1 ]; then
    zebra -d -f $zebra_conf_file
	ripd -f $ripd_conf_file -d
fi
