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-2016 Roberto Ierusalimschy, Waldemar Celes, Luiz Henrique de \
Figueiredo"
LICENSE="MIT"
REVISION="1"
SOURCE_URI="https://www.lua.org/ftp/lua-$portVersion.tar.gz"
CHECKSUM_SHA256="0c2eed3f960446e1a3e4b9a1ca2f3ff893b6ce41942cf54d5dd59ab4b3b058ac"
PATCHES="lua-$portVersion.patchset"
if [ $effectiveTargetArchitecture = x86_gcc2 ]; then
	PATCHES+="
		lua-$portVersion.gcc2.patchset
	"
fi

ARCHITECTURES="all"
SECONDARY_ARCHITECTURES="x86_gcc2 x86"

PROVIDES="
	lua$secondaryArchSuffix = $portVersion compat >= 5.3
	cmd:lua$secondaryArchSuffix = $portVersion compat >= 5.3
	cmd:lua5.3$secondaryArchSuffix = $portVersion compat >= 5.3
	cmd:luac$secondaryArchSuffix = $portVersion compat >= 5.3
	cmd:luac5.3$secondaryArchSuffix = $portVersion compat >= 5.3
	lib:liblua$secondaryArchSuffix = $portVersion compat >= 5.3
	"
REQUIRES="
	haiku$secondaryArchSuffix
	"

PROVIDES_devel="
	lua${secondaryArchSuffix}_devel = $portVersion compat >= 5.3
	devel:liblua$secondaryArchSuffix = $portVersion compat >= 5.3
	"
REQUIRES_devel="
	lua$secondaryArchSuffix == $portVersion base
	"

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

BUILD()
{
	make haiku MYCFLAGS="-DLUA_USE_POSIX -DLUA_COMPAT_5_1 -DLUA_ROOT='\"$prefix/\"' -DLUA_LDIR='\"$dataDir/lua/5.3/\"'" \
		LUA_T="lua5.3" LUAC_T="luac5.3" LUA_SO="liblua.so.5.3"
}

INSTALL()
{
	make install INSTALL_TOP="$prefix" INSTALL_MAN="$manDir/man1" \
		INSTALL_INC="$includeDir/lua53" INSTALL_LMOD="$dataDir/lua/5.3" \
		INSTALL_LIB="$libDir" INSTALL_BIN="$binDir" TO_BIN="lua5.3 luac5.3" \
		TO_LIB="liblua.a liblua.so.5.3"

	ln -s lua5.3 $binDir/lua
	ln -s luac5.3 $binDir/luac
	ln -s liblua.so.5.3 $libDir/liblua.so

	rm -r $dataDir #empty directory

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

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}/lua53
EOF

	ln -s $developLibDir/pkgconfig/lua.pc $developLibDir/pkgconfig/lua5.3.pc

	prepareInstalledDevelLib liblua
	packageEntries devel $developDir
}
