#!/bin/bash
# Install the wireOS recovery menu onto DEV Vector 2.0s and 1.0s

set -e

echo "Mount partitions as RWE"
mount -o rw,remount,exec /
mount -o rw,remount,exec /data
cd /data

echo "Checking if recoveryfs exists"
if [ -f /dev/block/bootdevice/by-name/recoveryfs ]; then
	echo "Why you running this on a dvt2 or 1 bro :wiredefeatcry:"
    exit 1
else
    echo "Ok, good to continue"
fi

# Up CPU to full speed
echo "Upping CPU speed, robot will feel very hot"
echo 1267200 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq

echo "Stop robot processes"
systemctl stop anki-robot.target
sleep 2
sleep 2
mkdir -p /data/install-dev-recovery
sleep 3
sleep 1
echo "Download Recoveryfs"
curl -o /data/install-dev-recovery/recfs.img.gz http://modder.my.to:81/wireos-DEV-recOS/wireos-DEV-recoveryfs.img.gz
echo "Download Recovery"
curl -o /data/install-dev-recovery/rec.img.gz http://modder.my.to:81/wireos-DEV-recOS/wireos-DEV-recovery.img.gz

sleep 5

echo "Checking if recoveryfs exists"
# Define the expected hash
EXPECTED_HASH_RFS="ad374059129aa5b1fe4731f975cf130f"

# Check if the file exists
if [ ! -f /data/install-dev-recovery/recfs.img.gz ]; then
    echo "recoveryfs does not exist. Confirm recoveryfs is on the server or download it to your bot manually."
    exit 0
else
    echo "recoveryfs is found. Proceeding to check the file hash."

    # Compute the hash of the file
    if command -v md5sum >/dev/null 2>&1; then
        ACTUAL_HASH_RFS=$(md5sum /data/install-dev-recovery/recfs.img.gz | awk '{print $1}')
    else
        echo "Error: md5sum command not found. Please install it to proceed."
        exit 1
    fi

    # Compare the hash values
    if [ "$ACTUAL_HASH_RFS" = "$EXPECTED_HASH_RFS" ]; then
        echo "File hash matches the expected value."
    else
        echo "Error: File hash does NOT match the expected value!"
        echo "Please delete files and try again"
        echo "Expected: $EXPECTED_HASH_RFS"
        echo "Actual:   $ACTUAL_HASH_RFS"
        exit 1
    fi

    # Clear the hash variable
    unset ACTUAL_HASH_RFS
    echo "RecoveryFS is good to go"
fi
sleep 5


echo "Checking if recovery exists"
# Define the expected hash
EXPECTED_HASH_REC="a81470f787019a3151ae1172d04d11a3"
# Check if the file exists
if [ ! -f /data/install-dev-recovery/rec.img.gz ]; then
    echo "recovery does not exist. Confirm recovery is on the server or download it to your bot manually."
    exit 0
else
    echo "recovery is found. Proceeding to check the file hash."

    # Compute the hash of the file
    if command -v md5sum >/dev/null 2>&1; then
        ACTUAL_HASH_REC=$(md5sum /data/install-dev-recovery/rec.img.gz | awk '{print $1}')
    else
        echo "Error: md5sum command not found. Please install it to proceed."
        exit 1
    fi

    # Compare the hash values
    if [ "$ACTUAL_HASH_REC" = "$EXPECTED_HASH_REC" ]; then
        echo "File hash matches the expected value."
    else
        echo "Error: File hash does NOT match the expected value!"
        echo "Please delete files and try again"
        echo "Expected: $EXPECTED_HASH_REC"
        echo "Actual:   $ACTUAL_HASH_REC"
        exit 1
    fi
    #Clear hash
    unset ACTUAL_HASH_REC
    echo "Recovery is good to go"
fi
sleep 5

# Wait for 5 seconds before proceeding
sleep 5

echo "Writing RecoveryFS (This is the longest part)"
gunzip -c "/data/install-dev-recovery/recfs.img.gz" > "/dev/block/bootdevice/by-name/recoveryfs"
echo "Writing Recovery"
gunzip -c "/data/install-dev-recovery/rec.img.gz" > "/dev/block/bootdevice/by-name/recovery"

sleep 5

echo "Removing temporary files"
rm -rf /data/install-dev-recovery/

sync
sync
sync
echo "Done! Rebooting in 10 seconds. If the bot doesn't boot then manually hold the back button for 15 seconds to manually go to recovery"
sleep 10
reboot
