# Reference: <https://postmarketos.org/vendorkernel>
# Kernel config based on: arch/arm/configs/arch/arm/configs/{samsung_serrano_defconfig, msm8930_serrano_eur_3g_defconfig}

pkgname="linux-samsung-serranodsdd"
pkgver=3.4.113
pkgrel=2
pkgdesc="Samsung Galaxy S4 Mini (dual sim) kernel fork"
arch="armhf"
_carch="arm"
_flavor="samsung-serranodsdd"
url="https://kernel.org"
license="GPL-2.0-only"
options="!strip !check !tracedeps"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev devicepkg-dev xz gcc6"

# Compiler: GCC 6 (doesn't boot when compiled with newer versions)
if [ "${CC:0:5}" != "gcc6-" ]; then
	CC="gcc6-$CC"
	HOSTCC="gcc6-gcc"
	CROSS_COMPILE="gcc6-$CROSS_COMPILE"
fi


# Source
_repository="android_kernel_samsung_msm8930-common"
_commit="0c7b2079e9ba8f945ea444bad393c8d7f8b099cc"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_commit.tar.gz::https://github.com/LineageOS/${_repository}/archive/${_commit}.tar.gz
	$_config
	01_msm-fix-perf_trace_counters.patch
	02_gpu-msm-fix-gcc5-compile.patch
"
builddir="$srcdir/${_repository}-${_commit}"

prepare() {
	default_prepare
	downstreamkernel_prepare "$srcdir" "$builddir" "$_config" "$_carch" "$HOSTCC"
}

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

package() {
	# kernel.release
	install -D "$builddir/include/config/kernel.release" \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"

	# zImage (find the right one)
	cd "$builddir/arch/$_carch/boot"
	_target="$pkgdir/boot/vmlinuz-$_flavor"
	for _zimg in zImage-dtb Image.gz-dtb *zImage Image; do
		[ -e "$_zimg" ] || continue
		msg "zImage found: $_zimg"
		install -Dm644 "$_zimg" "$_target"
		break
	done
	if ! [ -e "$_target" ]; then
		error "Could not find zImage in $PWD!"
		return 1
	fi
}

sha512sums="a89f9da68be6cf3b0bc77a9d70519b4ffc4ad332a53bfb35facf0e8ae418b7f1870fc1596c9305c596f7a0353be82f7eff0e211a32e330d0dd20c98398ab2826  linux-samsung-serranodsdd-0c7b2079e9ba8f945ea444bad393c8d7f8b099cc.tar.gz
0408b51df61a13d6b1740c6db3cbefce1e09bb61547ef07cc1db5c343551dc9105766a564f8ed57840d118fb4279309045e7fbbef7f6669e92ff8dcd53d47388  config-samsung-serranodsdd.armhf
f85ced49ee2f2461adea68dac0a10452aa809a7d41d869092d94eb085344919a9a385dec0c48011f4601ca4f441700f7fe49075c7eca4fb2d66b01f7d413ccb7  01_msm-fix-perf_trace_counters.patch
7be03a9e78b7ac330a54b1f00509caa0621a95c0c55901878ad757f9dd69cc05ba2c8b5ea987063ae1224f92c4d090d515fa5d369e7755181a4871b0d0f82881  02_gpu-msm-fix-gcc5-compile.patch"
