# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=ruby-ffi
_gemname=ffi
pkgver=1.16.3
pkgrel=1
pkgdesc="Ruby FFI library"
url="https://github.com/ffi/ffi"
arch="all"
license="BSD-3-Clause"
checkdepends="ruby-rspec"
makedepends="libffi-dev ruby ruby-dev"
install="$pkgname.post-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/ffi/$_gemname/archive/v$pkgver.tar.gz
	musl-compat.patch
	gemspec.patch
	"
builddir="$srcdir/$_gemname-$pkgver"

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

check() {
	local exclude=''
	# FIXME fails with: expected -0.477986771... to be within 0.01 of 0.3
	case "$CARCH" in
		x86)  exclude='spec/ffi/long_double_spec.rb' ;;
	esac

	# LD_LIBRARY_PATH is a workaround to fix test
	# `Library ffi_lib interprets INPUT() in loader scripts` in library_spec.
	GEM_PATH='dist:.gem' LD_LIBRARY_PATH="$builddir/spec/ffi/fixtures" \
		rspec --exclude-pattern "$exclude"
}

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="
622aae9d55b8e859eab58988eaf976206010ab816351567f2525e606b83b3349ce6b9f114208ba9a7c6de97cc6fe73f5708cf968b7dea0944134c6cb44b55c9b  ruby-ffi-1.16.3.tar.gz
634e98e1480d82d447ed709adec6468e41150ba807d44a8deb8507989f571417418bb6bf363f6cc5c0d832d936d214bcf20783cca8b758b2f03bfaee404ea4d7  musl-compat.patch
b9cb034a6730f2070a345a08b1504f20c1e90ad0cde9e53c3415c2c1cee9b437d5b5eb81c0fcabdd8f0e048941bf0ab81b8fbcd7c11475a341232a069306ec77  gemspec.patch
"
