#!/bin/bash

failures=0
uname -a
uptime
if [ ! -e "/sys/class/thermal/thermal_zone0/temp" ]; then
    echo "Thermal driver not loaded"
    failures=$((${failures}+1))
else
    TEMP=$( cat /sys/class/thermal/thermal_zone0/temp )
    echo "A53 temp $TEMP"
    if [ -e /sys/class/thermal/thermal_zone1 ]; then
        TEMP=$( cat /sys/class/thermal/thermal_zone1/temp )
        echo "GPU temp $TEMP"
        TEMP=$( cat /sys/class/thermal/thermal_zone2/temp )
        echo "VPU temp $TEMP"
    else
        echo "Multi TMU thermal driver not loaded"
    fi
fi

if [ -e "/sys/class/regulator/"  ]; then
    REGULATORS=$(ls -d /sys/class/regulator/*)

    for REG in ${REGULATORS}; do
        NAME=$(cat ${REG}/name)
        if [ -e ${REG}/state ]; then
            STATE=$(cat ${REG}/state)
        else
            STATE="NA"
        fi
        STANDBY=$(cat ${REG}/suspend_mem_state)
        USERS=$(cat ${REG}/num_users)
        echo "Regulator ${NAME} ${STATE} standby ${STANDBY} users ${USERS}"
    done
else
    echo "Regulator driver not loaded"
    failures=$((${failures}+1))
fi

TEST=$( lsusb | grep "Linux Foundation 3.0 root hub" )
if [ -z "${TEST}" ]; then
    echo "USB 3 subsystem failed to load"
    failures=$((${failures}+1))
fi

TEST=$( lsusb | grep "Linux Foundation 2.0 root hub" )
if [ -z "${TEST}" ]; then
    echo "USB 2 subsystem failed to load"
    failures=$((${failures}+1))
fi

TEST=$( lsusb | grep "Qualcomm / Option" )
if [ -z "${TEST}" ]; then
    echo "Qualcomm modem driver failed to load"
    failures=$((${failures}+1))
fi

TEST=$( lsusb | grep "Super Top" )
if [ -z "${TEST}" ]; then
    echo "SD card driver failed to load"
    failures=$((${failures}+1))
fi

TEST=$( ifconfig | grep "eth0" )
if [ -z "${TEST}" ]; then
    echo "Ethernet driver failed to load"
    failures=$((${failures}+1))
fi

TEST=$( ifconfig | grep "usb0" )
if [ -z "${TEST}" ]; then
    echo "USB ethernet driver failed to load"
    failures=$((${failures}+1))
fi

TEST=$( ifconfig | grep "usbcore" | grep "qmi_wwan" )
echo $TEST
if [ -z "${TEST}" ]; then
    echo "WWAN driver failed to load"
    failures=$((${failures}+1))
fi

if [ -e "/dev/i2c-0" ]; then
    TEST=$( i2cdetect -y 0 | grep "4b" )
    if [ -n "${TEST}" ]; then
        echo "pmic driver failed to load"
        failures=$((${failures}+1))
    fi

    TEST=$( i2cdetect -y 0 | grep "52" )
    if [ -n "${TEST}" ]; then
        echo "USB type C driver failed to load"
        failures=$((${failures}+1))
    fi

    TEST=$( i2cdetect -y 0 | grep "68" )
    if [ -n "${TEST}" ]; then
        echo "rtc driver failed to load"
        failures=$((${failures}+1))
    fi

    TEST=$( i2cdetect -y 0 | grep "6b" )
    if [ -n "${TEST}" ]; then
        echo "charger driver failed to load"
        failures=$((${failures}+1))
    fi
else
    echo "i2c bus 0  failed to load"
    failures=$((${failures}+1))
fi

if [ -e "/dev/i2c-2" ]; then
    TEST=$( i2cdetect -y 2 | grep "1e" )
    if [ -n "${TEST}" ]; then
        echo "magnetometer driver failed to load"
        failures=$((${failures}+1))
    fi

    TEST=$( i2cdetect -y 2 | grep "5d" )
    if [ -n "${TEST}" ]; then
        echo "touchscreen driver failed to load"
        failures=$((${failures}+1))
    fi
else
    echo "i2c bus 0  failed to load"
    failures=$((${failures}+1))
fi

alsabat -D plughw:CARD=sgtl5000,DEV=0 -c2 --saveplay default_dual.wav --standalone

echo "There were ${failures} static test failures"
