#!/bin/bash

# Variables
TARGET_BOLT_DATE="2025-06-23"
TARGET_VERSION="2525"
DOWNLOAD_URL="https://source.mynonpublic.com/gigablue/bolt/${TARGET_VERSION}/bolt_signed.bin"
FILE_NAME="bolt_signed.bin"
MD5_EXPECTED="7ed40d94e8e033fd9c468cba16772434"
FLASH_DEVICE="/dev/mtdblock0"
EXPECTED_MODEL="gbquad4kpro"
EMMC_SIZE_PATH="/sys/block/mmcblk0/size"

# /sys/block/mmcblk0/size reports 512-byte sectors.
# Rev1 uses 8 GB eMMC and is expected to be clearly above 12,000,000 sectors.
# Rev2 uses 4 GB eMMC and is expected to be clearly below 10,000,000 sectors.
REV1_MIN_SECTORS=12000000
REV2_MAX_SECTORS=10000000

# Check device model
DEVICE_MODEL=$(cat /proc/stb/info/gbmodel 2>/dev/null)

echo "Starting bootloader update for Gigablue Quad4K Pro Rev1..."
echo "Detected model: $DEVICE_MODEL"

if [[ "$DEVICE_MODEL" != "$EXPECTED_MODEL" ]]; then
    echo "Error: This update is only for model '$EXPECTED_MODEL'. Detected model '$DEVICE_MODEL' is not supported."
    exit 1
fi
echo "Model verification passed: $DEVICE_MODEL"

# Check hardware revision by eMMC size
if [[ ! -r "$EMMC_SIZE_PATH" ]]; then
    echo "Error: Cannot read eMMC size from $EMMC_SIZE_PATH. Aborting."
    exit 1
fi

EMMC_SECTORS=$(cat "$EMMC_SIZE_PATH" 2>/dev/null | awk '{print $1}')

if ! [[ "$EMMC_SECTORS" =~ ^[0-9]+$ ]]; then
    echo "Error: Invalid eMMC size value '$EMMC_SECTORS' from $EMMC_SIZE_PATH. Aborting."
    exit 1
fi

EMMC_SIZE_MIB=$((EMMC_SECTORS / 2048))

echo "Detected eMMC size: $EMMC_SECTORS sectors (${EMMC_SIZE_MIB} MiB)"

if (( EMMC_SECTORS >= REV1_MIN_SECTORS )); then
    DEVICE_REVISION="rev1"
    echo "Hardware revision detected: Rev1 / 8 GB eMMC"
elif (( EMMC_SECTORS <= REV2_MAX_SECTORS )); then
    DEVICE_REVISION="rev2"
    echo "Hardware revision detected: Rev2 / 4 GB eMMC"
    echo "Info: This bootloader update is only intended for Rev1 devices. Rev2 detected, aborting without flashing."
    exit 1
else
    echo "Error: Unknown eMMC size. Cannot safely determine whether this device is Rev1 or Rev2. Aborting."
    exit 1
fi

# Check current bootloader version date
CURRENT_BOLT_DATE=$(cat /proc/device-tree/bolt/date 2>/dev/null | awk '{print $1}')

echo "Current bootloader date: $CURRENT_BOLT_DATE"
echo "Target bootloader date: $TARGET_BOLT_DATE"

if [[ "$CURRENT_BOLT_DATE" == "$TARGET_BOLT_DATE" ]]; then
    echo "The bootloader is already up-to-date. No update required."
    exit 0
else
    echo "A newer bootloader version is available for $DEVICE_REVISION. Proceeding with the update..."
fi

# Download the file
echo "Downloading bootloader file from $DOWNLOAD_URL..."
wget -q -O "$FILE_NAME" "$DOWNLOAD_URL"

if [[ $? -ne 0 ]]; then
    echo "Error downloading the file. Aborting."
    exit 1
fi
echo "Download completed."

# Verify MD5 hash
echo "Verifying MD5 hash of the downloaded file..."
MD5_ACTUAL=$(md5sum "$FILE_NAME" | awk '{print $1}')

if [[ "$MD5_ACTUAL" != "$MD5_EXPECTED" ]]; then
    echo "MD5 hash mismatch. Aborting."
    exit 1
fi
echo "MD5 hash verified successfully."

# Perform the update
echo "Starting the flash process with dd to $FLASH_DEVICE..."
dd if="$FILE_NAME" of="$FLASH_DEVICE" bs=4M conv=fsync

if [[ $? -ne 0 ]]; then
    echo "Error flashing the bootloader. Aborting."
    exit 1
fi
echo "Bootloader updated successfully."

# Final message
echo "Bootloader update completed."
echo "New bootloader date: $TARGET_BOLT_DATE"
exit 0
