# Kernel config based on: arch/arm/configs/kobo_defconfig
maintainer="Andreas Kemnade <andreas@kemnade.info>"
pkgname=linux-postmarketos-imx-ereader
pkgver=7.0.0
pkgrel=0
pkgdesc="i.MX E-Book reader kernel fork, close to mainline"
replaces="linux-kobo-clara-mainline"
provides="linux-kobo-clara-mainline=$pkgver-r$pkgrel"
arch="armv7"
_carch="arm"
_flavor="postmarketos-imx-ereader"
url="https://kernel.org"
license="GPL-2.0-only"
options="!strip !check !tracedeps pmb:cross-native pmb:kconfigcheck-nftables"
makedepends="
	bash
	bc
	bison
	clang
	devicepkg-dev
	findutils
	flex
	lzop
	llvm
	lld
	openssl-dev
	perl
	postmarketos-installkernel
	zstd
"

# Source
_repository="linux"
_commit="7c2dc65b1abd2439f465e32aea0de4749baaecc8"
_rtl8189fs_repository="rtl8189ES_linux"
_rtl8189fs_commit="d685a6f7200dac6ddf2a4b1b96b848b92f34f9dd"
_config="config-$_flavor.$arch"
source="
	$pkgname-$_commit.tar.gz::https://github.com/akemnade/$_repository/archive/$_commit.tar.gz
	$_rtl8189fs_repository-$_rtl8189fs_commit.tar.gz::https://github.com/jwrdegoede/$_rtl8189fs_repository/archive/$_rtl8189fs_commit.tar.gz
	$_config
	0001-another-CFI-fix.ppatch
"
builddir="$srcdir/$_repository-$_commit"
_rtl8189fs_dir="$srcdir/$_rtl8189fs_repository-$_rtl8189fs_commit"

prepare() {
	default_prepare
	cp -v "$srcdir"/$_config .config
	patch -d "$_rtl8189fs_dir" -p1 <"$srcdir"/0001-another-CFI-fix.ppatch
}

build() {
	unset LDFLAGS
	make ARCH="$_carch" LLVM=1 \
		KBUILD_BUILD_VERSION="$(( pkgrel + 1 ))-postmarketOS"
	make ARCH="$_carch" LLVM=1 \
		KBUILD_BUILD_VERSION="$(( pkgrel + 1 ))-postmarketOS" \
		CONFIG_RTW_SDIO_PM_KEEP_POWER=n \
		CONFIG_RTW_DEBUG=n \
		-C "$_rtl8189fs_dir" KSRC="$builddir"
}

package() {
	mkdir -p "$pkgdir"/boot
	make zinstall modules_install dtbs_install \
		LLVM=1 \
		O="$_outdir" ARCH="$_carch" \
		INSTALL_MOD_STRIP=1 \
		INSTALL_PATH="$pkgdir"/boot \
		INSTALL_MOD_PATH="$pkgdir/usr" \
		INSTALL_DTBS_PATH="$pkgdir/boot/dtbs"
	install -Dm644 "$_rtl8189fs_dir"/8189fs.ko -t "$pkgdir/usr/lib/modules/$(make -s O="$_outdir" ARCH="$_carch" kernelrelease)/kernel/drivers/net/wireless"
	install -D "$builddir"/include/config/kernel.release \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"
}

sha512sums="
aba1d8d169222e89f7e293935cd29e2e290309f11c5feb3610d8c6015fd482ddbfeed91266f13c0c64fcad31bef9d465009246e68dec347b39b09473792e2bfb  linux-postmarketos-imx-ereader-7c2dc65b1abd2439f465e32aea0de4749baaecc8.tar.gz
fc385f053a98d3d2e555d08d10506168d844dbfa37c8e6f78dd7f0324b7d84c679850a50b36a8da5f1383120a089625090c47f7621b8b9246374d63c8924acb5  rtl8189ES_linux-d685a6f7200dac6ddf2a4b1b96b848b92f34f9dd.tar.gz
b3e70251d6bce739ab8c981f5fd91548a8a2e276249ca035c3e50345378d7f4550e990a1b7de1f2e2e1d9dec984a22a6ae2be1f85d7763d655b0c0e2bf43203a  config-postmarketos-imx-ereader.armv7
170ae9311268988ae7ecd5ad592f239cf4d30412fa3249befd756b41ba1ca98e815408edbc1f959b51f18da7665219ff6d7563044650c6ba15d133f2dfa92f8c  0001-another-CFI-fix.ppatch
"
