# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

_flavor=${FLAVOR:-vanilla}
_realname=zfs
_name=$_realname-$_flavor

_kpkg=linux-$_flavor
_kver=4.9.75
_kpkgrel=0

_realver=0.7.1
_mypkgrel=0

_kernelver=$_kver-r$_kpkgrel
_abi_release=${_kver}

pkgname=$_name
pkgver=$_kver
pkgrel=$(($_kpkgrel + $_mypkgrel))
pkgdesc="ZFS for Linux"
url="http://zfsonlinux.org"
arch="x86 x86_64 aarch64"
license="CDDL"
depends="spl-$_flavor linux-${_flavor}=${_kernelver}"
depends_dev="glib-dev e2fsprogs-dev util-linux-dev libtirpc-dev
	linux-$_flavor-dev=$_kernelver spl-$_flavor-dev"
makedepends="$depends_dev automake autoconf libtool linux-headers"
install_if="zfs linux-vanilla-$_kver"
install=""
subpackages="$pkgname-dev"
source="https://github.com/zfsonlinux/zfs/releases/download/zfs-$_realver/zfs-$_realver.tar.gz
	"


builddir="$srcdir/$_realname-$_realver"

prepare() {
	# source the kernel version
	(
	if [ -f "$startdir"/../../main/linux-$_flavor/APKBUILD ]; then
	        . "$startdir"/../../main/linux-$_flavor/APKBUILD
	        if [ "$_kver" != "$pkgver" ]; then
			die "$_name: Please update _kver to $pkgver"
		fi
	        if [ "$_kpkgrel" != "$pkgrel" ]; then
			die "$_name: Please update _kpkgrel to $pkgrel"
		fi
	fi
	) || return 1

	default_prepare
	autoreconf -vif || return 1
}

build() {
	cd "$builddir"
	./configure --prefix=/usr \
		--with-tirpc \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--with-config=kernel \
		--with-linux=/usr/src/linux-headers-${_abi_release} \
		--with-spl=/usr/src/spl-${_realver} \
		|| return 1
	make || return 1
}

package() {
	cd "$builddir"
	make DESTDIR="$pkgdir" \
		install || return 1
}

dev() {
	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/src "$subpkgdir"/usr || return 1
        default_dev
}

sha512sums="64b0a3b5eeaf09ce824e3e029880a8e5dcb3e4567d1fb2fa24c89d8e55fee1ebe2d2f3a9f77d5468cc5637074e27d5ba7696b566829c93fd69c610b288200a37  zfs-0.7.1.tar.gz"
