# $Id: PKGBUILD 117342 2014-08-13 06:39:46Z tpowa $
# Maintainer: Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
# Contributor: Bob Fanger < bfanger(at)gmail >
# Contributor: Filip <fila pruda com>, Det < nimetonmaili(at)gmail >

pkgname=r8168
pkgver=8.038.00
pkgrel=10
pkgdesc="A kernel module for Realtek 8168 network cards"
url="http://www.realtek.com.tw"
license=("GPL")
arch=('i686' 'x86_64')
depends=('glibc' 'linux')
makedepends=('linux-headers')
install=$pkgname.install
source=("https://dl.dropboxusercontent.com/u/362439/archlinux/r8168-$pkgver.tar.bz2"
        delayed_work_ethtool_ops_fix2.patch)
sha256sums=('1384bdf4d33b219f937bf1a600c72b22962baf7796e562b08e4da3c000f49072'
            '2a03fa65e476cb7dd3ed5976037e0218260ff2660b6a4c495a824865eb10c303')

prepare(){
	cd "$pkgname-$pkgver"

	# Fix build with Linux 3.15 and 3.16
	# https://code.google.com/p/r8168/issues/detail?id=19
	patch -Np1 -i "$srcdir/delayed_work_ethtool_ops_fix2.patch"
}

build() {
	_kernver=$(pacman -Q linux | cut -d . -f 2 | cut -f 1 -d -)
	KERNEL_RELEASE=$(cat /usr/lib/modules/extramodules-3.$_kernver-ARCH/version)

	cd "$pkgname-$pkgver"

	# avoid using the Makefile directly -- it doesn't understand
	# any kernel but the current.
	make -C /usr/lib/modules/$KERNEL_RELEASE/build \
			SUBDIRS="$srcdir/$pkgname-$pkgver/src" \
			EXTRA_CFLAGS="-DCONFIG_R8168_NAPI -DCONFIG_R8168_VLAN" \
			modules
}

package() {
	_kernver=$(pacman -Q linux | cut -d . -f 2 | cut -f 1 -d -)
	depends=("linux>=3.${_kernver}" "linux<3.$(expr ${_kernver} + 1)")
	KERNEL_VERSION=$(cat /usr/lib/modules/extramodules-3.$_kernver-ARCH/version)
	msg "Kernel = $KERNEL_VERSION"

	cd "$pkgname-$pkgver"
	install -Dm644 src/$pkgname.ko "$pkgdir/usr/lib/modules/extramodules-3.$_kernver-ARCH/$pkgname.ko"
	find "$pkgdir" -name '*.ko' -exec gzip -9 {} +

	sed -i "s|extramodules-.*-ARCH|extramodules-3.$_kernver-ARCH|" "$startdir/$pkgname.install"
}

