# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=lua-dromozoa-utf8
_pkgname=dromozoa-utf8
pkgver=1.4
pkgrel=0
pkgdesc="Lua 5.3 compatible pure-Lua UTF-8 implementation"
url="https://github.com/dromozoa/dromozoa-utf8/"
arch="noarch"
license="GPL-3.0"
depends=""
checkdepends=""
source="$pkgname-$pkgver.tar.gz::https://github.com/dromozoa/$_pkgname/archive/v$pkgver.tar.gz"
builddir="$srcdir/$_pkgname-$pkgver"

# luajit is not available for selected arches
case "$CARCH" in
	s390x) _luajit="";;
	*) checkdepends="$checkdepends luajit" _luajit="jit";;
esac

_luaversions="5.1 5.2 5.3"
for _v in $_luaversions; do
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
	checkdepends="$checkdepends lua$_v"
done

check() {
	local out
	cd "$builddir"

	local lver; for lver in $_luaversions $_luajit; do
		msg "Testing on lua$lver"
		out=$(lua$lver test.lua test.exp 2>&1) || {
			echo "Tests failed, printing last 30 lines of the output..."
			printf '%s\n' "$out" | tail -n 30
			return 1
		}
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	local lmod_dir="$subpkgdir/usr/share/lua/$lver"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_pkgname/$pkgver-1"

	cd "$builddir"
	mkdir -p "$lmod_dir"
	cp -r dromozoa "$lmod_dir"/

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="ce3440458b3b2e6e32df7279d037ce2e35df19853e08fc8b23a5a68898aa6bd0b26ffbf29eff68684493e38f852f44dce69fdff3de0156b20af021337770c035  lua-dromozoa-utf8-1.4.tar.gz"
