UPGRADE_FW_MAJOR="2022-09-13-0-11012d53-22.08-plus"
UPGRADE_FW_VERSION="2025-11-21-0-eb658dcd-25.11-plus"
UPGRADE_FW_REQUIRE="2022-09-13-0-11012d53-22.08-plus"

UPGRADE_FW_VERSION_SUFFIX=${UPGRADE_FW_VERSION#*-*-*-*-*-}
UPGRADE_FW_VERSION_PLUS=""

case $UPGRADE_FW_VERSION_SUFFIX in *plus*) UPGRADE_FW_VERSION_PLUS="+";; esac

CONF_DIR_PATH="/tmp/sysupgrade_cfg"
CONF_TGZ_PATH="/tmp/sysupgrade.tgz"

BOSMINER_CONF="/etc/bosminer.toml"

BOSMINER_CONF_DST="/tmp/bosminer.toml"
CONFIG_GENERATOR="sysupgrade_script"

get_bos_mode() {
	local bos_mode=$(cat "/etc/bos_mode" 2>/dev/null || cat "/tmp/bos_mode" 2>/dev/null)
	echo "${bos_mode:-nand}"
}

current_fw_version() {
	if ! cat /etc/bos_version 2>/dev/null; then
		awk '/Package: /{p=$2} /Version: /{v=$2} /Status: /{if (p == "firmware" && $NF == "installed") print v}' \
			'/usr/lib/opkg/status'
	fi
}

current_fw_major() {
	cat /etc/bos_major 2>/dev/null || echo unknown
}

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

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

	echo "board  : $board_name"
	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
}

# Keep this function only for s9 sysupgarde for new miners should not be used
# because it uses space on /tmp that may not be available
modify_configs() {
	local bos_mode=$(get_bos_mode)
	# extract configuration files from tarball
	mkdir -p "$CONF_DIR_PATH"
	tar -xzf "$CONF_TGZ_PATH" -C "$CONF_DIR_PATH"

	# NOTE (2022-08-15):
	# This is a temporary hotfix to avoid reset of hwid during sysupgrade for SD.
	# The real fix is done in Makefile of bos_sd package, but it solves only further
	# updates. To avoid change of hwid during the nearest update, we need this.
	# Can be safely clean up after the next major release.
	if [ "${bos_mode}" == "sd" ]; then
		if [ ! -f "${CONF_DIR_PATH}/etc/miner_hwid" ]; then
			cat "/tmp/miner_hwid" > "${CONF_DIR_PATH}/etc/miner_hwid"
		fi
	fi

	echo "$CONF_DIR_PATH"
	return 0
}

finish_configs_modification() {
	if [ "$1" == "store" ]; then
		tar -czf "$CONF_TGZ_PATH" -C "$CONF_DIR_PATH" $(ls -A "$CONF_DIR_PATH")
	fi

	# cleanup system
	rm -rf "$CONF_DIR_PATH"

	return 0
}

# For upgrade we need some minimal space in /tmp for scripts and fpga file
# 1MB should be more than enough, because fpga file is around 260KB
check_tmp_space() {
	local min_space=1024  # 1MB in KB
	local available=$(df -k /tmp | awk 'NR==2 {print $4}')
	
	if [ "$available" -lt "$min_space" ]; then
		echo "Error: Less than 1MB available in /tmp ($available KB free)" >&2
		return 1
	fi
	return 0
}
check_image() {
	check_tmp_space || return 1
	check_fw_version $1 && {
		return 0
	}
}

pre_upgrade() {
	local board_name=$1
	local tar_file=$2

	return
}
