#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2013-2023 Igor Pecovnik, igor@armbian.com
#
# This file is a part of the Armbian Build Framework
# https://github.com/armbian/build/
#

# this is sourced before any other arch specific config file, always. see main-config.sh

# Source the mainline kernel hooks, in a separate file for easier maintenance.

# shellcheck source=/dev/null
source "${SRC}/config/sources/mainline-kernel.conf.sh"

declare -g FAST_CREATE_IMAGE='yes'
declare -g MAIN_CMDLINE='rw no_console_suspend consoleblank=0 fsck.fix=yes fsck.repair=yes net.ifnames=0 splash plymouth.ignore-serial-consoles'

# So the idea is to let the board/family/hooks define these variables.
# If the family does NOT define them, assume sane defaults.
function late_family_config__common_defaults_for_mainline_kernel() {
	# if KERNELSOURCE is 'none', don't do anything; config is explicitly asking to skip kernel compilation.
	if [[ "${KERNELSOURCE}" == "none" ]]; then
		display_alert "KERNELSOURCE is set to 'none'; will skip kernel compilation" "common_defaults_for_mainline" "debug"
		return
	fi

	declare problems=0

	# Setting KERNELBRANCH and not KERNEL_MAJOR_MINOR: legacy code, that didn't receive KERNEL_MAJOR_MINOR yet.
	if [[ -n ${KERNELBRANCH} && -z ${KERNEL_MAJOR_MINOR} ]]; then
		display_alert "KERNELBRANCH ('${KERNELBRANCH}') is set, but KERNEL_MAJOR_MINOR is unset" "please make sure they match, or remove KERNELBRANCH and trust the system" "warn"
		((problems++))
	fi

	# The good-behaved mainline will hit this, we will auto-determine.
	if [[ -z ${KERNELBRANCH} && -n ${KERNEL_MAJOR_MINOR} ]]; then
		display_alert "KERNEL_MAJOR_MINOR ('${KERNEL_MAJOR_MINOR}') is set and KERNELBRANCH is unset" "hopefully mainline" "debug"
	fi

	# If KERNELSOURCE is set (NOT mainline), then so should KERNELBRANCH, KERNEL_MAJOR_MINOR be set. Check and warn if not.
	# Attention: having KERNELSOURCE set here usually means it is a fork, vendor kernel, or other non-strict mainline kernel.
	if [[ -n ${KERNELSOURCE} ]]; then
		if [[ -z ${KERNEL_MAJOR_MINOR} ]]; then
			display_alert "KERNELSOURCE is set, but KERNEL_MAJOR_MINOR is unset" "please make sure they match" "warn"
			((problems++))
		fi
		if [[ -z ${KERNELBRANCH} ]]; then
			display_alert "KERNELSOURCE is set, but KERNELBRANCH is unset" "please make sure they match" "warn"
			((problems++))
		fi
	fi

	# KERNEL_MAJOR_MINOR is the key to all this stuff. It must be set.
	if [[ -z ${KERNEL_MAJOR_MINOR} ]]; then
		display_alert "KERNEL_MAJOR_MINOR is unset" "common_defaults_for_mainline" "warn"
		((problems++))
	fi

	# exit with error if some breaking problem detected above.
	if ((problems > 0)); then
		exit_with_error "Please fix the above kernel configuration problems (check warnings above)."
	fi

	# Small debuggings to help when everything else fails and all sanity is gone.
	if [[ -z ${KERNELSOURCE} ]]; then
		display_alert "KERNELSOURCE is unset" "common_defaults_for_mainline" "debug"
	else
		display_alert "KERNELSOURCE is set by board/family/hooks to '${KERNELSOURCE}'" "common_defaults_for_mainline" "debug"
	fi

	if [[ -z ${KERNELBRANCH} ]]; then
		display_alert "KERNELBRANCH is unset" "common_defaults_for_mainline" "debug"
	else
		display_alert "KERNELBRANCH is set by board/family/hooks to '${KERNELBRANCH}'" "common_defaults_for_mainline" "debug"
	fi

	if [[ -z ${LINUXFAMILY} ]]; then
		display_alert "LINUXFAMILY is unset" "common_defaults_for_mainline" "debug"
	else
		display_alert "LINUXFAMILY is set by board/family/hooks to '${LINUXFAMILY}'" "common_defaults_for_mainline" "debug"
	fi

	if [[ -z ${KERNELPATCHDIR} ]]; then
		display_alert "KERNELPATCHDIR is unset" "common_defaults_for_mainline" "debug"
	else
		display_alert "KERNELPATCHDIR is set by board/family/hooks to '${KERNELPATCHDIR}'" "common_defaults_for_mainline" "debug"
	fi

	if [[ -z ${KERNEL_PATCH_ARCHIVE_BASE} ]]; then # @TODO: this is a bad idea, remove
		display_alert "KERNEL_PATCH_ARCHIVE_BASE is unset" "common_defaults_for_mainline" "debug"
	else
		display_alert "KERNEL_PATCH_ARCHIVE_BASE is set by board/family/hooks to '${KERNELPATCHDIR}'" "common_defaults_for_mainline" "debug"
	fi

	if [[ -z ${LINUXCONFIG} ]]; then
		display_alert "LINUXCONFIG is unset" "common_defaults_for_mainline" "debug"
	else
		display_alert "LINUXCONFIG is set by board/family/hooks to '${LINUXCONFIG}'" "common_defaults_for_mainline" "debug"
	fi

	# If LINUFAMILY is unset... bomb
	if [[ -z ${LINUXFAMILY} ]]; then
		exit_with_error "LINUXFAMILY is unset after family config; KERNELSOURCE=${KERNELSOURCE}"
	fi

	# Generic defaultings, used for all kernels (including vendor/legacy/3rd-party and mainline).
	# If KERNEL_PATCH_ARCHIVE_BASE is unset, set it to LINUXFAMILY.
	if [[ -z ${KERNEL_PATCH_ARCHIVE_BASE} ]]; then # @TODO: this is a bad idea, remove
		display_alert "KERNEL_PATCH_ARCHIVE_BASE is unset; using LINUXFAMILY: '${LINUXFAMILY}'" "common_defaults_for_mainline" "debug"
		KERNEL_PATCH_ARCHIVE_BASE="$LINUXFAMILY"
	fi

	# If KERNELPATCHDIR is unset, set it to "archive/${KERNELPATCHDIR}-${KERNEL_MAJOR_MINOR}" # @TODO: no, use LINUXFAMILY directly
	if [[ -z ${KERNELPATCHDIR} ]]; then
		display_alert "KERNELPATCHDIR is unset; using 'archive/${KERNEL_PATCH_ARCHIVE_BASE}-${KERNEL_MAJOR_MINOR}'" "common_defaults_for_mainline" "info"
		KERNELPATCHDIR="archive/${KERNEL_PATCH_ARCHIVE_BASE}-${KERNEL_MAJOR_MINOR}" # previously was KERNELPATCHDIR="$LINUXFAMILY-$BRANCH"
	fi

	# if LINUXCONFIG is unset... default to linux-${LINUXFAMILY}-${BRANCH} # Attention: no KERNEL_MAJOR_MINOR here, so manual file rollover is necessary
	if [[ -z ${LINUXCONFIG} ]]; then
		display_alert "LINUXCONFIG is unset; using 'linux-${LINUXFAMILY}-${BRANCH}'" "common_defaults_for_mainline" "debug"
		LINUXCONFIG="linux-${LINUXFAMILY}-${BRANCH}"
	fi

	# Mainline defaults

	if [[ -z ${KERNELSOURCE} ]]; then
		display_alert "Using mainline kernel defaults" "common_defaults_for_mainline: KERNEL_MAJOR_MINOR: ${KERNEL_MAJOR_MINOR}" "debug"

		# this "manifests"/resolves the mirrors; any changes after this will need to resolve their own mirrors; later hooks won't be subject to mirroring
		KERNELSOURCE="${MAINLINE_KERNEL_SOURCE}"

		# If the branch is unset, auto-determine it based on KERNEL_MAJOR_MINOR; allow hooks to customize
		if [[ -z ${KERNELBRANCH} ]]; then
			declare original_kernelsource="${KERNELSOURCE}" # store for later comparing
			call_extension_method "mainline_kernel_decide_version" <<- 'MAINLINE_KERNEL_DECIDE_VERSION'
				*determine KERNELBRANCH, based on family / KERNEL_MAJOR_MINOR / etc*
				This hook is run when when we know the family uses mainline (does NOT set KERNELSOURCE),
				but the family/board/hooks haven't determined a KERNELBRANCH (tag/branch/commit reference) yet.
				All hooks are called in the usual order, the last one setting it "wins".
				It is expected that hooks here are able to:
				- if kernel in RC period, convert say 6.7 to tag:v6.7-rc7
				- "lock" certain families (or all families) to a certain tag in a branch; say convert 6.6 to tag:v6.6.6
				- change KERNELSOURCE to adapt to too-new -rc releases that haven't shown up in stable kernel git tree yet
			MAINLINE_KERNEL_DECIDE_VERSION

			display_alert "Mainline hooks determined" "new KERNELBRANCH='${KERNELBRANCH}'" "debug"

			# if original_kernelsource does not match the current KERNELSOURCE, log it
			if [[ "${original_kernelsource}" != "${KERNELSOURCE}" ]]; then
				display_alert "Mainline hooks changed source" "new KERNELSOURCE='${KERNELSOURCE}'" "debug"
			fi
			unset original_kernelsource
		fi

	fi

}

function late_family_config__common_defaults_for_mainline_uboot() {
	if [[ -z ${BOOTDIR} ]]; then
		display_alert "BOOTDIR is unset" "common_defaults_for_mainline" "debug"
		BOOTDIR="$MAINLINE_UBOOT_DIR"
	else
		display_alert "BOOTDIR is set by board/family/hooks to '${BOOTDIR}'" "common_defaults_for_mainline" "debug"
	fi

	if [[ -z ${BOOTSOURCE} ]]; then
		display_alert "BOOTSOURCE is unset" "common_defaults_for_mainline" "debug"
		BOOTSOURCE="$MAINLINE_UBOOT_SOURCE"
	else
		display_alert "BOOTSOURCE is set by board/family/hooks to '${BOOTSOURCE}'" "common_defaults_for_mainline" "debug"
	fi

	if [[ -z ${BOOTBRANCH} ]]; then
		display_alert "BOOTBRANCH is unset; BOOTBRANCH_BOARD=${BOOTBRANCH_BOARD} or tag:v2022.07" "common_defaults_for_mainline" "debug"
		BOOTBRANCH="${BOOTBRANCH_BOARD:-"tag:v2022.07"}" # @TODO: move out of here and into some hook
	else
		display_alert "BOOTBRANCH is set by board/family/hooks to '${BOOTBRANCH}'" "common_defaults_for_mainline" "debug"
	fi
}

true # make sure to exit with 0 status; this protects against shortcircuits etc above.
