# Template file for 'nim'
pkgname=nim
version=0.11.2
revision=1
depends="gcc"
hostmakedepends="libzip git ed"
short_desc="The Nim programming language"
maintainer="allan <mail@may.mooo.com>"
license="MIT"
homepage="http://nim-lang.org/"
distfiles="https://github.com/Araq/Nim/archive/v${version}.tar.gz"
checksum=8b46ca59461f870471060dcaff535c7203937c7ae67856f1ba027d000b5fa4c6
wrksrc=Nim-$version

post_extract() {
	cd $wrksrc
	test -d csources ||
	git clone --depth=1 https://github.com/nim-lang/csources
}

do_build() {
	case "$XBPS_TARGET_MACHINE" in
		arm*)
			ed config/nim.cfg <<-EDIT
				,s/^arm.linux.gcc.exe .*/arm.linux.gcc.exe = "$CC"/
				,s/^arm.linux.gcc.linkerexe .*/arm.linux.gcc.linkerexe = "$CC"/
				w
				q
			EDIT
	esac

	cd csources && sh build.sh
	cd ..
	ed koch.nim <<-EDIT
		,s/-d:useLibzipSrc//
		w
		q
	EDIT
	ed lib/wrappers/zip/libzip.nim <<-EDIT
		,s/.2|.1|.0/.4/
		w
		q
	EDIT

	bin/nim c koch

	case "$XBPS_TARGET_MACHINE" in
		arm*) bin/nim c -d:release --os:linux --cpu:arm compiler/nim;;
		*) ./koch boot -d:release
	esac
}

do_install() {
	./koch install ${DESTDIR}/usr/lib

	if [ "$CROSS_BUILD" ]; then
		mv compiler/nim ${DESTDIR}/usr/lib/nim/bin/nim
	fi

	vmkdir usr/bin
	ln -sf /usr/lib/nim/bin/nim ${DESTDIR}/usr/bin/nim
}
