#!/bin/sh

. /lib/functions.sh
. /lib/dyiot/bin/get_hardware

function init_wifi() {
    rmmod 8188eu
    insmod /lib/lnxall/rtlap/8188eu.ko
    cp -rf /lib/lnxall/rtlap/rtl8188eufw.bin /lib/firmware/
    cd /sys/class/gpio
    echo '136' >export
    cd gpio136
    echo 'out' >direction
    echo '1' >value
}

function init_ssid() {
    local sn=$1
    local result=$(cat /etc/config/wireless | grep "lnxall_2g_SN")
    if [[ "$result" != "" ]]; then
        sed -i "s/lnxall_2g_SN/lnxall_2g_$sn/" /etc/config/wireless
    fi

    result=$(cat /etc/config/wireless | grep "lnxall_5g_SN")
    if [[ "$result" != "" ]]; then
        sed -i "s/lnxall_5g_SN/lnxall_5g_$sn/" /etc/config/wireless
    fi
    result=$(cat /etc/config/wireless | grep "lnxall_def_SN")
    if [[ "$result" != "" ]]; then
        sed -i "s/lnxall_def_SN/lnxall_def_$sn/" /etc/config/wireless
    fi
}

function init_wan_dhcp_fallbackip() {
    local product=$(fw_printenv | grep -i ^product | awk -F = '{print $2}' | tr '\n' '\0')
    case "$product" in
    WooLink1002-W-4D | WooLink1004-G | WooLink1227-W-4G)
        ifname='eth0'
        ;;
    *)
        return
        ;;
    esac
    (
        . /lib/functions/network.sh
        sleep 60
        ifconfig $ifname 2>&1 | grep RUNNING
        [ "$?" != "0" ] && return
        network_get_ipaddr my_ipaddr wan
        [ -z "$my_ipaddr" ] && {
            uci set network.wan.proto="static"
            uci set network.wan.ipaddr="192.168.1.167"
            uci set network.wan.netmask="255.255.255.0"
            /etc/init.d/network restart
        }
    ) &
}

function init_hwinfo() {
    local board=$(board_name)
    uboot_env=$(fw_printenv)

    mac_addr=$(echo "$uboot_env" | awk -F'=' '/^ethaddr1=/{print $2}')
    mac_addr1=$(echo "$uboot_env" | awk -F'=' '/^ethaddr=/{print $2}')
    sn=$(echo "$uboot_env" | awk -F'=' '/^SN=/{print $2}')
    hwver=$(echo "$uboot_env" | awk -F'=' '/^HWVER=/{print $2}')
    fdate=$(echo "$uboot_env" | awk -F'=' '/^DATE=/{print $2}')
    if [ -n "$hwver" ]; then
        local hw_ver_right=$(cat /app/config/fac.ini | grep "hardware_ver = $hwver")
        if [[ "$hw_ver_right" != "" ]]; then
            echo "hardware_ver $hwver is right, no need change"
        else
            sed -i "s/hardware_ver = [0-9\.]\+/hardware_ver = $hwver/" /app/config/fac.ini
        fi
    fi
    if [ -n "$sn" ]; then
        local sn_right=$(cat /app/config/fac.ini | grep "^sn = $sn$")
        if [[ "$sn_right" != "" ]]; then
            echo "sn is right $sn is right, no need change"
        else
            sed -i "s/sn = [0-9A-Za-z]\+/sn = $sn/" /app/config/fac.ini
        fi
        case "$board" in
        HuiWei | HuiWei-NAND)
            uci set system.system.hostname=$sn
            uci commit system
            init_wifi
            ;;
        QIYANG | QIYANG_IMX6ULL | QIYANG_2 | generic | QIYANG_IMX6ULL_2 | CarV2 | QIYANG_BAU3 | WooLink2F6280_W_4G)
            uci set system.system.hostname=$sn
            uci commit system
            ;;
        WOOLINK-MT7621-512M-256M | WOOLINK-MT7628-128M-32M)
            uci set system.@system[0].hostname=$sn
            uci commit system
            ;;
        *)
            uci set system.@system[0].hostname=$sn
            uci commit system
            ;;
        esac
    else
        sn=$(cat /app/config/fac.ini | grep -i ^sn | awk -F = '{print $2}' | awk '{gsub(/^\s+|\s+$/, "");print}' | tr '\n' '\0')
        uci set system.system.hostname=$sn
        uci set system.@system[0].hostname=$sn
        uci commit system
    fi

    uci -q batch <<-EOF
		set network.wwan=interface
		set network.wwan.proto='dhcp'
		set network.wwan.metric='30'
		set network.wwan.ifname='usb0'
	EOF

    if [ -n "$sn" ]; then
        tmp=$(cat /app/node/nodes_cfg.json | grep "\[GW_SN\]")
        if [[ "$tmp" != "" ]]; then
            sed -i "s/\[GW_SN\]/$sn/g" /app/node/nodes_cfg.json
        fi
    fi

    init_ssid $sn
}

function init_io() {
    local board=$(board_name)
    case "$board" in
        woolink,mt7621-64m)
        echo 0 >/sys/class/gpio/PCIE1_power/value
        echo 0 >/sys/class/gpio/PCIE2_power/value
        echo 0 >/sys/class/gpio/PCIE3_power/value
        echo 0 >/sys/class/gpio/M2_power/value
        sleep 1
        echo 1 >/sys/class/gpio/PCIE1_reset/value
        echo 1 >/sys/class/gpio/PCIE2_reset/value
        echo 1 >/sys/class/gpio/PCIE3_reset/value
        echo 1 >/sys/class/gpio/M2_reset/value
        ;;
    esac
    local product=$(fw_printenv | grep -i ^product | awk -F = '{print $2}' | tr '\n' '\0')
    case "$product" in
    WooLink2102-W-4G*)
        echo 0 >/sys/class/gpio/export 2>/dev/null
        echo in >/sys/class/gpio/gpio0/direction
        echo 11 >/sys/class/gpio/export 2>/dev/null
        echo out >/sys/class/gpio/gpio11/direction
        ;;
    esac
}

function main() {
    init_io
    init_hardware_info
    init_hwinfo
    init_wan_dhcp_fallbackip

    local port_right=$(cat /etc/whsnbg.conf | grep "mqtt_port = 1884")
    if [[ "$port_right" != "" ]]; then
        echo "whsnbg port 1884 is right, no need change"
    else
        sed -i "s/mqtt_port = 1883/mqtt_port = 1884/" /etc/whsnbg.conf
    fi

    local name_right=$(cat /etc/config/dhcp | grep "gateway.lnxall.com")
    if [[ "$name_right" != "" ]]; then
        echo "whsnbg port 1884 is right, no need change"
    else
        sed -i "s/gw.dyiots.com/gateway.lnxall.com/" /etc/config/dhcp
    fi
}

main
