# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=ruby-ffi
_gemname=ffi
pkgver=1.9.23
pkgrel=0
pkgdesc="Ruby FFI library"
url="https://github.com/ffi/ffi"
arch="all"
license="BSD"
makedepends="libffi-dev ruby ruby-dev"
install="$pkgname.post-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/ffi/$_gemname/archive/$pkgver.tar.gz
	gemspec.patch"
builddir="$srcdir/$_gemname-$pkgver"
options="!check"  # does not work with PaX (it'd need paxmark -m /usr/bin/ruby)

build() {
	cd "$builddir"

	gem build $_gemname.gemspec
	gem install --local \
		--install-dir dist \
		--ignore-dependencies \
		--no-document \
		--verbose \
		$_gemname-$pkgver.gem
}

check() {
	cd "$builddir"

	# Install missing development dependencies needed and used only for running
	# tests. We can't install them easily with bundler, because there's
	# no way how to force it to NOT update already satisifed dependencies.
	# We can't install them directly with gem either, because there's no
	# way how to force it to instally only dependencies, not the gem itself.
	local dev_deps=$(gem install --explain --development --minimal-deps $_gemname-$pkgver.gem \
		| head -n -1 | sed -En 's/-([^-]+)$/:\1/p')
	gem install --instal-dir=.gem --conservative $dev_deps

	GEM_PATH='dist:.gem' .gem/bin/rspec
}

package() {
	local gemdir="$pkgdir/$(ruby -e 'puts Gem.default_dir')"

	local carch
	case "$CARCH" in
		x86) carch="i386";;
		arm*) carch="arm";;
		ppc64le) carch="powerpc64";;
		*) carch="$CARCH";;
	esac

	cd "$builddir"/dist

	mkdir -p "$gemdir"
	cp -r extensions gems specifications "$gemdir"/

	# Remove unnecessary files and rubbish...
	find "$gemdir"/extensions/ -name mkmf.log -delete

	cd "$gemdir"/gems/$_gemname-$pkgver
	rm -rf ext/ lib/*.so
	find lib/ffi/platform/* -prune ! -name "$carch-linux" -exec rm -rf {} +
}

sha512sums="39f862b8f946e67f84f915e015b895d137d68d50b90585c0ce9d4bb9dbcb5c714653c454635ac10b3a71290811f1afb32da90bbeb430dd589245e592fe55c70e  ruby-ffi-1.9.23.tar.gz
17c0cd281d0b471ea5e065dcfe027ca15c698c1393b3def3b59729c059d68b5a8969ad3cc0c88545ffbdb202a8750b219aa6cce9308a43e42845989cd650551a  gemspec.patch"
