# Kernel config based on defconfig and exynos7870.config
pkgname=linux-postmarketos-exynos7870
pkgver=6.8.1
pkgrel=2
pkgdesc="Close-to-mainline kernel for Samsung Exynos 7870 devices"
arch="aarch64"
_carch="arm64"
_flavor="postmarketos-exynos7870"
url="https://gitlab.com/exynos7870-mainline/linux"
license="GPL-2.0-only"
options="!strip !check !tracedeps pmb:cross-native"
makedepends="
	bash
	bc
	bison
	flex
	openssl-dev
	perl
	postmarketos-installkernel
"

_owner="exynos7870-mainline"
_repo="mainline-patches"
_commit="6608cbe5d3b29c44a177d12c14c7f1b070584363"
_config="config-$_flavor.aarch64"

# Source
source="
	$_config
	linux-$pkgver.tar.gz::https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-$pkgver.tar.gz
	patches-$_commit.tar.gz::https://gitlab.com/$_owner/$_repo/-/archive/$_commit/$_repo-$_commit.tar.gz
"
builddir="$srcdir/linux-$pkgver"

prepare() {
	default_prepare
	cp "$srcdir/$_config" .config

	# Apply the patches to the mainline kernel.
	for i in "$srcdir"/$_repo-*/patches/*; do
		patch -p1 < "$i"
	done

	# Delete the minimal devicetrees
	rm -f "$srcdir"/$_repo-*/*-minimal-dts.dts

	# Move the devicetrees in the kernel tree
	for i in "$srcdir"/$_repo-*/*dts*; do
		cp $i "arch/arm64/boot/dts/exynos/"
	done

	# Add the devicetrees in the Makefile
	for i in "$srcdir"/$_repo-*/*.dts; do
		# Get the file name, and change .dts to .dtb
		i="$(echo $(basename $i) | sed 's/.$/b/')"
		echo "dtb-\$(CONFIG_ARCH_EXYNOS) += $i" >> \
			"$builddir/arch/arm64/boot/dts/exynos/Makefile"
	done
}

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

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

	# The bootloader does not support zipped kernels
	make install modules_install dtbs_install \
		ARCH="$_carch" \
		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"

	install -Dm644 "$builddir/arch/$_carch/boot/Image" \
		"$pkgdir/boot/vmlinuz"
}

sha512sums="
eb4426424d2d8e1b00dd0606f3ad78d0ba3791e92cba0eeff8df925eab254464ca70c63fe08a0fa293cf2bda58f1c440f25296f375f4e76fa9a828b01057be7e  config-postmarketos-exynos7870.aarch64
a08c966b4c3cce22fa4963b2b17b42966ceb959ccb0280f83a67fa84abed773ec5e155df7678df121c7825772a9ba82df71fa142734754d3847e73b7e65488f7  linux-6.8.1.tar.gz
275b3a8be2e089348ed18d12ff07118bf66574be46aeb0004950f8a46e46609e5312a2e792ab85a9a8eef0ace9fec7eb4066f839f76efd688e0ee5e09623cdc6  patches-6608cbe5d3b29c44a177d12c14c7f1b070584363.tar.gz
"
