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

pkgname="linux-samsung-p4wifi"
pkgver=4.16.0
pkgrel=1
pkgdesc="Galaxy Tab 10.1 kernel"
arch="armhf"
_carch="arm"
_flavor="samsung-p4wifi"
url="https://kernel.org"
license="GPL2"
options="!strip !check !tracedeps"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev xz flex bison libressl-dev"
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

# Source
_repository="linux"
_rev="b40b7265e2e3ec66e460e5615e7804f7a5d9db67"
_config="config-${_flavor}.${arch}"
source="
	$pkgname-$_rev.tar.gz::https://github.com/decatf/${_repository}/archive/${_rev}.tar.gz
	$_config
	compiler-gcc6.h
"
builddir="$srcdir/${_repository}-${_rev}"

prepare() {
	local _patch_failed=
	cd "$builddir"
	# first apply patches in specified order
	for i in $source; do
		case $i in
		*.patch|*.patch::*)
			_patch=${i%::*}
			msg "Applying $_patch..."
			if ! patch -s -p1 -N -i "$srcdir"/$_patch; then
				echo $_patch >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

	mkdir -p "$srcdir"/build
	cp -v "$srcdir"/$_config "$srcdir"/build/.config
	make -C "$builddir" O="$srcdir"/build ARCH="$_carch" HOSTCC="$HOSTCC" \
		olddefconfig
}

build() {
	cd "$srcdir"/build
	unset LDFLAGS
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-${_flavor}" \
		CFLAGS_MODULE=-fno-pic
}

package() {
	cd "$srcdir/build/arch/${_carch}/boot"

	if [ "$CARCH" == "aarch64" ]; then
		install -Dm644 "$srcdir/build/arch/${_carch}/boot/Image" \
			"$pkgdir/boot/vmlinuz-$_flavor"
	else
		install -Dm644 "$srcdir/build/arch/${_carch}/boot/"*zImage \
			"$pkgdir/boot/vmlinuz-$_flavor"
	fi

	install -D "$srcdir/build/include/config/kernel.release" \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"

	cd "$srcdir"/build

	local _install
	case "$CARCH" in
	aarch64*|arm*)	_install="modules_install dtbs_install" ;;
	*)		_install="modules_install" ;;
	esac

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


sha512sums="1678192f73cf4a0bba8e4aba4b7e90ce0e5a61d47a952328725e773ccc9060d42dc299e887e6398454442dfa1580bd68c7edd479a51ec0d91b9a7d8a06014b67  linux-samsung-p4wifi-b40b7265e2e3ec66e460e5615e7804f7a5d9db67.tar.gz
287c414d87431ca00fb455b4cc3325d243d01d75a819054053a28899152f1b0143707c10ac050b7daeac03e8d79d40886f30bca22b5272ff06963ef7d3026c97  config-samsung-p4wifi.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h"
