# Based on linux-lg-mako. Changes:
# - add findutils makedepend (necessary for the initramfs)
# - it builds an initramfs - do not use ramdisk.cpio and
#	ramdisk-recovery-device.cpio, in there, always use the
#	initramfs from the recovery partition ("isorec")
# - directly boot to that initramfs, not only when the recovery
#	key combination was pressed.
# - use Alpine's busybox-static instead of the pre-compiled
#	binary, that comes with the source
#
# Kernel config changes, based on: arch/arm/configs/i9100_defconfig
# - enable devtmpfs (needed for udev -> touch support in weston)
# - change compression from CONFIG_KERNEL_LZMA=y to
#	CONFIG_KERNEL_GZIP=y (so it works with busybox)

# This must be built in the target chroot, so busybox.static can
# be used.
_pmb_build_in_native_chroot="false"

_vendor=samsung
_flavor=samsung-i9100
_hash="349a3e91e76d17e67ef6213e1f6712e700695631"
_config="config-${_flavor}.armhf"

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

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

ksrcdir="$srcdir/android_kernel_samsung_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

	# Use Alpine's busybox.static instead of the pre-compiled busybox shipped
	# with the source
	cp -v /bin/busybox.static "$ksrcdir"/usr/galaxys2_initramfs_files/busybox

	# do not use ramdisk.cpio and ramdisk-recovery-device.cpio, always use
	# the initramfs from the recovery partition ("isorec"), so we can build
	# it later and independently from the kernel. also directly boot that
	# partition, not only when the recovery key combination was used.
	cd "$ksrcdir"/usr/
	mv galaxys2_initramfs.list galaxys2_initramfs.list_old
	grep -v "../../ramdisk" galaxys2_initramfs.list_old > galaxys2_initramfs.list \
		|| return 1
	cp -v "$srcdir"/init "$ksrcdir"/usr/galaxys2_initramfs_files/init || return 1

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

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



# this is so we can do: 'abuild menuconfig' to reconfigure kernel
menuconfig() {
	cd "$srcdir"/build || return 1
	make ARCH="$_carch" menuconfig
	cp .config "$startdir"/$_config
}

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

	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine" \
		|| return 1
}

package() {
	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"
}

sha512sums="28198b1312d66a1af28e112f3252aa9fbbbee8bf373776c556cdbc1c75517072a1eec6522717df0b2bc4f6b5f06b9ce07d2cb29a2de07fc56c4ea072f4d46c82  linux-samsung-i9100-349a3e91e76d17e67ef6213e1f6712e700695631.zip
7ce82e81ca8d472f01f8097e3a434fe0130d66b16b29ed3d4c5ce94870fb4efe3b5e79523802b078fcdc737e5a1ee6351bc8427d1271a098eab8e9f35b95e6b1  config-samsung-i9100.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h
dd4094d5f4ec281d32f12af88cb22a782e497c8e52f69cf60b73ac7d6171fc95f1f8040b3d0ad2ff3f016d22ac1d91c5b522e5d03203534a76742bc55a082af5  init"
