# Template file for 'nim'
pkgname=nim
version=0.17.2
revision=1
_cversion=0.17.2
_nimbleversion=0.8.8
depends="gcc"
hostmakedepends="ed"
short_desc="The Nim programming language"
maintainer="allan <mail@may.mooo.com>"
license="MIT"
homepage="http://nim-lang.org/"
distfiles="https://github.com/nim-lang/Nim/archive/v${version}.tar.gz
 https://github.com/nim-lang/csources/archive/v${_cversion}.tar.gz>csources-${_cversion}.tar.gz
 https://github.com/nim-lang/nimble/archive/v${_nimbleversion}.tar.gz>nimble-${_nimbleversion}.tar.gz"
checksum="eb8a98c7e576e8d858de98473569a29c539308fc7bb15a086b3f2b73d675f752
 7eb28ba7d5a03d02fc1c1fd9089c037de9c058d327fd6e95d599db63d9afa9c8
 4a849370f58af0d6fe940c15536ab9487f75517c695dd845f84ad96e71f2f4bd"
build_wrksrc=Nim-$version
create_wrksrc=yes
nopie=yes

post_extract() {
	mv csources-$_cversion $build_wrksrc/csources
	mkdir $build_wrksrc/dist
	mv nimble-$_nimbleversion $build_wrksrc/dist/nimble
}

do_build() {
	cd csources
	CFLAGS= LDFLAGS= sh build.sh
	cd ..

	ed lib/wrappers/openssl.nim <<-EDIT
		,s/versions = "(/versions = "(|.43|.41/
		w
		q
	EDIT

	bin/nim c koch
	./koch boot -d:release

	case "$XBPS_TARGET_MACHINE"
	in aarch64*) _arch=arm64
	;; arm*) _arch=arm
	esac

	case "$XBPS_TARGET_MACHINE"
	in arm*|aarch64*)
		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"/
			a
			arm64.linux.gcc.exe = "$CC"
			arm64.linux.gcc.linkerexe = "$CC"
			.
			w
			q
		EDIT
		bin/nim c -d:release --os:linux --cpu:$_arch --listCmd compiler/nim
		for _p in \
			dist/nimble/src/nimble \
			tools/nimgrep \
			nimsuggest/nimsuggest
		do
			bin/nim c -d:release --os:linux --cpu:$_arch --listCmd $_p
			mv $_p bin
		done
	;; *)
		./koch tools
	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
	vmkdir usr/share/nim
	ln -sf /usr/lib/nim/bin/nim ${DESTDIR}/usr/bin/nim
	for _f in nimble nimsuggest nimgrep; do
		chmod 0755 bin/$_f
		cp bin/$_f ${DESTDIR}/usr/lib/nim/bin
		ln -sf /usr/lib/nim/bin/$_f ${DESTDIR}/usr/bin/$_f
	done
	cp -r doc examples ${DESTDIR}/usr/lib/nim
	ln -sf /usr/lib/nim/doc ${DESTDIR}/usr/share/nim/doc
	ln -sf /usr/lib/nim/examples ${DESTDIR}/usr/share/nim/examples
	vlicense copying.txt
}
