#!/bin/sh
. /lib/dyiot/bin/diag_sys.sh

json_init

# uboot_env=`fw_printenv`
uboot_env=$(factory get)
mac_addr=$(echo "$uboot_env" | awk -F'=' '/^MAC=/{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}')
echo "HWVER: $hwver"
echo "SN: $sn"
echo "DATE: $date"
echo "SWVER: $(cat /etc/issue)"
json_add_string "sn" "$sn"
json_add_string "product" "$PRODUCT"
json_add_int "timestamp" $(date +%s)
json_add_string "MAC" "$mac_addr"
json_add_string "MAC1" "$mac_addr1"
json_add_string "HWVER": "$hwver"
json_add_string "SWVER": "$(cat /etc/issue)"

prepare_env

file_diff() {
    local file0="$1"
    local file1="$2"
    local md50="$(md5sum ${file0} | awk '{print $1}')"
    local md51=""
    [ -f "${file1}" ] && md51="$(md5sum ${file1} | awk '{print $1}')"
    if [ "${md50}" = "${md51}" ] ; then
        # echo "file is the same: '${file0}' '${file1}'" 1>&2
        return 0
    fi
    return 1
}

backup_etc_config_wireless() {
    if [ ! -e '/etc/config/wireless.bak' ] ; then
        mv '/etc/config/wireless' '/etc/config/wireless.bak'
        sleep 1
    fi

    if [ ! -e '/tmp/wireless.test' ] ; then
        cat <<EOF >/tmp/wireless.test
config wifi-device 'radio0'
        option type 'mac80211'
        option path '1e140000.pcie/pci0000:00/0000:00:01.0/0000:02:00.0'
        option channel 'auto'
        option band '2g'
        option htmode 'HT20'
        option disabled '0'

config wifi-iface 'default_radio0'
        option device 'radio0'
        option network 'wifiwan'
        option mode 'sta'
        option ssid 'lnxallAP_2G'
        option encryption 'psk-mixed'
        option key 'lnxall123'
        option disabled '0'

config wifi-device 'radio1'
        option type 'mac80211'
        option path '1e140000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0'
        option channel 'auto'
        option band '5g'
        option htmode 'VHT80'
        option disabled '0'

config wifi-iface 'default_radio1'
        option device 'radio1'
        option mode 'sta'
        option ssid 'lnxallAP_5G'
        option encryption 'psk-mixed'
        option key 'lnxall123'
        option disabled '0'
EOF
    fi
    file_diff '/tmp/wireless.test' '/etc/config/wireless' || cp -v \
        '/tmp/wireless.test' '/etc/config/wireless'
    /etc/init.d/network reload

    local count=0
    local notok=1
    # wait for wlan0 have IPv4 address
    while true ; do
        local ipaddr="`ip addr show dev wlan0 2>/dev/null | sed -n -r -e 's#^\s+inet\s+([0-9./]+)\s+.+$#\1#p'`"
        if [ -n "${ipaddr}" ] ; then
            notok=0
            break
        fi
        count=$((count + 1))
        [ ${count} -gt 10 ] && break
        sleep 1
    done
    return $notok
}

restore_etc_config_wireless() {
    if [ ! -e '/etc/config/wireless.bak' ] ; then
        echo "Error, wireless.bak not found" 1>&2
        return 1
    fi
    file_diff '/etc/config/wireless.bak' '/etc/config/wireless' || cp -v \
        '/etc/config/wireless.bak' '/etc/config/wireless'
    return $?
}

json_add_result_to_obj1() {
    json_add_object
    json_add_string "interfaces" "$1"
    json_add_int "error_code" "$2"
    json_add_string "error_msg" "$3"
    json_close_object
}

json_add_object "data"

json_add_array	"ALL"

msg=$(test_led led-heart led-internet led-pcie1 led-pcie2 led-pcie3 led-wlan2 led-wlan5 mt76-phy1 mt76-phy0)
ret=$?
json_add_result_to_obj "led" "$ret" "$msg"

msg=$(test_rtc)
ret=$?
json_add_result_to_obj "RTC" "$ret" "$msg"

msg=$(test_internet usb0)
ret=$?
json_add_result_to_obj "4G" "$ret" "$msg"

msg=$(test_internet wan)
ret=$?
json_add_result_to_obj "WAN" "$ret" "$msg"

msg=$(test_lan)
ret=$?
json_add_result_to_obj "LAN" "$ret" "$msg"

msg=$(test_hci hci0)
ret=$?
json_add_result_to_obj "BLE1" "$ret" "$msg"

msg=$(test_hci hci1)
ret=$?
json_add_result_to_obj "BLE2" "$ret" "$msg"

msg=$(test_hci hci2)
ret=$?
json_add_result_to_obj "BLE3" "$ret" "$msg"

if backup_etc_config_wireless ; then
    siglevel_2g="`lua /app/tools/diag_chuanye_signal.lua wlan0 lnxallAP_2G`"
    siglevel_5g="`lua /app/tools/diag_chuanye_signal.lua wlan1 lnxallAP_5G`"
    if [ -z "${siglevel_2g}" ] ; then
        json_add_result_to_obj1 "signal_2g" "1" "Failed connect to lnxallAP_2G"
    else
        json_add_result_to_obj1 "signal_2g" "0" "${siglevel_2g} dBm"
    fi
    if [ -z "${siglevel_5g}" ] ; then
        json_add_result_to_obj1 "signal_5g" "1" "Failed connect to lnxallAP_5G"
    else
        json_add_result_to_obj1 "signal_5g" "0" "${siglevel_5g} dBm"
    fi
else
    json_add_result_to_obj1 "signal_2g" "1" "Failed connect to lnxallAP_2G"
    json_add_result_to_obj1 "signal_5g" "1" "Failed connect to lnxallAP_5G"
fi
restore_etc_config_wireless

json_close_array
json_close_object

echo ""
echo "---TEST SUMMARY---"
#cat $TEST_SUMMARY
sleep 1
json_str=$(json_dump)
echo "test_result:$json_str"

restore_env
