# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

setvar _luaversions = ""5.1 5.2 5.3""

setvar pkgname = 'lua-posix'
setvar _name = 'luaposix'
setvar pkgver = '33.4.0'
setvar pkgrel = '0'
setvar pkgdesc = ""POSIX library for Lua $_luaver""
setvar url = ""https://github.com/luaposix/luaposix""
setvar arch = ""all""
setvar license = ""MIT""
setvar depends = """"
setvar makedepends = ""perl automake autoconf linux-headers lua-bitlib""
setvar subpackages = ""$pkgname-doc""

for _i in $_luaversions {
	setvar makedepends = ""$makedepends lua$_i-dev lua$_i""
	setvar subpackages = ""$subpackages lua$_i-posix:_posix_${_i/./_}""
}
setvar source = ""luaposix-$pkgver.tar.gz::https://github.com/luaposix/luaposix/archive/release-v$pkgver.tar.gz
	""

setvar _sdir = ""$srcdir"/$_name-release-v$pkgver"

proc prepare {
	cd $_sdir
	for i in $source {
		case (i) {
		*.patch { msg $i; patch -p1 -i "$srcdir"/$i || return 1}
		}
	}
	aclocal && autoconf && automake --add-missing || return 1
	for _i in $_luaversions {
		cp -r $_sdir "$srcdir"/build-$_i
	}
}

proc build {
	for _i in $_luaversions {
		msg "build for Lua $_i"
		cd "$srcdir"/build-$_i"
		LUA=lua$_i" LUA_INCLUDE=$(pkg-config lua$_i --cflags) \
			./configure --prefix=/usr \
			--libdir=/usr/lib/lua/$_i \
			--datadir=/usr/share/lua/$_i \
			|| return 1
		make || return 1
	}
}

proc package {
	for _i in $_luaversions {
		cd "$srcdir"/build-$_i
		make DESTDIR="$pkgdir" luadir="/usr/share/lua/$_i" install \
			|| return 1
	}
}

proc _split {
	setvar depends = ''
	local d= _ver=$1 _f=$2
	if test $_ver != "5.2" {
		# use versioned dep to work around
		# http://bugs.alpinelinux.org/issues/4063
		setvar depends = ""lua$_ver-bitlib<26""
	}
	for d in usr/lib/lua usr/share/lua {
		if test -d "$pkgdir"/$d/$_ver {
			mkdir -p "$subpkgdir"/$d/$_ver
			mv "$pkgdir"/$d/$_ver/$_f* "$subpkgdir"/$d/$_ver/ \
				|| return 1
			rmdir "$pkgdir"/$d/$_ver 2>/dev/null || true
		}
	}
}

proc _split_posix {
	local _ver=$1
	setvar pkgdesc = ""POSIX library for Lua $_ver""
	setvar install_if = ""lua$_ver lua-posix=$pkgver-r$pkgrel""
	_split $_ver posix
}

for _i in $_luaversions {
	eval "_posix_${_i/./_}() { _split_posix $_i; }"
}

setvar md5sums = ""b36ff049095f28752caeb0b46144516c  luaposix-33.4.0.tar.gz""
setvar sha256sums = ""e66262f5b7fe1c32c65f17a5ef5ffb31c4d1877019b4870a5d373e2ab6526a21  luaposix-33.4.0.tar.gz""
setvar sha512sums = ""3cb78ccb343a55e2bf5b182410e2050356379e8f7a86d42a79853db6198628cae85f9984854f99f41505cb7e4fbb5288e4a46633a6db459649837729af76044c  luaposix-33.4.0.tar.gz""