UPGRADE_FW_MAJOR="2022-09-13-0-11012d53-22.08-plus"
UPGRADE_FW_VERSION="2026-04-14-0-912d084c-26.04-plus"
UPGRADE_FW_REQUIRE="2022-09-13-0-11012d53-22.08-plus"

current_fw_version() {
    cat /etc/bos_version
}

current_fw_major() {
    cat /etc/bos_major
}

check_fw_version() {
    echo "Gathering current firmware information..."

    local fw_major=$(current_fw_major)
    local fw_version=$(current_fw_version)

    echo "version             : $fw_version"
    echo "major               : $fw_major"

    echo "Checking compatibility..."

    if [ "$UPGRADE_FW_VERSION" ">" "$fw_version" ]; then
        # firmware upgrade
        if [ "$UPGRADE_FW_REQUIRE" ">" "$fw_version" ]; then
            echo "Firmware upgrade to '$UPGRADE_FW_VERSION' is not possible!"
            echo "Firmware version '$UPGRADE_FW_REQUIRE' is required before upgrading to this version."
            return 1
        fi
    elif [ "$UPGRADE_FW_VERSION" "<" "$fw_version" ]; then
        # firmware downgrade
        if [ "$UPGRADE_FW_MAJOR" != "$fw_major" ]; then
            echo "Firmware downgrade to '$UPGRADE_FW_VERSION' is not possible!"
            echo "Downgrade is only possible among firmwares with major version '$UPGRADE_FW_MAJOR'."
            echo "Do the factory reset and try to upgrade to this version."
            return 1
        fi
    fi

    return 0
}

# Check if /etc has enough space (1MB) for logging during upgrade, and free
free_space_etc() {
    # Check if /etc has at least 1MiB free space
    local required_space=1024
    local etc_free=$(df | grep /etc | awk '{print $4}')

    # If we already have enough space, return success
    if [ "$etc_free" -ge "$required_space" ]; then
        return 0
    fi

    # Get used space for /var/log/metrics
    local metrics_used=$(du -s /var/log/metrics 2>/dev/null | awk '{print $1}')
    # Handle case where directory doesn't exist or is empty
    [ -z "$metrics_used" ] && metrics_used=0

    # Check if freeing metrics logs would give us enough space
    if [ $((etc_free + metrics_used)) -ge "$required_space" ]; then
        rm -rf /var/log/metrics/*
        return 0
    fi

    # Not enough space even after freeing logs
    return 1
}

check_image() {
    free_space_etc || {
        echo "Not enough free space in /etc for logging during upgrade."
        echo "Please free up some space and try again."
        return 1
    }
    check_fw_version
}

do_upgrade() {
    local image_file="$1"

    . /lib/functions/common.sh
    local bos_mount_image_path="${BOS_INSTALL_MOUNT_PATH}/${BOS_ROOTFS_FILE}"

    # Move the file so it will not fill up the /tmp
    mv "$image_file" "$bos_mount_image_path"

    # Make backup of stock config if backup directory do not exist. In BOS version 24.06 we introduced
    # disabling mining in stock after reboot by changing stock config. So we need to backup it for
    # future uninstallation. For backwards compatibility we can not use ENV variable for `/config` directory.
    # On latest FW we changed the location of the mount point to `/mnt/stock_config`, but we have to
    # keep the old location here, because on FW older than 24.06 the `/tmp/stock_config` directory is used
    if [ ! -d "${BOS_INSTALL_MOUNT_PATH}/config" ]; then
        cp -r -p "/tmp/${BOS_ENV_STOCK_CONFIG}/" "${BOS_INSTALL_MOUNT_PATH}/config"
    fi

    # Reboot will start `restart` action of `bos-runner.sh`
    # where the BOS unmounts and run again standard way
    # running `/nvdata/bos/bos-run.sh`. So `bos-runner.sh`, `common.sh`,
    # `bos-tools` is updated from new image. But only action `run` is
    # executed with new version, action `restart` is run with old.
    reboot
    exit 0
}
