abinfo "Preparing build environment ..."
SRCARCH=$(bash "$SRCDIR"/autobuild/build-common/get-kernel-arch "$ARCH")
if (( $? )); then
	echo "Unable to map ab arch name to kernel arch"
	abdie
fi
export SRCARCH

wrap_make() {
	env --unset ARCH make "$@"
}

# ANNOTATIONS_SUFFIX: Suffix for the annotations file (mainline, retro - these
# follow vastly different configuration methodologies, the former enables all
# that the Kconfig could allow, while the latter only enables what makes sense
# for the target architectures and attempts to scour RAM).
#
# KERNEL_VARIANTS: Variant of the kernels to build.
if ab_match_archgroup mainline; then
	ANNOTATIONS_SUFFIX="mainline"
elif ab_match_archgroup retro; then
	ANNOTATIONS_SUFFIX="retro"
fi
ANNOTATIONS_FILE="${SRCDIR}/autobuild/annotations.${ANNOTATIONS_SUFFIX}"

FLAVOUR_LINE="$(grep -oP '^#\s*FLAVOUR:\s*\K.*' "$ANNOTATIONS_FILE")"
KERNEL_VARIANTS=()
if [[ -n "$FLAVOUR_LINE" ]]; then
	while read -r -d' ' flavour; do
		if [[ "$flavour" == "$ARCH"* ]]; then
			KERNEL_VARIANTS+=("$flavour")
		fi
	done <<< "$FLAVOUR_LINE"
else
	abdie "Cannot find the line defining flavours in the annotations file: $ANNOTATIONS_FILE !"
fi

# LOCALNAME: The string configured in CONFIG_LOCALVERSION
# PACKAGE_LOCALNAME: LOCALVERSION with -aosc and variant stripped, e.g.
#   -aosc-main-4k -> -main, -aosc-lts -> -lts
for config in "${KERNEL_VARIANTS[@]}"; do
	abinfo "Installing / adapting kernel configuration ..."
	annotations \
		--arch "$ARCH" \
		--flavour "$config" \
		--export \
		--file "$ANNOTATIONS_FILE" \
		> "$SRCDIR"/.config

	wrap_make ${MAKE_AFTER} olddefconfig

	abinfo "Setting up variables ..."
	. "$SRCDIR"/autobuild/build-common/set-var

	case "${LOCALNAME}" in
	*vanilla* )
		abinfo "Skipping config change detection for vanilla kernel build"
		;;
	* )
		abinfo "Sanity check: kernel config did not change"
		annotations \
			--arch "$ARCH" \
			--flavour "$config" \
			--check "$SRCDIR"/.config \
			--file "$ANNOTATIONS_FILE"
		;;
	esac

	if [ "${LOCALNAME}" != "-aosc${config/$ARCH/}" ] ; then
		aberr "Invalid kernel LOCALVERSION: '$LOCALNAME'"
		aberr "Expecting: -aosc${config/$ARCH/}"
		abdie "Please check the value of CONFIG_LOCALVERSION as noted in ${ANNOTATIONS_FILE}."
	fi

	abinfo "Kernel variant: $KERNEL_VARIANT"
	abinfo "Kernel version (uname -r): '${version}${LOCALNAME}'"

	PACKAGE_LOCALNAME=${LOCALNAME##-aosc}
	# If there's only one component (the kernel variant), don't strip any further.
	if [ ! -z "${PACKAGE_LOCALNAME%-*}" ] ; then
		PACKAGE_LOCALNAME=${PACKAGE_LOCALNAME%-*}
	fi

	abinfo "Sanity check: version and local name should match package name"
	if [[ "-aosc${config/$ARCH/}" =~ -aosc-main* ]]; then
		EXPECTED_PKGNAME="linux-kernel-$version"
	else
		if [[ "${ARCH}" = "loongarch64_nosimd" ]]; then
			# Note: No multi-versioning.
			EXPECTED_PKGNAME="linux-kernel${PACKAGE_LOCALNAME}"
		else
			EXPECTED_PKGNAME="linux-kernel${PACKAGE_LOCALNAME}-$version"
		fi
	fi
	if [[ "$PKGNAME" != "$EXPECTED_PKGNAME" ]]; then
		aberr "Building version $EXPECTED_PKGNAME but PKGNAME is $PKGNAME! Fix your defines/config!"
		abdie
	fi
	unset EXPECTED_PKGNAME

	# Note: objtool currently supports amd64, loongarch64, ppc64el.
	if [[ ( "${ARCH}" == "amd64" && -f "${SRCDIR}/tools/objtool/arch/x86/Build") || \
	      ( "${ARCH}" == "loongarch64" && -f "${SRCDIR}/tools/objtool/arch/loongarch/Build") || \
	      ( "${ARCH}" == "ppc64el" && -f "${SRCDIR}/tools/objtool/arch/powerpc/Build") ]]; then
		abinfo "Building objtool ..."
		wrap_make \
			-C "$SRCDIR"/tools/objtool \
			${MAKE_AFTER} LIBOPCODES_LIBS=''

		abinfo "Installing objtool ..."
		install -Dvm755 "$SRCDIR"/tools/objtool/objtool \
			"$PKGDIR"/usr/src/linux-headers-${version}${LOCALNAME}/tools/objtool/objtool
	fi

	abinfo "Building kernel image, modules, and misc files ..."
	wrap_make ${MAKE_AFTER}

	if [[ "${ARCH}" = "loongarch64_nosimd" ]]; then
		abinfo "Building U-Boot image ..."
		make uImage
	fi

	abinfo "Stripping Kernel modules ..."
	. "${SRCDIR}/autobuild/build-common/strip-modules"
	if (( $? )); then
		abdie
	fi

	abinfo "Installing Kernel modules ..."
	mkdir -pv "$PKGDIR"/usr
	wrap_make modules_install \
		INSTALL_MOD_PATH="$PKGDIR"/usr ${MAKE_AFTER}

	abinfo "Installing Kernel image ..."
	if [[ "${ARCH}" = "amd64" || \
	      "${ARCH}" = "i486" ]]; then
		install -Dvm644 "$SRCDIR"/arch/x86/boot/bzImage \
			"$PKGDIR"/boot/vmlinuz-$version$LOCALNAME
	elif [[ "${ARCH}" = "arm64" ]]; then
		install -Dvm644 "$SRCDIR"/arch/arm64/boot/Image \
			"$PKGDIR"/usr/lib/aosc-os-arm64-boot/linux-kernel-$version$LOCALNAME/Image
		mkdir -pv "$PKGDIR"/boot
		for i in `find "$SRCDIR"/arch/arm64/boot/dts/ -name '*.dtb'`; do
			install -Dvm644 $i -t \
				"$PKGDIR"/usr/lib/aosc-os-arm64-boot/dtbs-kernel-$version$LOCALNAME/"$(basename $(dirname $i))"
		done
		install -Dvm644 "$SRCDIR"/arch/arm64/boot/Image \
			"$PKGDIR"/boot/vmlinux-$version$LOCALNAME
	elif [[ "${ARCH}" = "riscv64" ]]; then
		install -Dvm644 "$SRCDIR"/arch/riscv/boot/Image \
			"$PKGDIR"/usr/lib/aosc-os-riscv64-boot/linux-kernel-$version$LOCALNAME/Image
		mkdir -pv "$PKGDIR"/boot
		for i in `find "$SRCDIR"/arch/riscv/boot/dts/ -name '*.dtb'`; do
			install -Dvm644 $i -t \
				"$PKGDIR"/usr/lib/aosc-os-riscv64-boot/dtbs-kernel-$version$LOCALNAME/"$(basename $(dirname $i))"
		done
		install -Dvm644 "$SRCDIR"/arch/riscv/boot/Image \
			"$PKGDIR"/boot/vmlinux-$version$LOCALNAME
	elif [[ "${ARCH}" = "loongarch64" ]]; then
		install -Dvm644 "$SRCDIR"/arch/loongarch/boot/vmlinuz.efi \
			"$PKGDIR"/boot/vmlinuz-$version$LOCALNAME
	elif [[ "${ARCH}" = "loongarch64_nosimd" ]]; then
		install -Dvm644 "$SRCDIR"/arch/loongarch/boot/uImage.bin \
			"$PKGDIR"/boot/uImage
	elif [[ "${ARCH}" = "loongson3" ]]; then
		install -Dvm644 "$SRCDIR"/vmlinuz \
			"$PKGDIR"/boot/vmlinuz-$version$LOCALNAME
	elif [[ "${ARCH}" = "ppc64el" ]]; then
		install -Dvm644 "$SRCDIR"/vmlinux \
			"$PKGDIR"/boot/vmlinux-$version$LOCALNAME
	elif [[ "${ARCH}" = "alpha" ]]; then
		install -Dvm644 "$SRCDIR"/arch/alpha/boot/vmlinux.gz \
			"$PKGDIR"/boot/vmlinuz-$version$LOCALNAME
	fi

	abinfo "Installing kernel headers and objects ..."
	env LOCALNAME=${LOCALNAME} version=${version} ARCH=$ARCH \
		bash "$SRCDIR"/autobuild/build-common/install-headers

	if [ -f "$SRCDIR"/System.map ]; then
		abinfo "Copying System.map"
		install -Dvm644 "$SRCDIR"/System.map \
			"$PKGDIR"/usr/lib/modules/${version}${LOCALNAME}/System.map
	else
		abwarn "No System.map found"
	fi

	abinfo "Generating postinst/prerm scripts ..."
	. "$SRCDIR"/autobuild/build-common/gen-scripts
done

abinfo "Removing firmwares ..."
rm -rfv "$PKGDIR"/usr/lib/firmware

# vim: set noexpandtab sts=4 ts=4 sw=4 syntax=bash :
