# APKBUILD based on linux-vanilla aport. Changes:
# - disabled module installation
# - add !check !tracedeps
# - add xz package (necessary to build)
#
# Kernel config based on: arch/arm/configs/bravo_defconfig
# Changes:
# - apply patches to compile with gcc6
# - enable devtmpfs (needed for udev -> touch support in weston)
# - enable udev required options (https://wiki.gentoo.org/wiki/Udev#Kernel)
# - disable ANDROID_PARANOID_NETWORK (removes network restrictions)

_vendor=htc
_flavor=htc-bravo
_config="config-${_flavor}.armhf"
_hash="4bc19919f805777947c243e6c2ed41ece530488b"

pkgname=linux-${_flavor}
pkgver=3.0.16
case $pkgver in
	*.*.*)  _kernver=${pkgver%.*};;
	*.*) _kernver=$pkgver;;
esac
pkgrel=6
arch="armhf"
pkgdesc="HTC Desire kernel from spezi77"
url="https://github.com/spezi77/android_kernel_htc_qsd8k_3.0"
depends="postmarketos-mkinitfs"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev xz"
options="!strip !check !tracedeps"
install=
source="
	$pkgname-$_hash.tar.gz::https://github.com/spezi77/android_kernel_htc_qsd8k_3.0/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_htc_qsd8k_3.0-${_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
	yes "" | make -C "$ksrcdir" O="$srcdir"/build ARCH="$_carch" HOSTCC="$HOSTCC" \
		oldconfig
}



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

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

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

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"
}

sha512sums="94929945d6fcb4428f320325ced207a9888cd758357766ef914c4ca2d5cea4b9cced77275463dac316591ba942e6e1514fec8c1f8e11c8324155116785ffb277  linux-htc-bravo-4bc19919f805777947c243e6c2ed41ece530488b.tar.gz
742a9ebbc2ba0fae46e2c8a185b4b68b1f14b3c3c15f42813836afb0be7607967ce56c35137c0c6731b9444299306f3c86683463b6b904171d33b08dd8263780  config-htc-bravo.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h"