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

pkgname=linux-jolla-sbj
pkgver=3.4.108
pkgrel=2
pkgdesc="Jolla phone kernel fork"
arch="armv7"
_carch="arm"
_flavor="jolla-sbj"
url="http://releases.sailfishos.org/sources/3.1.0.11/"
license="GPL2"
options="!strip !check !tracedeps pmb:cross-native"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev devicepkg-dev gcc6 rpm2cpio"
_kbuildver="3.4.108.20200929.1"
_rpm_wlan_commit="2f37d840cc7f52eb38b0613a832a55b216d89bd5"
_rpm_adapt_commit="8472b1e422b50fb5aa1ea46231a64db8ab2573dd"
_rpm_wlan_version="0.2.7-1.3.6"
_rpm_adapt_version="1.11.2"
_rpm_wlan_file="sbj-wlan-$_rpm_wlan_version.jolla.armv7hl.rpm"
_rpm_adapt_file="kernel-adaptation-sbj-$_kbuildver-$_rpm_adapt_version.jolla.armv7hl.rpm"

# 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

# Source
_hash="3.1.0.11"
_config="config-$_flavor.$arch"
source="
	$pkgname-$_hash.tar.bz2::http://releases.sailfishos.org/sources/$_hash/sailfish-$_hash-oss-adaptation-sbj.tar.bz2
	$_config
	01_fix-video-argb-setting.patch
	02_gpu-msm-fix-gcc5-compile.patch
	duplicate-return-address-definition.patch
	https://gitlab.com/postmarketeer/jolla-sbj-wlan/-/raw/$_rpm_wlan_commit/$_rpm_wlan_file
	https://gitlab.com/postmarketeer/jolla-kernel-adaptation-sbj/-/raw/$_rpm_adapt_commit/$_rpm_adapt_file
"
_kver="kernel-adaptation-sbj-3.4.108.20190506.1"
_kernelarchive="$srcdir/sailfish-$_hash-oss-adaptation-sbj/kernel-adaptation-sbj/$_kver.tar.bz2"
builddir="$srcdir/$_kver"
_rpm_mod_path="./lib/modules/$_kbuildver"
_files="
	kernel/net/wireless/wlan.ko
	kernel/net/wireless/cfg80211.ko
"

prepare() {
	cd "$srcdir"
	tar -xf "$_kernelarchive"
	cd "$ksrcdir"

	default_prepare
	. downstreamkernel_prepare
}

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

package() {
	rpm2cpio "$srcdir/$_rpm_wlan_file" | cpio -id "$_rpm_mod_path/kernel/net/wireless/*.ko"
	rpm2cpio "$srcdir/$_rpm_adapt_file" | cpio -id "$_rpm_mod_path/kernel/net/wireless/*.ko"

	downstreamkernel_package "$builddir" "$pkgdir" "$_carch" "$_flavor"

	_kernel_release=$(cat $builddir/include/config/kernel.release)
	for _i in $_files; do
		install -Dm644 "$_rpm_mod_path/$_i" \
		"$pkgdir/lib/modules/$_kernel_release/$_i"
	done
}

sha512sums="
b43353cb1f5c0205535153515eed0c10ca652fc1b2f28c12a40e8dbce08e304c49991e8c155800711fb0c07012c820a26fbf4c931b9b015994b67b1dc4615ef6  linux-jolla-sbj-3.1.0.11.tar.bz2
cd008aafd3ae04dbf3f010e893fa2d47169a2045d1706a2f28d5175011da96ca1280a2c73692bb14f2e7452327d2e83f9fdc251997a51c8df9c3f4ae5eb03b10  config-jolla-sbj.armv7
e3083c260ed9c4fc0d83f43106700b6173876dbfae072f6e6696ac6785a4dc17771fbcb3e21601e04ed29a11bf0f7a2a09ad549375e0c34cea245c7f2e2bcbbd  01_fix-video-argb-setting.patch
7be03a9e78b7ac330a54b1f00509caa0621a95c0c55901878ad757f9dd69cc05ba2c8b5ea987063ae1224f92c4d090d515fa5d369e7755181a4871b0d0f82881  02_gpu-msm-fix-gcc5-compile.patch
9b15bf1f6cb66f54c785b0af6c9db8a7d63257e6fa3eeb0cbf47284334166aa6a7ff93e1b64ab69206c8047d64641199c0f5fcbc257bbe039263252fff45118e  duplicate-return-address-definition.patch
4c582883b609b5cd312b48213ac633e8c65ac3ea310c6016c01da769440f694f42c22f06f13599ce12e9992afe150de364fcaf2f816d9d678d019d00019f007b  sbj-wlan-0.2.7-1.3.6.jolla.armv7hl.rpm
96e152268ff4f25bd67aadbca9b6059f1dcd076306ca2b9567f439dd42146c1e1cffeea23a90a862e436467eb375c2295078ed6b01490d8baac318faea0d2e12  kernel-adaptation-sbj-3.4.108.20200929.1-1.11.2.jolla.armv7hl.rpm
"
