_flavor=motorola-osprey
_hash="ac87541051278c7ea7f494c0b689d08e4a5cdd4f"
_config="config-$_flavor.armhf"

pkgname=linux-$_flavor
pkgver=3.10.49
case $pkgver in
	*.*.*)  _kernver=${pkgver%.*};;
	*.*) _kernver=$pkgver;;
esac
pkgrel=6
arch="armhf"
pkgdesc="Moto G (2015) kernel from LineageOS"
url="https://github.com/LineageOS/android_kernel_motorola_msm8916"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev dtbtool gcc6"
options="!strip !check !tracedeps pmb:cross-native"
source="
	$pkgname-$_hash.tar.gz::https://github.com/LineageOS/android_kernel_motorola_msm8916/archive/$_hash.tar.gz
	$_config
	compiler-gcc6.h
	Wno-error.patch
	02_mdss_fb_refresh_rate.patch
"
license="GPL2"

_abi_release=$pkgver
_carch="arm"

# Compiler: this kernel was only tested with GCC6. Feel free to make a merge
# request if you find out that it is booting working with newer GCCs as
# well. See <https://postmarketos.org/vendorkernel> for instructions.
if [ "${CC:0:5}" != "gcc6-" ]; then
	CC="gcc6-$CC"
	HOSTCC="gcc6-gcc"
	CROSS_COMPILE="gcc6-$CROSS_COMPILE"
fi

ksrcdir="$srcdir/android_kernel_motorola_msm8916-$_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
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine"
	# Generate dt.img
	dtbTool -s 2048 -p "scripts/dtc/" -o "arch/arm/boot/dt.img" "arch/arm/boot"
}

package() {
	export builddir="$srcdir"/build
	# 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

	# Master DTB (deviceinfo_bootimg_qcdt)
	install -Dm644 "$builddir/arch/arm/boot/dt.img" \
		"$pkgdir/boot/dt.img"

	cd "$srcdir"/build

	make -j1 modules_install \
		ARCH="$_carch" \
		INSTALL_MOD_PATH="$pkgdir"
}

sha512sums="fbc2e1fa6ff33dd4c20bb063c655fe85ee20c194a9c19a5fadd3434b8ade4b95377848349ce9266d74ca34882446e1e257c0e1a3e35bd23ff05813918dc74750  linux-motorola-osprey-ac87541051278c7ea7f494c0b689d08e4a5cdd4f.tar.gz
d98f97a3dce8c580161f185bf0ebe2a349c94539848a742adfe6d94b30a709c53d1ac59c5fdfb53b72b5f2fbccb0e62c45117951cbcbdae22fdf24690f4d79e1  config-motorola-osprey.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h
135828e7c9a8cdd4e1f71143e84a80a5d882d2627cacf5e5d808d1172d57cb0ca19520b53429a5f3eee7a17d1489bd77d31ce34e3790062fadb6aa392c09e1af  Wno-error.patch
a8c955bf718f155011e980f3d0948be98e1fee5649f418299a4851780543019daf7afa406aa7b0829375645107e4e6fbf241026b0cabe2b2ac895a47df83d2d8  02_mdss_fb_refresh_rate.patch"
