# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua-stringy
pkgver=0.5.0
_pkgver=${pkgver%.*}-${pkgver##*.}
pkgrel=0
pkgdesc="Lua string utility library"
url="https://github.com/brentp/lua-stringy/"
arch="all"
license="MIT"
checkdepends=""
makedepends=""
subpackages=""
source="$pkgname-$pkgver.tar.gz::https://github.com/brentp/$pkgname/archive/v$_pkgver.tar.gz
	makefile.patch"
builddir="$srcdir/$pkgname-$_pkgver/stringy"

# luajit is not available on s390x.
case "$CARCH" in
	s390x) _luajit="";;
	*) checkdepends="$checkdepends luajit" _luajit="jit";;
esac

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

prepare() {
	default_prepare

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."
		make -C "$builddir-$lver" \
			CC="${CC:-gcc}" \
			LUA_PC="lua$lver" \
			LUA="lua$lver"
	done
}

check() {
	local lver; for lver in $_luaversions $_luajit; do
		msg "Testing on lua$lver..."
		cd "$builddir-${lver/jit/5.1}"
		lua$lver stringy_test.lua
	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 cmoddir="$subpkgdir/$(pkg-config --variable=INSTALL_CMOD lua$lver)"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$pkgname/$_pkgver"

	install -m 755 -D "$builddir-$lver"/stringy.so "$cmoddir"/stringy.so

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

sha512sums="899cd7921e7dcbc12c1b9d5891637566af7e9a0330b75e2bf31f83447618e981e7a2e88da065aca7525bd564e9d04d720b6f7138edea78007a8fc972dc5f59a2  lua-stringy-0.5.0.tar.gz
c3a8a6705e1b39a99e117f57e49263f676905775452787a1c8dc5a30eda51bc652d9bfe6c975bcb4ded9cb60a6bf70f6654dd31575f22f932be58474de01efba  makefile.patch"
