# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=crystal
pkgver=0.24.2
pkgrel=4
_bootver=0.24.2
_llvmver=5
pkgdesc="The Crystal Programming Language"
url="https://crystal-lang.org/"
arch="x86_64 aarch64"
license="Apache-2.0"
depends="gc-dev libatomic_ops libevent-dev gcc gmp-dev pcre-dev"
checkdepends="libressl-dev libxml2-dev paxmark readline-dev tzdata yaml-dev zlib-dev"
makedepends="llvm$_llvmver-dev ${BUILD_STATIC:+"llvm$_llvmver-static"}"
subpackages="$pkgname-doc
	$pkgname-bash-completion:bashcomp:noarch
	$pkgname-zsh-completion:zshcomp:noarch
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/crystal-lang/$pkgname/archive/$pkgver.tar.gz
	https://dev.alpinelinux.org/archive/crystal/crystal-$_bootver-x86_64-alpine-linux-musl.tar.gz
	https://dev.alpinelinux.org/archive/crystal/crystal-$_bootver-aarch64-alpine-linux-musl.tar.gz
	makefile-shell.patch
	fix-makefile-stderr-redirect.patch
	makefile-allow-extra-flags.patch
	paxmark.patch
	aarch64-linux-musl.patch
	fix-codegen-specs.patch
	disable-specs-using-GB2312-encoding.patch
	libressl.patch
	alpine-shards-help.patch
	"
builddir="$srcdir/$pkgname-$pkgver"

export CRYSTAL_CONFIG_VERSION="$pkgver"
export CRYSTAL_CACHE_DIR="$srcdir/.cache"

# Build static crystal binary and upload it to dev.a.o, so it can be used
# for building crystal next time (needed for bootstrapping).
# NOTE: After https://github.com/crystal-lang/crystal/issues/5689 is fixed,
# we can also use prebuilt binary from upstream if needed.
snapshot() {
	local binary="$builddir/.build/crystal"
	local tarname="$pkgname-$pkgver-$CTARGET"

	if [ ! -f "$binary" ] || ! file "$binary" | grep -qw 'statically linked'; then
		msg "Building statically linked crystal..."
		BUILD_STATIC=1 abuild clean deps unpack prepare build
	fi

	cd "$srcdir"

	strip "$builddir"/.build/crystal
	install -D -m 755 "$builddir"/.build/crystal "$tarname"/bin/crystal
	tar -czf "$tarname.tar.gz" "$tarname"

	msg "Uploading $tarname.tar.gz to dev.alpinelinux.org"
	scp "$tarname.tar.gz" dev.alpinelinux.org:/archive/$pkgname/

	APKBUILD="../APKBUILD" abuild undeps
}

prepare() {
	default_prepare
	cd "$builddir"

	cat > Makefile.local <<-EOF
		progress = 1
		threads = ${JOBS:-2}
		verbose = 1
		EXTRA_FLAGS = --verbose --target $CTARGET ${BUILD_STATIC:+"--link-flags=-no-pie"}
		LLVM_CONFIG = /usr/lib/llvm$_llvmver/bin/llvm-config
	EOF
}

build() {
	cd "$builddir"

	make crystal \
		CRYSTAL_CONFIG_PATH="lib:/usr/lib/crystal" \
		PATH="$srcdir/$pkgname-$_bootver-$CBUILD/bin:$PATH" \
		release=1 \
		static=${BUILD_STATIC:-}
}

check() {
	cd "$builddir"

	make spec PATH=".build:$PATH" SPEC_FLAGS="--no-color --verbose"
}

package() {
	cd "$builddir"

	install -D -m 755 .build/crystal "$pkgdir"/usr/bin/crystal
	install -D -m 644 man/crystal.1 "$pkgdir"/usr/share/man/man1/crystal.1

	# XXX: Move to /usr/share...?
	mkdir -p "$pkgdir"/usr/lib
	cp -r src "$pkgdir"/usr/lib/$pkgname

	cd "$pkgdir"/usr/lib/$pkgname
	rm ext/sigfault.* llvm/ext/llvm_ext.o
}

bashcomp() {
	pkgdesc="Bash completions for $pkgname"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	cd "$builddir"
	install -D -m 644 etc/completion.bash \
		"$subpkgdir"/usr/share/bash-completion/completions/$pkgname
}

zshcomp() {
	pkgdesc="ZSH completions for $pkgname"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel zsh"

	cd "$builddir"
	install -D -m 644 etc/completion.zsh \
		"$subpkgdir"/usr/share/zsh/site-functions/_$pkgname
}

sha512sums="db9fb973eeb2bc72ef91bb894b7c5aea73e727ff46e739aa431d68e556238f346a688a7a43de284afb4667b49366afce3ca423a523bea521180fa8f19ba6862d  crystal-0.24.2.tar.gz
6d7ee01ff5bfaaf79fa80c15575ae4496e17629ed18cb0577db6734266c2dbf5b0bf0a554c7c153e1b81f4c71e62bb803fd4b6513e3d039571ee4f953d85ecf4  crystal-0.24.2-x86_64-alpine-linux-musl.tar.gz
d6ed122a5ddb4e1ea772357cd3115dcf8cd92a75f973b6f7e4b8b6135fb4123fb98334495f6f312da7ab7ba3bda57bd3cb4f06c0f421df2c09d19c0ab1b8b67b  crystal-0.24.2-aarch64-alpine-linux-musl.tar.gz
2a81b6eaeb934ef5d4435a80a346425a760a602d04bccc89068dde7ed48a6099c4ea59a291f444127e91e81acde0ee42d857481041869cf6094a2e29f0de184a  makefile-shell.patch
2f995903093da9af4263bf2d7fde75715a114b02b74ba26c8290d7b20042be8a2db305a2ea144925f054354f6f6a430750d19fa036bf764562dedb56da12866a  fix-makefile-stderr-redirect.patch
d1641f538cb1ae9f817b57e19800b0af91e3723c752fba38b2644b8acc66d80862c6bef22194f1f4c64ae7d8393c2772f0a0ea0a76a199151ef8c74c686ea49f  makefile-allow-extra-flags.patch
dec4ed7abfd215eec825bed97b1e40bf3aa8bd959a4ec7153ea1bc51d409d892ebec19ddb0372e3a543e74c7018aca0c1f3cf956fa1d95efb946c8c3417c8749  paxmark.patch
0252e850b79d622fe69329fac3653128b1cc0108961c928a9efa8fa5df1bce13692b6a520697c45c37791a9e6547d5ec7f0f11905e8299902055ab3dbdfd7556  aarch64-linux-musl.patch
702927a86c2e934511415e97093fb6fe04e2e63dd5a4e2e2b1fcc772c7f02e037f1b05e516943222f282515fc562618a83442e2d53cc899933a2b2e8ad8dd53d  fix-codegen-specs.patch
269976fbc990dde075769e651c9fac6e7e0b39681e55cbd067c6d2a790c58a096bdc9981f62ae69b5217623025ba768daa63bc5316bd8ddd18192d88839aedcb  disable-specs-using-GB2312-encoding.patch
b5c9c03ad6b648759262a896da3dfbe530ea2d626a5d18fbade08af14a32f8556cdcaf593504db405ff6e5772d3bf00d9d1a8af82f677436602c5bbcbf69b1c6  libressl.patch
2aa476a2077679a72b94c220bbca6a157555f4751227cb34da1f67b1f8a9a549e00ccb2a8b939ee516f31a429f25b67dbb048984ec49a0c01f092385651daf32  alpine-shards-help.patch"
