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

pkgname="linux-samsung-i8190"
pkgver=3.0.31
pkgrel=1
pkgdesc="Samsung Galaxy SIII mini kernel fork"
arch="armhf"
_carch="arm"
_flavor="samsung-i8190"
url="https://kernel.org"
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_golden"
_commit="2662dc4f65ef3ad5eeb4222d3aad5dd418a04510"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_commit.tar.gz::https://github.com/NovaFusion/${_repository}/archive/${_commit}.tar.gz
	$_config
	compiler-gcc6.h
	00_fix_makefile.patch
	01_fix_return_address.patch
	02_timeconst_fix.patch
	03_fix_rndis.patch
	04_fix_bcma.patch
	05_change_dhd_firmware_path.patch
"
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
	
	# Modules
	cd "$builddir"
	unset LDFLAGS
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1))-postmarketOS" \
		INSTALL_MOD_PATH="$pkgdir" INSTALL_MOD_STRIP=1 \
		modules_install
}

sha512sums="318586c486d831719d678e7645c073146b6d2cfc60ac0b8b54ddc6e545723d207d46a7acd78d944e7a855e3a5181fa8ccf863d9dd0c10129a6814e9e7e70a2fa  linux-samsung-i8190-2662dc4f65ef3ad5eeb4222d3aad5dd418a04510.tar.gz
8ff2701cca482389bf60cbe29fdde6cfa3dec9a44fd82098fae7c07a0e03c5e3577c93aa988fbf2da979cc0f836f48b9c3cc5b5ab75b66dd1df4b71c91a894ed  config-samsung-i8190.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h
1b3ffbfd511af7a81590e0004bcf45b89592d6bf38cc3dd48af1e227f879426894eede34376ac708e56e84217af6ff22ee16526b3e64905ef30f0d101bbcbc29  00_fix_makefile.patch
ea1d3b5a234fa565e3c1a792de48f4fc4e6023d281d303c8e319c7ef28edc5739ab0e4dea0139a41f0a5c7d03e27921ccaa214fd0ac5c72245a094ce60128864  01_fix_return_address.patch
a2bb98fb8d988bbb659cae00fbaca360828300e9b98b90aed5ee0dd839c3f740696df4094a9021b813cbada06820d115aabed581a47cdd2c947e8d853c20b145  02_timeconst_fix.patch
6585c67450f1ef6d0c165ce4cb5c64f64d779147d1390558bf72cd40da5a33d53de6fd3c67e5ab864c14b3f67602bbe3060eea07c85b19bb82a0735024b369eb  03_fix_rndis.patch
31fe10c659d59fd086b49c20412a27cc6241e650cda1200efe86cedfc5d9ac380a3c7ff931161a40b26ab49b6e3fa04d27316efd66fba6605a5e35ef526b41e4  04_fix_bcma.patch
4d509ef6b004834cff731cfc57016efcd9d375d3d39de41a2ecb93573af781761bd498904887997824e0bb48de6a5ee4e39a8d7763adef165c5bcc3c62a897f2  05_change_dhd_firmware_path.patch"
