#!/bin/sh

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

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

help()
{
    cat <<_HELP_
Setup WAN port mode.
Usage: $0 {set|del} {wan|wan1|wan2|wwan} {dhcp|static} GATEWAY IPADDR/NETMASK [-d DNS[,DNS]] [-m metric] [-rRS]
        -d DNS[,DNS] # Manually add dns list
        -m metric # ADD metric
        -r # Add default route on this WAN port, It's default behavior.
        -R # Don't add default route on this WAN port

Example:
    $0 set wan -D # Set port 'wan' as dhcp mode.
    $0 set wan -D -d 8.8.8.8,9.9.9.9 # Set port 'wan' as dhcp mode with manual DNS settings.
    $0 set wan -D -m 60 # Set port 'wan' as dhcp mode with metric.
    $0 set wan -D -R # Set port 'wan' as dhcp mode, but don't set default route on it.
    $0 set wan -S 172.18.38.100 172.18.38.1/255.255.255.0 -d 8.8.8.8 -m 60 -R # Set wan as static ip without default route,and metric is 60.
_HELP_
}


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

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

case "$PRODUCT" in
    QIYANG_IMX6ULL_2|WooLink2F6280_W_4G|WooLink5F6280-W-4G|WooLink2F6280-W-4G)
        ifx="$1";ifname="eth0"
        ;;
    WLOSANY_EM1000)
        ifx="$1"
        if [ "${ifx}" = 'wan' ] ; then
            ifname="net1"
        elif [ "${ifx}" = 'wan1' ] ; then
            ifname="net2"
        else
            echo "Error, invalid network interface for EM1000: ${ifx}" 1>&2
            exit 1
        fi
        ;;
    WLOSANY_RK3568|WLOSANY_RK3568_FORLINX|WLOSANY_BAU-B30-003|WLOSANY_VM1220T)
        ifx="$1"
        if [ "${ifx}" = 'wan' ] ; then
            ifname="eth0"
        elif [ "${ifx}" = 'wan1' ] ; then
            ifname="eth1"
        else
            echo "Error, invalid network interface for RK3568: ${ifx}" 1>&2
            exit 1
        fi
        ;;
    WLOSANY_ECU1170)
        ifx="$1"
        if [ "${ifx}" = 'wan' ] ; then
            ifname="eth0"
        elif [ "${ifx}" = 'wan1' ] ; then
            ifname="eth1"
        else
            echo "Error, invalid network interface for RK3568: ${ifx}" 1>&2
            exit 1
        fi
        ;;
    WLOSANY_RK3588*)
        ifx="$1"
        if [ "${ifx}" = 'wan' ] ; then
            ifname="eth0"
        elif [ "${ifx}" = 'wan1' ] ; then
            ifname="eth1"
        else
            echo "Error, invalid network interface for RK3588: ${ifx}" 1>&2
            exit 1
        fi
        ;;
    WLOSANY_X86_V3)
        ifx="$1";ifname="enp1s0"
        ;;
    WLOSANY_LC100)
        ifx="$1";ifname="eth1"
        if [ "${ifx}" != 'wan' ] ; then
            echo "Error, invalid network interface for LC100: ${ifx}" 1>&2
            exit 1
        fi
        ;;
    WLOSANY_WooLink2004)
        ifx="$1";ifname="eth0"
        if [ "${ifx}" != 'wan' ] ; then
            echo "Error, invalid network interface for WooLink2004: ${ifx}" 1>&2
            exit 1
        fi
        ;;
    *)
    ifx="$1";ifname="eth0"
    echo "Do not supported this product [$PRODUCT],using default ifname:$ifname"
    ;;
esac
if [ $# -ge 1 ]; then
shift 1
fi

proto="dhcp"
case $1 in
    dhcp | -D) proto=dhcp;;
    static | -S) proto=static;;
    *)
    ;;
esac
if [ $# -ge 1 ]; then
shift 1
fi

if [ "$proto" == "static" ] ; then
    gateway="$1"
    ips="$2"
    if [ $# -ge 2 ]; then
        shift 2
    fi
fi

defaultroute='1'
while [ -n "$1" ]; do
    case $1 in
        -d) dnss="$2";shift 2;;
        -m) metric="$2";shift 2;;
        -R) defaultroute='0';shift 1;;
        -r) defaultroute='1';shift 1;;
        --) shift;break;;
        -*) help;exit 1;;
        *) break;;
    esac
done

set_wan()
{
    uci set network.${ifx}='interface'

    if [[ "$PRODUCT" == "WLOSANY_"* ]]; then
        uci set network.${ifx}.device=$ifname
        # [ "${ifx}" = "wan" ] && uci set network.${ifx}.metric=80
    else  
        uci set network.${ifx}.defaultroute=$defaultroute
        uci set network.${ifx}.ifname=$ifname
    fi
    
    case "$proto" in
    dhcp)
        uci set network.${ifx}.proto='dhcp'
        uci del network.${ifx}.netmask
        uci del network.${ifx}.gateway
        uci del network.${ifx}.ipaddr
    ;;
    static)
        uci set network.${ifx}.proto='static'
        uci set network.${ifx}.gateway=$gateway
        uci set network.${ifx}.force_link=0

        netmask=''
        uci delete network.${ifx}.ipaddr
        for ip in ${ips//,/ }; do
            OIFS=$IFS;IFS='/';set -- $ip;ipaddr=$1;netmask=$2;IFS=$OIFS
            if [ "$ipaddr" != "$gateway" ]; then
                uci set network.${ifx}.ipaddr=$ipaddr
            fi
        done
        [ -z "${netmask}" ] && netmask='255.255.255.0'
        uci set network.${ifx}.netmask=$netmask
    ;;
    *) help; exit 1;;
    esac

    if [ -n "$dnss" ]; then
        uci delete network.${ifx}.dns
        for dns in ${dnss/,/ }; do
            uci add_list network.${ifx}.dns=$dns
        done
    fi

    if [ -n "$metric" ]; then
        uci set network.${ifx}.metric=$metric
    fi
}

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

case "$cmd" in
    set) set_wan;;
    del) del_wan;;
    *) help;exit 1;;
esac

uci commit network
# uci commit firewall


#/etc/init.d/network reload

# case "$PRODUCT" in
#         WL-SMC-LC1000)
#             netuci
#             ;;
#         *) ;;
# esac
echo "Set WAN port IP  $ifx "

exit 0
