#!/bin/sh

test_wan() {
    interface=$1
    remote_addr=$2
    echo "interfaces : $interfaces"
    echo "remote_addr: $remote_addr"

    local_ip=$(ifconfig $interfaces | awk -F'[: \t]' '/inet addr/{print $13}')
    echo "local_ip: $local_ip"
    if [ -z "$local_ip" ]; then
        echo "- WAN=FAILED_NO_IP!!!"
        return 1
    fi

    result=$(ping -I $interface -c 1 -w 3 $remote_addr | grep "64 bytes from")
    if [ -z "$result" ]; then
        echo "- WAN=FAILED_PING_ERR!!!"
        return 1
    fi
    echo "+ WAN=PASSED"
    return 0
}

test_4G() {
    interface=$1
    remote_addr=$2
    echo "interfaces : $interfaces"
    echo "remote_addr: $remote_addr"

    local_ip=$(ifconfig $interfaces | awk -F'[: \t]' '/inet addr/{print $13}')
    echo "local_ip: $local_ip"
    if [ -z "$local_ip" ]; then
        echo "- WAN=FAILED_NO_IP!!!"
        return 1
    fi

    result=$(ping -I $interface -c 1 -w 3 $remote_addr | grep "64 bytes from")
    if [ -z "$result" ]; then
        echo "- 4G=FAILED_PING_ERR!!!"
        return 1
    fi
    echo "+ 4G=PASSED"
    return 0
}

test_lan() {
    interface=$1
    remote_addr=$2
    local_addr=$3
    echo "interfaces : $interfaces"
    echo "remote_addr: $remote_addr"

    ifconfig $interface $local_addr

    local_ip=$(ifconfig $interfaces | awk -F'[: \t]' '/inet addr/{print $13}')
    echo "local_ip: $local_ip"
    if [ -z "$local_ip" ]; then
        echo "- LAN=FAILED_NO_IP!!!"
        return 1
    fi

    result=$(ping -I $interface -c 1 -w 3 $remote_addr | grep "64 bytes from")
    if [ -z "$result" ]; then
        echo "- LAN=FAILED_PING_ERR!!!"
        return 1
    fi
    echo "+ LAN=PASSED"
    return 0
}

test_wifi()
{
	interfaces="wlan"$1
	error_code=0
	error_msg="success"
	id="$1"
	ifconfig wlan${id} > /dev/null 2>&1
	if [ "$?" != "0" ]
	then
		echo "- WIFI${id}=FAILED!!!"
		return 1
	fi
	echo "+ WIFI${id}=PASSED"
	return 0
}

test_udisk() {
    node=$1
    if [ ! -e $node ]; then
            echo "- $node=FAILED!!!"
            return 1
    fi
    echo "+ $node=PASSED"
    return 0
}

test_sdcard(){
	if [ ! -e "/mnt/mmc" ]; then
		mkdir /mnt/mmc
	fi

	if [ -e "/dev/mmcblk0" ]; then
        umount /dev/mmcblk0p1
		echo ",,b
		write
		" | sfdisk /dev/mmcblk0      
        mount /dev/mmcblk0p1 /mnt/mmc
		touch /mnt/mmc/1
        if [ ! -e "/mnt/mmc/1" ]; then
			echo "error:sd card can not mount"
            echo "- tfcard=FAILED!!!"
			return 1
		else
			# if umount success file will disappear
			umount /dev/mmcblk0p1
			if [ ! -e "mnt/mmc/1" ];then
				echo "sd card ok"
                echo "+ tfcard=PASSED"
                mount /dev/mmcblk0p1 /mnt/mmc
				rm /mnt/mmc/1
                umount /dev/mmcblk0p1  
				return 0
			else
				echo "error sd card"
                echo "- tfcard=FAILED!!!"
				return 1
			fi
		fi
	else
		echo "No mmcblk0 NODE,Failed!!!"
        echo "- tfcard=FAILED!!!"
		return 1
	fi
	return 1
}

test_wan "eth0" "114.114.114.114"
test_4G "usb0" "114.114.114.114"
test_lan "br-lan" "192.168.8.200" "192.168.8.100"
test_wifi "0" 
test_udisk "/dev/sda"
test_sdcard
