# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

_flavor=grsec
_kpkg=linux-$_flavor
_realname=open-vm-tools
_kver=4.4.47
_kpkgrel=0
_realver=10.1.0
_ver=${_realver/_p/-}
_mypkgrel=0

# source open-vm-tools version
if [ -f ../main/$_realname/APKBUILD ]; then
	. ../main/$_realname/APKBUILD
	[ "${_realver}" != "$pkgver" ] && pkgname=$_realname-$_flavor \
		&& die "please set _realver to $pkgver"
fi

# source the kernel version
if [ -f ../main/linux-${_flavor}/APKBUILD ]; then
        . ../main/linux-${_flavor}/APKBUILD
	[ "$_kver" != "$pkgver" ] && die "please set _kver to $pkgver"
	[ "$_kpkgrel" != "$pkgrel" ] && die "please set _kpkgrel to $pkgrel"
fi

_abiver="${_kver}-${_kpkgrel}"
_abi_releases="${_abiver}-${_flavor} ${_abiver}-virtgrsec"
_kernelver="$_kver-r$_kpkgrel"

pkgname="$_realname-$_flavor"
pkgver=$_kver
pkgrel=$(($_kpkgrel + $_mypkgrel))

pkgdesc="The Open Virtual Machine Tools are the open source implementation of VMware Tools."
url="https://github.com/vmware/open-vm-tools/"
arch="x86 x86_64"
license="LGPL"
subpackages="$_realname-virtgrsec:_virtgrsec"
depends="linux-${_flavor}=${_kernelver}"
depends_dev="bash glib-dev gettext-dev linux-${_flavor}-dev=${_kernelver} linux-virtgrsec-dev=${_kernelver}"
makedepends="$depends_dev autoconf automake libtool linux-headers"
source="https://github.com/vmware/open-vm-tools/archive/stable-$_ver.tar.gz
	0001-Fix-vmxnet-module-on-kernels-3.16.patch
	missing-config-dir.patch
	"
install_if="linux-${_flavor}=${_kernelver} open-vm-tools"

builddir="$srcdir/open-vm-tools-stable-$_ver"/open-vm-tools

prepare() {
	cd "$builddir"
	update_config_sub || return 1
	default_prepare || return 1
	autoreconf -vif || return 1
	for _abi in $_abi_releases; do
		mkdir ../build-$_abi
		cp -r * ../build-$_abi/
	done
}

build() {
	for _abi in $_abi_releases; do
		cd "$builddir"/../build-$_abi
		./configure \
			--build=$CBUILD \
			--host=$CHOST \
			--disable-deploypkg \
			--without-pam \
			--without-x \
			--without-dnet \
			--without-icu \
			--without-procps \
			--without-ssl \
			--with-kernel-release="${_abi}" \
			|| return 1
			make -j1 -C modules modules MODULES="vmxnet vmhgfs" \
				|| return 1
	done
}

package() {
	for _abi in $_abi_releases; do
		mkdir -p "$pkgdir"/lib/modules/${_abi}/misc/
		cd "$builddir"/../build-$_abi
		for module in $(find . -type f -name '*.ko'); do
			install -D -m644 $module "$pkgdir/lib/modules/${_abi}/misc/" || return 1
		done
	done
}

_virtgrsec() {
	pkgdesc="VMware guest modules for virtgrsec kernel"
	mkdir -p "$subpkgdir"/lib/modules
	mv "$pkgdir"/lib/modules/${_abiver}-virtgrsec "$subpkgdir"/lib/modules/ || return 1
}

md5sums="46f5a319275f63658f1a44b6c8755f6b  stable-10.1.0.tar.gz
3254b99481cea970acd6d65863ce6b48  0001-Fix-vmxnet-module-on-kernels-3.16.patch
5ab8e8249a65a8b750c5bf278f438351  missing-config-dir.patch"
sha256sums="2b76d8461022774f2c077b032fbe870f86046f20724eb24824cf9b6ccc4b5355  stable-10.1.0.tar.gz
85be67ed9827e685d55dd5cba25144d95296159ff1be73f491b343c395dd6a7d  0001-Fix-vmxnet-module-on-kernels-3.16.patch
fb7a4bb532ffc81c8be73c66008538f4119a1147bbbdbf32328d286f012b2035  missing-config-dir.patch"
sha512sums="f497b828510ebc2a88788ad5292047f20513c844e889b3f652ecce382efd6d4672560be7387b55dcb7300cfdde5c8eda407836c9b75614c018e058bb169b374d  stable-10.1.0.tar.gz
c445631b7faf8badfbf8e66518bfb70aec1049c1a0393ff038253e70190d7f8580cbcd3e7037c43664ee0ee48d26edd8f8eaca06383bbd7fa763a182050f9551  0001-Fix-vmxnet-module-on-kernels-3.16.patch
f77d3848d87d7c387d2cc1555ff3a39e547a8e8fcecee3aa1f785b828fd238c3779c1e5704f52ec46701b29a2ad9aec1cd5aef04677bc2a03b8a7780435f7fdd  missing-config-dir.patch"
