# APKBUILD based on linux-vanilla aport. Changes:
# - disabled module installation
# - add !check !tracedeps
# - package: just install zimage and kernel.release, because the kernel config
#	does not generate modules or dtb files
# - do not create -dev subpackage (makes no sense without module support)
#
# Kernel config based on: arch/arm/configs/lineageos_i9305_defconfig
# Changes:
# - enable devtmpfs (needed for udev -> touch support in weston)
# - adding xz to makedepends because of lzma: unrecognized option: 9 (https://wiki.postmarketos.org/wiki/Troubleshooting:kernel)
# - add kernel modules installation
# - disable ANDROID_PARANOID_NETWORK (removes network restrictions)

_vendor=samsung
_flavor=samsung-i9305
_hash="fa8fa332fdec11744ffe0c742775cc22efe0d041"
_config="config-${_flavor}.armhf"

pkgname=linux-${_flavor}
pkgver=3.0.101
case $pkgver in
	*.*.*)  _kernver=${pkgver%.*};;
	*.*) _kernver=$pkgver;;
esac
pkgrel=4
arch="armhf"
pkgdesc="Samsung Galaxy SIII LTE kernel from LineageOS"
url="https://github.com/LineageOS/android_kernel_samsung_smdk4412"
depends=""
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev xz busybox-static-armhf"
options="!strip !check !tracedeps"
install=
source="
	$pkgname-$_hash.tar.gz::https://github.com/LineageOS/android_kernel_${_vendor}_smdk4412/archive/${_hash}.tar.gz
	$_config
	compiler-gcc6.h
"
subpackages=""
license="GPL2"

_abi_release=${pkgver}
_carch="arm"
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

ksrcdir="$srcdir/android_kernel_${_vendor}_smdk4412-${_hash}"

prepare() {
	local _patch_failed=
	cd "$ksrcdir"

	# first apply patches in specified order
	for i in $source; do
		case $i in
		*.patch)
			msg "Applying $i..."
			if ! patch -s -p1 -N -i "$srcdir"/$i; then
				echo $i >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

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

	mkdir -p "$srcdir"/build
	cp "$srcdir"/$_config "$srcdir"/build/.config
	make -C "$ksrcdir" O="$srcdir"/build ARCH="$_carch" HOSTCC="$HOSTCC" \
		silentoldconfig
}

build() {
	cd "$srcdir"/build
	unset LDFLAGS

	echo "-- [ Build kernel ] --"
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1))-Alpine"

	echo "-- [ Build modules ] --"
	# the 'no-pic' flag is necessary to avoid the 
	# error 'Unknown symbol _GLOBAL_OFFSET_TABLE_ (err 0)' when loading the module
	make ARCH="$_carch" CC="${CC:-gcc}" CFLAGS_MODULE="-fno-pic" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1))-Alpine" CONFIG_NO_ERROR_ON_MISMATCH=y \
		modules
}

package() {
	echo "--[ Installing kernel ]--"
	install -Dm644 "$srcdir/build/arch/arm/boot/zImage" \
		"$pkgdir/boot/vmlinuz-$_flavor"

	install -D "$srcdir/build/include/config/kernel.release" \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"

	
	cd "$srcdir"/build
	unset LDFLAGS

	echo "--[ Installing modules ]--"
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1))-Alpine" CONFIG_NO_ERROR_ON_MISMATCH=y \
		INSTALL_MOD_PATH="$pkgdir" modules_install
}

sha512sums="d87dd54152980b30012fead62e639dde6ff0862d376790eac32800aeffd91482a220c7b1f233933a1879796dcaa3722e86c42a5f0e4df4168ae1f65362952779  linux-samsung-i9305-fa8fa332fdec11744ffe0c742775cc22efe0d041.tar.gz
168d3f4839332f31b7cedf18f274b0a691af19e76f4d9cebba02a060c3908dc73f5a3d3a7cfa39f49a6a5aaf29440bfab258de746a5c3981e27195e7baea0c6c  config-samsung-i9305.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h"
