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

pkgname="linux-htc-m8"
pkgver=3.4.113
pkgrel=0
pkgdesc="HTC One M8 kernel fork"
arch="armhf"
_carch="arm"
_flavor="htc-m8"
url="https://kernel.org"
license="GPL2"
options="!strip !check !tracedeps"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev dtbtool xz"
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

# Source
_repository="android_kernel_htc_msm8974"
_commit="1e7a57485c90cb838cffe022831e6bec4a599548"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_commit.tar.gz::https://github.com/LineageOS/${_repository}/archive/${_commit}.tar.gz
	$_config
	compiler-gcc6.h
	02_gpu-msm-fix-gcc5-compile.patch
	psmouse_base.patch
"
builddir="$srcdir/${_repository}-${_commit}"

prepare() {
	default_prepare

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

	# Remove -Werror from all makefiles
	local i
	local makefiles="$(find . -type f -name Makefile)
		$(find . -type f -name Kbuild)"
	for i in $makefiles; do
		sed -i 's/-Werror-/-W/g' "$i"
		sed -i 's/-Werror//g' "$i"
	done

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

	# Generate master DTB (deviceinfo_bootimg_qcdt)
	dtbTool -s 2048 -p "scripts/dtc/" -o "arch/arm/boot/dt.img" "arch/arm/boot/"
}

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

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

sha512sums="3f826c698af5839fd83e9204d8d4c28db35a0e8c174c0cef005acea5fa53b2463fb0d984f2d1c99e8608ecd9698fb59e954f2e7218688f75a97fee735b7b9856  linux-htc-m8-1e7a57485c90cb838cffe022831e6bec4a599548.tar.gz
e64aee85e627e943fa8acbf9c9600615a6c5bb97098aa7113c57fdbee60c966f064416946e97bc9ea1013a00b9771236c8a7452436144bb4566a9ba62e9fbfbf  config-htc-m8.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h
7be03a9e78b7ac330a54b1f00509caa0621a95c0c55901878ad757f9dd69cc05ba2c8b5ea987063ae1224f92c4d090d515fa5d369e7755181a4871b0d0f82881  02_gpu-msm-fix-gcc5-compile.patch
f7e31cfe422696922aec52002e56c58103183966edee3bb8e1995d33f8d37b07c32280a8ecc213874e358a6ff367418556a4542fba7e3c17e4b8e82e05c3a543  psmouse_base.patch"
