#!/bin/bash

EXEC=./ursengine

echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
echo performance > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor
echo performance > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor
echo performance > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor

sudo rm /lib/aarch64-linux-gnu/libSDL2-2.0.so.0
sudo ln -s /usr/local/lib/libSDL2-2.0.so.0 /lib/aarch64-linux-gnu/libSDL2-2.0.so.0

if [ -f /root/check_and_create_gr-2_data_partition.sh ]
then
    /root/check_and_create_gr-2_data_partition.sh
    if [ -e /tmp/wedidit ]
    then
        rm /root/check_and_create_gr-2_data_partition.sh
        reboot
    else
        echo "!!!failed to make data partition!!!"
        exit 1
    fi
fi

/bin/bash /root/remove_dead_mountpoints.sh

until false; do
    cd /home/gr2

    if [ -s $EXEC ]
    then
        $EXEC
        ret=$?
    else
        ret=127
    fi
    echo "ursengine exit code $ret."

    if [ "$ret" -eq 0 ]; then
        # clean exit
        exit 0
    fi

    if [ "$ret" -eq 5 ]; then
        halt
        exit 0
    fi

    # returned some audio problem.. try to bootstrap audio drivers, then retry the loop
    if [ "$ret" -eq 2 ]; then
        CARD_DRV=`lsmod | awk '$1 ~ /_soundcard$/ {print $1; exit}'`
        if [ -z "$CARD_DRV" ]; then
            case `tr -d '\0' < /proc/device-tree/soc/sound/compatible 2>/dev/null` in
                *gr2-ssm2603*) CARD_DRV=snd_soc_gr2_ssm2603_soundcard ;;
                *)             CARD_DRV=snd_soc_audioinjector_pi_soundcard ;;
            esac
        fi
        echo "audio recovery: reloading $CARD_DRV"
        modprobe -r $CARD_DRV
        modprobe $CARD_DRV
        sleep 1
        modprobe -r $CARD_DRV
        modprobe $CARD_DRV
    fi

    # some video problem.. but this can be handled internally.. this can be reclaimed
    if [ "$ret" -eq 3 ]; then
        echo "NO DISPLAY FOUND!"
        modprobe -r panel_waveshare_dsi vc4 2>/dev/null
        sleep 0.5
        modprobe panel_waveshare_dsi
        modprobe vc4
    fi

    if [ "$ret" -eq 4 ]; then
        reboot
        exit 0
    fi

    # executable doesn't exist or corrupt??
    if [ "$ret" -eq 127 ]; then
        # first try old one
        echo "primary executable failed, trying old version.."
        if [ -s ./ursengine.old ]
        then
            ./ursengine.old
            ret=$?
        else
            ret=127
        fi
        echo "ursengine.old exit code $ret."

        # old executable doesn't exist or corrupt??
        # try usb.. our last hope
        if [ "$ret" -eq 127 ]; then
            # check scsi devices that are mounted under the /media directory.. this automatically excludes our sata disk that's mounted on /
            # the following explosion in the proverbial ascii factory convinced me that even for simple scripts like these, it's better just to use rust
            mount_rows=`rg "/dev/sd" /proc/mounts | rg "/media/" | head -n1`
            num_rows=`echo "$mount_rows" | wc -l`
            mount_point=`echo $mount_rows | tr -s ' ' | cut -d " " -f 2`
            mount_point_spaced=`sed 's/\\\040/ /g' <<<"$mount_point"`
            # printf -v mount_point_spaced_quoted "%s" "$mount_point_spaced"
            echo "checking mount point: [$mount_point_spaced].."
            echo "num_rows [$num_rows]"
            if [ "$num_rows" -eq 1 ] & [ -d "$mount_point_spaced" ]
            then
                echo "internal executables failed, trying usb.."
                # if this is a fat/msdos mount, then we need to use the umask option when mounting!
                "$mount_point_spaced/ursengine"
            else
                echo "failed to find a mounted usb stick"
            fi

        fi
    fi

    sleep 1
    echo "respawn.."
done
