# Reference: <https://postmarketos.org/vendorkernel>
# Kernel config based on: <https://git.archlinux.org/svntogit/packages.git/plain/trunk/config?h=packages/linux>

pkgname="linux-chuwi-hi10plus"
pkgver=5.1.7
pkgrel=0
pkgdesc="Chuwi Hi10 Plus stable kernel"
arch="x86_64"
_carch="x86"
_flavor="chuwi-hi10plus"
url="https://kernel.org"
license="GPL-2.0-only"
options="!strip !check !tracedeps !pmb:kconfigcheck"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev
	     devicepkg-dev bison flex openssl-dev xz coreutils findutils"
subpackages="$pkgname-dev"

# Compiler: latest GCC from Alpine
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

# Source
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$pkgver.tar.gz::https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/linux-$pkgver.tar.gz
	$_config
	input-silead-Add-MSSL0017-to-acpi_device_id.patch
	platform-x86-touchscreen_dmi-Add-info-for-the-CHUWI-.patch
"
builddir="$srcdir/linux-$pkgver"

prepare() {
	default_prepare
	REPLACE_GCCH=0 \
		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
	cd "$builddir"

	make -j1 modules_install \
		ARCH="$_carch" \
		INSTALL_MOD_PATH="$pkgdir" \
		INSTALL_DTBS_PATH="$pkgdir/usr/share/dtb"

}

dev() {
    cd $builddir

    # https://github.com/torvalds/linux/blob/master/Documentation/kbuild/headers_install.txt
    make -j1 headers_install \
        ARCH="$_carch" \
        INSTALL_HDR_PATH="$subpkgdir"/usr
}

sha512sums="8d84560e22a6473653efa1dc229c8f8c4f95da8c2f61050583bb1285efa774c958fb13da1cf83a4f9c2cd2e73f2075569da346fa64399ccd058e793754da066a  linux-chuwi-hi10plus-5.1.7.tar.gz
c591fbe7eef35cd62eed569782cdafb06a07d216d3657b007469502119426b3f52eccd268f02d5fd60e461bc96b262ca3f5f4b1ab9a7122d7af27d83db6cadc6  config-chuwi-hi10plus.x86_64
cffb4b1dc204a15697aa26749df49b3f450de808cb630cf53d4718d933031a42789b79e94d39a9fe44a75e385f609c70aa8ccfb2e91e4fe473e0ee5245146c66  input-silead-Add-MSSL0017-to-acpi_device_id.patch
b97509150d4061001e90e1a595723c479252647b5f94f50b7be0673d11556b28a90fa0afff9f0961a4512c3cbc8d4b02b827e75f75385cbb3ad63b378f3931e3  platform-x86-touchscreen_dmi-Add-info-for-the-CHUWI-.patch"
