#!/bin/bash
#Revert DEV or OSKR Vector 1.0s to prod

set -e

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

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


echo "Checking if recovery exists"
if [ ! -f /dev/block/bootdevice/by-name/aboot ]; then
    echo "Ok you good"
else
    echo "How the hell do you run without a aboot, is that even possible????"
    exit 1
fi


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

echo "Stop robot processes"
systemctl stop anki-robot.target
sleep 2
mkdir -p /data/revert-to-prod
sleep 3
echo "Download aboot"
curl -o /data/revert-to-prod/aboot.img http://modder.my.to:81/revert-to-prod/aboot.img
echo "Download Recovery"
curl -o /data/revert-to-prod/rec.img.gz http://modder.my.to:81/prod-recOS/prod-recovery.img.gz
echo "Download Recoveryfs (This takes a while)"
curl -o /data/revert-to-prod/recfs.img.gz http://modder.my.to:81/prod-recOS/prod-recoveryfs.img.gz

sleep 5

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

# Check if the file exists
if [ ! -f /data/revert-to-prod/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/revert-to-prod/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 "Expected: $EXPECTED_HASH_RFS"
        echo "Actual:   $ACTUAL_HASH_RFS"
        sleep 2
        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="dc040ea45f808773ae7a8236ef51f42c"
# Check if the file exists
if [ ! -f /data/revert-to-prod/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/revert-to-prod/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 "Expected: $EXPECTED_HASH_REC"
        echo "Actual:   $ACTUAL_HASH_REC"
        sleep 2
        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/revert-to-prod/recfs.img.gz" > "/dev/block/bootdevice/by-name/recoveryfs"
echo "Writing Recovery"
gunzip -c "/data/revert-to-prod/rec.img.gz" > "/dev/block/bootdevice/by-name/recovery"
echo "Restoring Aboot"
dd if=/dev/block/bootdevice/by-name/abootbak of=/dev/block/bootdevice/by-name/aboot
echo "Clear boot slots"
set +e
dd if=/dev/zero of=/dev/block/bootdevice/by-name/boot_a
dd if=/dev/zero of=/dev/block/bootdevice/by-name/boot_b
set -e

echo "Installing prod ota just in case so you can install your unlock ota in case recovery doesn't work"
curl -o /data/update-engine-signed http://modder.my.to:81/update-engine-signed 

chmod +rwx /data/update-engine-signed

/data/update-engine-signed http://modder.my.to:81/otas/Anki/prod/1.6.0.3331.ota -v

# Double check boot slots
bootctl a set_active f
bootctl b set_active f
bootctl a mark_successful f
bootctl b mark_successful f

# This will wipe all user data on the bot to ensure there are no problems
echo "Erasing user data"
touch /run/wipe-data

sleep 5

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