#!/bin/bash

export TERM=xterm-256color
export LINES=24
export COLUMNS=80

#whiptail --msgbox "TestBox" 10 40

#echo "Checking hardware presence:"

echo -n > /tmp/l5dvk-test.log

# GPS
stty 9600 -icrnl < /dev/ttymxc2
read -t 5 -N 120 GPS < /dev/ttymxc2
echo $GPS | grep \$G > /dev/null
if [ $? = 0 ]; then
	echo "GPS      pass" >> /tmp/l5dvk-test.log;
else
	echo "GPS      fail" >> /tmp/l5dvk-test.log;
fi

# WiFi module presence
if [ `cat /sys/bus/sdio/devices/mmc1\:fffd\:1/device` = "0x9116" ]; then
	echo "RS9116   pass" >> /tmp/l5dvk-test.log;
else
	echo "RS9116   fail" >> /tmp/l5dvk-test.log;
fi

lsusb -d 14cd:8601 | grep "Super Top" > /dev/null
if [ $? = 0 ]; then
        echo "USB HUB  pass" >> /tmp/l5dvk-test.log;
else
        echo "USB HUB  fail" >> /tmp/l5dvk-test.log;
fi

lsusb -d 14cd:8170 | grep "Super Top" > /dev/null
if [ $? = 0 ]; then
        echo "microSD  pass" >> /tmp/l5dvk-test.log;
else
        echo "microSD  fail" >> /tmp/l5dvk-test.log;
fi

# SEC1110 smartcard reader
lsusb -v -d 0424:1104 | grep CCID > /dev/null
if [ $? = 0 ]; then
        echo "SEC1110  pass" >> /tmp/l5dvk-test.log;
else
        echo "SEC1110  fail" >> /tmp/l5dvk-test.log;
fi

# SIMcom WWAN modem
lsusb -d 1e0e:9001 | grep Qualcomm > /dev/null
if [ $? = 0 ]; then
        echo "WWAN     pass" >> /tmp/l5dvk-test.log;
else
        echo "WWAN     fail" >> /tmp/l5dvk-test.log;
fi

# audio codec
aplay -l | grep sgtl5000 > /dev/null
if [ $? = 0 ]; then
        echo "SGTL500  pass" >> /tmp/l5dvk-test.log;
else
        echo "SGTL500  fail" >> /tmp/l5dvk-test.log;
fi

cat /sys/class/power_supply/bq25890-charger/health | grep Good > /dev/null
if [ $? = 0 ]; then
        echo "BQ25890  pass" >> /tmp/l5dvk-test.log;
else
        echo "BQ25890  fail" >> /tmp/l5dvk-test.log;
fi

if [ -e /dev/mtd0 ]; then
	echo -n "Purism SPC" > /dev/mtd0; sync
	hexdump -n 10 -e '8/1 "%c"' /dev/mtd0ro | grep "Purism SPC"
	if [ $? = 0 ]; then
	        echo "SPIflash pass" >> /tmp/l5dvk-test.log;
	else
		echo "SPIflash fail" >> /tmp/l5dvk-test.log;
	fi
else
	echo "SPIflash fail" >> /tmp/l5dvk-test.log;
fi

# I2C devices bus 0: 4b, 52, 68, 6b
if [ -e /sys/class/i2c-adapter/i2c-0/0-004b/bd71837-pmic/registers ]; then
        echo "I2C0@4b  pass" >> /tmp/l5dvk-test.log;
else
        echo "I2C0@4b  fail" >> /tmp/l5dvk-test.log;
fi
i2cdetect -y 0 0x52 0x52 | grep UU > /dev/null
if [ $? = 0 ]; then
        echo "I2C0@52  pass" >> /tmp/l5dvk-test.log;
else
        echo "I2C0@52  fail" >> /tmp/l5dvk-test.log;
fi
i2cdetect -y 0 0x68 0x68 | grep UU > /dev/null
if [ $? = 0 ]; then
        echo "I2C0@68  pass" >> /tmp/l5dvk-test.log;
else
        echo "I2C0@68  fail" >> /tmp/l5dvk-test.log;
fi
i2cdetect -y 0 0x6b 0x6b | grep UU > /dev/null
if [ $? = 0 ]; then
        echo "I2C0@6b  pass" >> /tmp/l5dvk-test.log;
else
        echo "I2C0@6b  fail" >> /tmp/l5dvk-test.log;
fi

# I2C devices bus 2: 0a, 1e*, 5d, 6a
i2cdetect -y 2 0x0a 0x0a | grep UU > /dev/null
if [ $? = 0 ]; then
        echo "I2C2@0a  pass" >> /tmp/l5dvk-test.log;
else
        echo "I2C2@0a  fail" >> /tmp/l5dvk-test.log;
fi
i2cdetect -y 2 0x1e 0x1e | grep 1e > /dev/null
if [ $? = 0 ]; then
        echo "I2C2@1e  pass" >> /tmp/l5dvk-test.log;
else
        echo "I2C2@1e  fail" >> /tmp/l5dvk-test.log;
fi
i2cdetect -y 2 0x5d 0x5d | grep UU > /dev/null
if [ $? = 0 ]; then
        echo "I2C2@5d  pass" >> /tmp/l5dvk-test.log;
else
        echo "I2C2@5d  fail" >> /tmp/l5dvk-test.log;
fi

cat /sys/class/i2c-adapter/i2c-2/2-006a/iio:device0/name | grep lsm9ds1-imu_gyro > /dev/null
if [ $? = 0 ]; then
        echo "I2C2@6a  pass" >> /tmp/l5dvk-test.log;
else
        echo "I2C2@6a  fail" >> /tmp/l5dvk-test.log;
fi

whiptail --title "Hardware presence" --scrolltext --textbox /tmp/l5dvk-test.log 15 60

amixer sset "Lineout" 31 > /dev/null 2>&1
amixer sset "PCM" 154 > /dev/null 2>&1
amixer sset "Headphone" 127 > /dev/null 2>&1

whiptail --yesno "Is there a picture on the HDMI screen?" 15 60

whiptail --msgbox "Listen for sound from headphone and speaker" 15 60
aplay /usr/local/bin/piano2.wav > /dev/null 2>&1
whiptail --yesno "Sound heard?" 15 60

whiptail --msgbox "Watch user LED blink two times" 15 60
echo 1 > /sys/class/leds/LED\ 1/brightness
sleep 1
echo 0 > /sys/class/leds/LED\ 1/brightness
sleep 1
echo 1 > /sys/class/leds/LED\ 1/brightness
sleep 1
echo 0 > /sys/class/leds/LED\ 1/brightness
whiptail --yesno "LED OK?" 15 60

whiptail --msgbox "Vibration motor will vibrate two times" 15 60
echo 128 > /sys/class/leds/nxp\:\:haptic/brightness
sleep 1
echo 0 > /sys/class/leds/nxp\:\:haptic/brightness
sleep 1
echo 128 > /sys/class/leds/nxp\:\:haptic/brightness
sleep 1
echo 0 > /sys/class/leds/nxp\:\:haptic/brightness
whiptail --yesno "Motor OK?" 15 60



whiptail --msgbox "Will activate WiFi and scan\nthis takes up to 15 seconds" 15 60
modprobe rsi_sdio > /dev/null 2>&1
sleep 2
nmcli device wifi list | grep NETGEAR13 > /dev/null
if [ $? = 0 ]; then
	echo "WiFi     pass" >> /tmp/l5dvk-test.log;
	whiptail --msgbox "WiFi OK, scan OK" 15 60
else
	echo "WiFi     fail" >> /tmp/l5dvk-test.log;
	whiptail --msgbox "WiFi OK, scan OK" 15 60
fi

