# Kernel config based on: arch/arm/configs/kminilte_00_defconfig

pkgname="linux-samsung-kminilte"
pkgver=3.4.113
pkgrel=0
pkgdesc="Samsung Galaxy S5 Mini kernel fork"
arch="armhf"
_carch="arm"
_flavor="samsung-kminilte"
url="https://github.com/cm-3470/android_kernel_samsung_kminilte"
license="GPL2"
options="!strip !check !tracedeps"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev"
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

# Source
_repository="android_kernel_samsung_kminilte"
_commit="7a8c010f226189116dcf2301d338555ae0d270c3"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_commit.tar.gz::https://github.com/cm-3470/${_repository}/archive/${_commit}.tar.gz
	$_config
	compiler-gcc6.h
"
builddir="$srcdir/${_repository}-${_commit}"

prepare() {
	default_prepare

	# gcc6 support
	cp -v "$srcdir/compiler-gcc6.h" "$builddir/include/linux/"

	# Remove -Werror from all makefiles
	local i
	local makefiles="$(find . -type f -name Makefile)
		$(find . -type f -name Kbuild)"
	for i in $makefiles; do
		sed -i 's/-Werror-/-W/g' "$i"
		sed -i 's/-Werror//g' "$i"
	done

	# Prepare kernel config ('yes ""' for kernels lacking olddefconfig)
	cp "$srcdir"/$_config "$builddir"/.config
	yes "" | make ARCH="$_carch" HOSTCC="$HOSTCC" oldconfig
}

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="351b043822d9fc25a002f07a490422341fd03671d661bb949085ef8d69042f1ae528f80ade7076aba3eaffec80f65c09fdbb600ffe04063c69d876c22a53709b  linux-samsung-kminilte-7a8c010f226189116dcf2301d338555ae0d270c3.tar.gz
c260d491d1c2d69f85d392613a6bcbfa920ae61b91d8c79fd6349d657e76806330e5c1ab992ec3c6835a070ef2880d2c9d81133c59df0ef5aa6713c9dc8caf17  config-samsung-kminilte.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h"
