# Contributor: Will Sinatra <wpsinatra@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=chez-scheme
# XXX: Upgrades require rebuilding community/idris2 & community/idris2-stage0
pkgver=10.2.0
_realver=${pkgver%%[a-z]}
pkgrel=0
pkgdesc="Cisco R6RS Scheme Compiler"
url="https://github.com/cisco/ChezScheme"
arch="all"
license="Apache-2.0"
makedepends="util-linux-dev ncurses-dev libx11-dev zlib-dev lz4-dev"
subpackages="$pkgname-doc"
source="https://github.com/cisco/ChezScheme/releases/download/v$pkgver/csv$pkgver.tar.gz
	check-expected::https://raw.githubusercontent.com/cisco/ChezScheme/v$pkgver/.github/workflows/summarytest-some-fast

	disable-iconv-tests.patch"
builddir="$srcdir"/csv$pkgver

# upstream only seems to run the test suite on x86_64 and aarch64.
# Therefore, the expected check file from above only works on these.
case "$CARCH" in
x86_64|aarch64) ;;
*) options="!check" ;;
esac

case "$CARCH" in
	x86)     _chez_host=ti3le    ;;
	x86_64)  _chez_host=ta6le    ;;
	arm*)    _chez_host=tarm32le ;;
	aarch64) _chez_host=tarm64le ;;
	riscv64) _chez_host=trv64le  ;;
	ppc64le) _chez_host=tpb64l   ;;
	s390x)   _chez_host=tpb64b   ;;
esac

prepare() {
	default_prepare

	# Ensure we don't use vendored zlib/lz4 versions.
	rm -r zlib lz4
}

build() {
	./configure --os=tXle \
		--installprefix=/usr \
		--installman=/usr/share/man \
		--nogzip-man-pages \
		--temproot=$pkgdir \
		--machine=$_chez_host \
		--installschemename="chez" \
		--installscriptname="chez-script" \
		ZLIB="$(pkg-config --libs zlib)" LZ4="$(pkg-config --libs liblz4)"
	make
}

check() {
	# Only run a subset of the test suite, otherwise the test
	# take several hours to complete on some architectures.
	make test-some-fast

	# The `make test` command does not exit with a non-zero exit
	# status when test fail (see BUILDING). For this reason, we
	# compare the summary file here. This is also the approach
	# taken by ChezScheme for their GitHub CI configuration.
	diff -q "$srcdir"/check-expected "$_chez_host"/mats/summary
}

package() {
	make install DESTDIR="$pkgdir"

	# Remove pre-compiled example programs (reduces package size).
	rm -f "$pkgdir"/usr/lib/csv$_realver/examples/*.so

	# Move examples to -doc subpackage.
	mkdir -p "$pkgdir"/usr/share/doc/$pkgname/
	mv "$pkgdir"/usr/lib/csv$_realver/examples \
		"$pkgdir"/usr/share/doc/$pkgname/
}

sha512sums="
81755bdb04fc68e85d745f46a251a4bb8a8171115490a21686c90b4770f2c2fc00c6feac794f967408b070c2b53ae1167a87ecb3dc977a02af962317c49345bb  csv10.2.0.tar.gz
cdf845795cf06506b7af21d68325cb4d72044b6179bf2f827c658a3668657da353fe6006a576db52bc1bd737adc9e8971aa08b8ffa81a28b4e7a3df8dadc32b0  check-expected
c1177eedfd562593f2be664c4f0643b7cbe7d4ff0d9e994acbf226739f26f76d32f3b8f3024067e44536eb0e1edcff9a2daa3b61e05dfcd0e04b7430a3097503  disable-iconv-tests.patch
"
