# Reference: <https://postmarketos.org/vendorkernel>
# Kernel config based on: arch/arm64/configs/pinenote_defconfig
maintainer="Antoine Martin (ayakael) <dev@ayakael.net>"
pkgname=linux-pine64-pinenote
pkgver=6.18.5
_kernver=${pkgver%.*}
pkgrel=0
pkgdesc="Pine64 PineNote close to mainline fork"
arch="aarch64"
_carch="arm64"
_flavor="pine64-pinenote"
url="https://kernel.org"
license="GPL-2.0-only"
options="!strip !check !tracedeps pmb:cross-native pmb:kconfigcheck-community"
makedepends="
	bash
	bc
	bison
	clang
	findutils
	flex
	lld
	llvm
	openssl-dev
	perl
	python3
	xz
"

# Source
_config="config-$_flavor.$arch"
_commit="fe4f8068cda0b9f159803cee85254b69da099057"
source="
	https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/linux-$_kernver.tar.xz
	$pkgname-$_kernver-$_commit.patch::https://ayakael.net/forge/linux-pinenote/compare/$_commit..v$_kernver.patch
	pmos-kconfig-default.patch
	$_config
"
# get patch releases
if [ "${pkgver/*.}" -ne 0 ]; then
	source="$source $pkgname-$pkgver.patch.xz::https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/patch-$pkgver.xz"
fi
builddir="$srcdir"/linux-$_kernver

prepare() {
	default_prepare
	cp "$srcdir/config-$_flavor.$CARCH" .config
}

build() {
	unset LDFLAGS
	make ARCH="$_carch" LLVM=1 \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-postmarketOS"
}

package() {
	mkdir -p "$pkgdir"/boot

	if [ -e "$builddir/arch/$_carch/boot/vmlinuz.efi" ]; then
		# ZBOOT EFI decompressor for EFI booting
		install -Dm644 "$builddir/arch/$_carch/boot/vmlinuz.efi" \
			"$pkgdir/boot/linux.efi"
	else
		echo "WARNING: CONFIG_EFI_ZBOOT not enabled!"
		install -Dm644 "$builddir/arch/$_carch/boot/Image.gz" \
			"$pkgdir/boot/vmlinuz"
	fi

	make modules_install dtbs_install \
		ARCH="$_carch" \
		LLVM=1 \
		INSTALL_PATH="$pkgdir"/boot/ \
		INSTALL_MOD_PATH="$pkgdir" \
		INSTALL_MOD_STRIP=1 \
		INSTALL_DTBS_PATH="$pkgdir"/boot/dtbs
	rm -f "$pkgdir"/lib/modules/*/build "$pkgdir"/lib/modules/*/source

	install -D "$builddir"/include/config/kernel.release \
		"$pkgdir"/usr/share/kernel/$_flavor/kernel.release
}

sha512sums="
88599ffdec96d150c1feb9b261ba93bb0301a9d0e1ad6bef7aeab1f5372cbfc57d8b43c7e902bd8f76921d1dbd8189663c142ea869e51d0e2b483b150ee00fe0  linux-6.18.tar.xz
f8443f7ff4262bb9361c9c29ef10b247367a80e7aebbae1efdf3aad545f95c0afc1e1c98f0b4d1e008086809c4ba2264b042178258dc4e1b5692714599e0cd26  linux-pine64-pinenote-6.18-fe4f8068cda0b9f159803cee85254b69da099057.patch
a9ca621c6c2ee5a1377f11c6b13959d5b80454df1fff8a7783ec13937f9c860dc77fb1c084c7394e539340b0540af30c7a689802037eaaa82bc380505548bd89  pmos-kconfig-default.patch
145267c7ce90e6bca1c48af1ed4aa2b302bd68e1e61efe0e5f28a12336eda9198e1bffe880d726c3057bbd98147d6442a9b256c60463c40928682fc2c0de1034  config-pine64-pinenote.aarch64
2c07af94135abbb1fc118be061c7b39e3b692783e67231954eabe1ccac8948a126084e651d5648aebd6d58667ac82ba41264057df1cc81fcaa161df5eee56395  linux-pine64-pinenote-6.18.5.patch.xz
"
