#!/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-mega_data_partition.sh ]
then
    /root/check_and_create_gr-mega_data_partition.sh
    if [ -e /tmp/wedidit ]
    then
        rm /root/check_and_create_gr-mega_data_partition.sh
        reboot
    fi
fi

/bin/bash /root/config-midi.sh

until false; do
    cd /home/cm4test

    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 1 ]; then
        halt
        exit 0
    fi

    # returned some audio problem.. try to bootstrap audio drivers, then retry the loop
    if [ "$ret" -eq 2 ]; then
        modprobe -r snd_soc_audioinjector_isolated_soundcard
        modprobe snd_soc_audioinjector_isolated_soundcard
        sleep 1
        modprobe -r snd_soc_audioinjector_isolated_soundcard
        modprobe snd_soc_audioinjector_isolated_soundcard
    fi

    # some video problem.. but this can be handled internally.. this can be reclaimed
    if [ "$ret" -eq 3 ]; then
        echo "NO DISPLAY FOUND!"
    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 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

