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

pkgname="linux-wiko-lenny3"
pkgver=3.18.19
pkgrel=0
pkgdesc="Wiko Lenny 3 Kernel"
arch="armhf"
_carch="arm"
_flavor="wiko-lenny3"
url="https://wikomobile.com"
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
_download_url="http://www.wikogeek.com/index.php?tel="
_download_id="65b9eea6e1cc6bb9f0cd2a47751a186f"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_download_id.zip::${_download_url}${_download_id}
	$_config
	compiler-gcc6.h
	00_disable_drvgen.patch
	01_add_generated_cust_dtsi.patch
	02_fix_various_include_directives.patch
"
builddir="$srcdir/LENNY3_OpenSource/kernel-3.18"

prepare() {
	default_prepare

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

	# Remove -Werror from all makefiles
	find . -type f -name Makefile -print0 | \
		xargs -0 sed -i 's/-Werror-/-W/g'
	find . -type f -name Makefile -print0 | \
		xargs -0 sed -i 's/-Werror//g'

	# 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="83c56f0c8569b7ca3cd29ebb5769ebcc1bb457c9eefeacfa1ad3620436693fc7cbaeda93c7a2157ab92f463b0b1431a3f3a79e14a783000199dea1cc5b2d3e77  linux-wiko-lenny3-65b9eea6e1cc6bb9f0cd2a47751a186f.zip
a1fea5c1e3cc7341b9426e1475d694aaf98f3083396a456eb90185bca43ee98b986771327fe621823d6935be19ca5aafcec932de662850843de55e6e235c6481  config-wiko-lenny3.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h
8a04940e6f5424df67a3cc4c75dd19be1c9b560859e9db0ae67b766d52982a46156b601698afb80f90b9b99f63ef74e1f6147902b43ec97c25d2b75ba2f8d2b3  00_disable_drvgen.patch
adda36d779a79b2f840361a7df91028fdb86dd9aa7d2b431b8bd1fc674175d6c5bd098f7a6765cc4e3f7b3428ff7d35d9f5bac31cb7602ea270a6affe843a2e9  01_add_generated_cust_dtsi.patch
42a7ef954d517f945bdbca16d92aff63b6b07260d666119dc96c33db577231f907c7968d9466bf163c397a37f4fb23a6886047b1eb821aa14687d43442c8e615  02_fix_various_include_directives.patch"
