SUMMARY="A powerful, fast, light-weight, embeddable scripting language"
DESCRIPTION="Lua combines simple procedural syntax with powerful data \
description constructs based on associative arrays and extensible semantics. \
Lua is dynamically typed, runs by interpreting bytecode for a register-based \
virtual machine, and has automatic memory management with incremental garbage \
collection, making it ideal for configuration, scripting, and rapid \
prototyping."
HOMEPAGE="https://www.lua.org/"
COPYRIGHT="1994-2020 Lua.org, PUC-Rio"
LICENSE="MIT"
REVISION="1"
SOURCE_URI="http://www.lua.org/ftp/lua-$portVersion.tar.gz"
CHECKSUM_SHA256="4ba786c3705eb9db6567af29c91a01b81f1c0ac3124fdbf6cd94bdd9e53cca7d"
SOURCE_DIR="lua-$portVersion"
PATCHES="lua54-$portVersion.patchset"
if [ $effectiveTargetArchitecture = x86_gcc2 ]; then
	PATCHES+="
		lua54-$portVersion.gcc2.patchset
	"
fi

ARCHITECTURES="?all"
SECONDARY_ARCHITECTURES="?x86_gcc2 ?x86"

libVersion="${portVersion%.*}"
libVersionCompat="$libVersion compat >= $libVersion"
luaInc="`echo ${libVersion} | tr -d '.'`"
luaVer="`echo $portVersion | cut -d~ -f1`"

PROVIDES="
	lua54$secondaryArchSuffix = $portVersion compat >= 5.4
	cmd:lua$secondaryArchSuffix = $portVersion compat >= 5.4
	cmd:lua$libVersion$secondaryArchSuffix = $portVersion compat >= 5.4
	cmd:luac$secondaryArchSuffix = $portVersion compat >= 5.4
	cmd:luac$libVersion$secondaryArchSuffix = $portVersion compat >= 5.4
	lib:liblua$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	"

PROVIDES_devel="
	lua54${secondaryArchSuffix}_devel = $portVersion compat >= 5.4
	devel:liblua$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES_devel="
	lua54$secondaryArchSuffix == $portVersion base
	"
CONFLICTS_devel="
	lua${secondaryArchSuffix}_devel
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	"
BUILD_PREREQUIRES="
	cmd:gcc$secondaryArchSuffix
	cmd:make
	"

BUILD()
{
	make haiku MYCFLAGS="-DLUA_ROOT='\"$prefix/\"' -DLUA_LDIR='\"$dataDir/lua/$libVersion/\"'" \
		LUA_T="lua$libVersion" LUAC_T="luac$libVersion" LUA_SO="liblua.so.$libVersion"
}

INSTALL()
{
	make install INSTALL_TOP="$prefix" INSTALL_MAN="$manDir/man1" \
		INSTALL_INC="$includeDir/lua$luaInc" INSTALL_LMOD="$dataDir/lua/$libVersion" \
		INSTALL_LIB="$libDir" INSTALL_BIN="$binDir" TO_BIN="lua$libVersion luac$libVersion" \
		TO_LIB="liblua.a liblua.so.$libVersion"

	ln -s lua$libVersion $binDir/lua
	ln -s luac$libVersion $binDir/luac
	ln -s liblua.so.$libVersion $libDir/liblua.so

	rm -r $dataDir #empty directory
	rm -r $prefix/lib/lua #empty directory

	mkdir -p $developLibDir/pkgconfig
	cat > $developLibDir/pkgconfig/lua.pc << EOF
V=$libVersion
R=$luaVer

INSTALL_BIN=$binDir
INSTALL_INC=$includeDir
INSTALL_LIB=$libDir
INSTALL_MAN=$manDir
INSTALL_LMOD=${dataDir}/lua/\${V}
INSTALL_CMOD=${libDir}/lua/\${V}
exec_prefix=$prefix
libdir=$libDir
includedir=$includeDir

Name: Lua
Description: An Extensible Extension Language
Version: \${R}
Requires:
Libs: -L\${libdir} -llua
Cflags: -I\${includedir}/lua$luaInc
EOF

	ln -s $developLibDir/pkgconfig/lua.pc $developLibDir/pkgconfig/lua$libVersion.pc

	prepareInstalledDevelLib liblua
	packageEntries devel $developDir
}

TEST()
{
	src/lua$libVersion -v
}
