# Reference: <https://postmarketos.org/vendorkernel>
# Kernel config based on: arch/arm/configs/k2_ul_defconfig

pkgname="linux-htc-k2ul"
pkgver=3.4.10
pkgrel=1
pkgdesc="HTC One SV kernel fork"
arch="armv7"
_carch="arm"
_flavor="htc-k2ul"
url="https://kernel.org"
license="GPL-2.0-only"
options="!strip !check !tracedeps"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev devicepkg-dev gcc4"

# Compiler: GCC 6 (doesn't boot when compiled with newer versions)
if [ "${CC:0:5}" != "gcc4-" ]; then
	CC="gcc4-$CC"
	HOSTCC="gcc4-gcc"
	CROSS_COMPILE="gcc4-$CROSS_COMPILE"
fi

# Source
# How to get the original version:
# 1. Go to HTCDev.com, log in and go to the kernel source page
# 2. Select the One SV, EU region and the v4.1.2 Android version
# 3. Download the kernel with the 2.14.401.6 description
_repository="k2ul-jb-3.4.10-cl157896-2"
_commit="6ac3629384fac88beef65fd841f5135f9f832624"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_commit.tar.gz::https://github.com/Hacker1245/${_repository}/archive/${_commit}.tar.gz
	$_config
	gcc8-fix-put-user.patch
	fix_return_address.patch
	no-hardcoded-CROSS_COMPILE.patch
	fix_perl.patch
	fix_otg_errors.patch
	fix_kgsl_error.patch
"
builddir="$srcdir/${_repository}-${_commit}"

prepare() {
	default_prepare
	downstreamkernel_prepare "$srcdir" "$builddir" "$_config" "$_carch" "$HOSTCC"
}

build() {
	unset LDFLAGS
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-postmarketOS"
}

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
}

sha512sums="de02e36c7c19c886cc1f16b5a3a4464542a905e85cc3a531bd27b4944ee1ada01aad6e5fe8fd0346b991c923144154fbce8d86b17f69e69d05549440129b2fc9  linux-htc-k2ul-6ac3629384fac88beef65fd841f5135f9f832624.tar.gz
2215579488fb0c90f3d6e3b2a0fb395630aa9e3b2f87495db0263e46ad8a181fd72f8c08c40ad7f13bde9fd014b873d02edde5a743e9d670008e534ebdc957f8  config-htc-k2ul.armv7
197d40a214ada87fcb2dfc0ae4911704b9a93354b75179cd6b4aadbb627a37ec262cf516921c84a8b1806809b70a7b440cdc8310a4a55fca5d2c0baa988e3967  gcc8-fix-put-user.patch
b0a73e261b6dedfe6135a17d25e9e989958b4ae379ba31abbf9b0e05f50f3082c4f5d4ff4fb848e8d283acbf68c54014ac8cfcc57d4e1be2d456e715237b661c  fix_return_address.patch
555033f2ef3e23d84a552e92a6307bf292f91146ee4d8a03b2485f9b5028c2aa3f7ced6ef268acaca7d12649f4bfa7f8fccca39394097f63a5ff2134b912e9e2  no-hardcoded-CROSS_COMPILE.patch
584543558b32dc4fe6d1156a466b8c6acf869cbe64cd59cbbc9d54b716d195ea2807f2c337b4a22a0bbd9b350b07f27de0fd4c750b599afb337ff953dd920bd9  fix_perl.patch
f6229e37304d127cd39bbbd6f0e11100ca8c6b44f1e66b125aba06d7d5ae96d380273e55a7a8fec46af324979523e032de9151d92e181da9242fb5aaf0dba769  fix_otg_errors.patch
c84d3eb75628ee37ad5d16f858bdbf7b9fc2ff1336e5d91559ecdc4d198127b25929c3b264a50f526fafd3722aa39fb2d87fedcfb3a134f2ddd355650a65a4bd  fix_kgsl_error.patch"
