# Template file for 'chez-scheme'
pkgname=chez-scheme
version=9.4
revision=1
wrksrc="ChezScheme-${version}"
build_style=gnu-configure
hostmakedepends="libX11-devel ncurses-devel zlib-devel"
makedepends="${hostmakedepends}"
short_desc="Compiler and run-time system for R6RS Scheme"
maintainer="Christian Neukirchen <chneukirchen@gmail.com>"
license="Apache-2.0"
homepage="http://scheme.com/"
distfiles="https://github.com/cisco/ChezScheme/archive/v${version}.tar.gz
 https://github.com/nanopass/nanopass-framework-scheme/archive/221eecb965d9dfacccd97d1cb73f2a31c4119d3a.tar.gz"
checksum="9f4e6fe737300878c3c9ca6ed09ed97fc2edbf40e4cf37bd61f48a27f5adf952
 b5522e7cdb1d90a0882c84ba4254aea195ec09b8697053acfa1335321c52ffe3"

only_for_archs="i686 i686-musl x86_64 x86_64-musl armv6l armv7l"
case "$XBPS_MACHINE" in
	i686*) _host=ti3le;;
	x86_64*) _host=ta6le;;
	arm*) _host=arm32le;;  # no threading makefile
	*) msg_error "$pkgname: unsupported host platform.\n";;
esac
case "$XBPS_TARGET_MACHINE" in
	i686*) _target=ti3le;;
	x86_64*) _target=ta6le;;
	arm*) _target=arm32le;;
	*) msg_error "$pkgname: unsupported target platform.\n";;
esac

post_extract() {
	# hack out submodules

	mkdir .git
	sed -i '/git submodule/s/^/: #/' configure

        mv ../nanopass-*/* nanopass 

	sed -i -e '/:/s,../zlib/libz.a,,g' \
		-e '/ld/s,../zlib/libz.a,,g' \
		-e '/^mdclib =/s/=/= $(LDFLAGS)/g' \
		-e '/^mdclib =/s/$/ -lz/g' \
		-e '/^C =/s/$/ $(CFLAGS)/g' \
		-e '/^C =/s/gcc/$(CC)/g' \
		-e 's/^\tld/\t$(LD)/g' \
		c/Mf-*
	ln -s /usr/include/zlib.h zlib
	ln -s /usr/include/zconf.h zlib
}

do_configure() {
	./configure --installprefix=/usr --installman=/usr/share/man \
		--nogzip-man-pages --temproot=$DESTDIR --machine=$_host
}

if [ "$CROSS_BUILD" ]; then

do_build() {
	make ${makejobs} CC="$BUILD_CC" CFLAGS="$BUILD_CFLAGS" \
		LD="ld" LDFLAGS="$BUILD_LDFLAGS"
	./workarea $_target
	cd $_target
	make ${makejobs} -C s -f Mf-cross m=$_host xm=$_target base=../../$_host
	make ${makejobs} -C c CC=$CC LD=$LD CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS"
}
do_install() {
	cd $_target
	make -f ../$_host/Mf-install DESTDIR=${DESTDIR} m=$_target
}

else

do_build() {
	make ${makejobs} CC=$CC LD=$LD CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS" 
}
do_install() {
	make DESTDIR=${DESTDIR} install
}

fi
