#!/bin/sh

PRODUCT=$(cat /tmp/sysinfo/board_name)

if [ -z "$PRODUCT" ]; then
    echo "get PRODUCT error"
    exit 2;
fi

help()
{
    cat <<_HELP_
Setup/Remove LAN or LAN of inline port on static ip mode.
Usage: $0 {set|del} {lan|ilan} [--ipaddr IPADDR] [--netmask NETMASK] [-B]

Example:
    $0 set lan --ipaddr 172.18.38.1 --netmask 255.255.255.0 -B # set lan port with static ip addresses and enable bridge
    $0 set ilan --ipaddr 172.18.38.1 --netmask 255.255.255.0 --vid 101 # set ilan port with static ip addresses
_HELP_
}


if [ $# -lt 1 ]; then
    help
    exit 1
fi

case $1 in
    set) cmd="$1";;
    del) cmd="$1";;
    *) help; exit 1;;
esac
shift 1

get_lan_ifname() {
    case "$1" in
        QIYANG_IMX6ULL_2|WooLink2F6280_W_4G|WooLink5F6280-W-4G|WooLink2F6280-W-4G)
            ifname="eth1"
            ;;
        WLOSANY_EM1000|WLOSANY_RK3568|WLOSANY_RK3568_FORLINX|WLOSANY_BAU-B30-003|WLOSANY_X86_V3)
            ifname="br-lan"
            ;;
        WLOSANY_RK3568_XDDL|WLOSANY_ECU1170)
            ifname="br-lan"
            ;;
        WLOSANY_RK3588*)
            ifname="br-lan"
            ;;
		WLOSANY_LC100)
			ifname="eth0"
			;;
        WLOSANY_WooLink2004)
            ifname="br-lan"
            ;;
        *)
        ifname="eth1"
        echo "Do not supported this product [$1],using default ifname:$ifname"
    esac 
}

get_ilan_ifname() {
    case "$1" in
        QIYANG_IMX6ULL_2|WooLink2F6280_W_4G|WooLink5F6280-W-4G|WooLink2F6280-W-4G)
            ifname="eth0"
            ;;
        WLOSANY_EM1000)
            ifname="eth0"
            ;;
        WLOSANY_X86_V3)
            ifname="enp2s0"
            ;;
        *)
        ifname="eth0"
        echo "Do not supported this product [$1],using default ifname:$ifname"
        exit 1;;
    esac 
}

case $1 in
    lan) 
        ifx="$1";
        get_lan_ifname "$PRODUCT"
    ;;
    ilan) 
        ifx="$1";
        get_ilan_ifname "$PRODUCT"   
    ;;
    *) help; exit 1;;
esac
shift 1


defaultroute='1'
netmask='255.255.255.0'
while [ -n "$1" ]; do
    case $1 in
        --ipaddr) ipaddr="$2";shift 2;;
        --netmask) netmask="$2";shift 2;;
        -B) bridge='1';shift 1;;
        --) shift;break;;
        -*) help;exit 1;;
        *) break;;
    esac
done

set_lan()
{
    [ -n "$ipaddr" ] || help ||exit 1
    uci delete network.${ifx}
    uci set network.${ifx}=interface
	#echo ${bridge}
    #if [ "$ifx" == "ilan" -o -n "$bridge" ]; then
    #    uci set network.${ifx}.type=bridge
    #fi
    case "$1" in
        QIYANG_IMX6ULL_2|WooLink2F6280_W_4G|WooLink5F6280-W-4G|WooLink2F6280-W-4G)
            uci set network.${ifx}.ifname="${ifname}"
            ;;
        WLOSANY_EM1000|WLOSANY_RK3568|WLOSANY_RK3568_FORLINX|WLOSANY_BAU-B30-003|WLOSANY_X86_V3)
            uci set network.${ifx}.type="bridge"
            uci set network.${ifx}.device="br-lan"
            ;;
        WLOSANY_LC100|WLOSANY_ECU1170)
            uci set network.${ifx}.type="bridge"
            uci set network.${ifx}.device="br-lan"
            ;;
        WLOSANY_WooLink2004)
            uci set network.${ifx}.type="bridge"
            uci set network.${ifx}.device="br-lan"
            ;;
        *)
        exit 1;;
    esac 
    
    uci set network.${ifx}.proto='static'
    uci set network.${ifx}.ipaddr="${ipaddr}"
    uci set network.${ifx}.netmask="${netmask}"
}

del_lan()
{
    uci delete network.${ifx}
    # uci del_list firewall.${zone}.network="${ifx}"
}

case "$cmd" in
    set) set_lan $PRODUCT ;;
    del) del_lan;;
    *) help;exit 1;;
esac

uci commit network
echo "Set LAN port IP  $ifx "

exit 0

