# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua5.1
_pkgname=lua
pkgver=5.1.5
_luaver=${pkgname#lua}
pkgrel=6
pkgdesc="A powerful light-weight programming language designed for extending applications."
url="https://www.lua.org/"
arch="all"
license="MIT"
depends_dev="$pkgname"
makedepends="libtool autoconf automake"
provides="lua"
provider_priority=100
replaces="lua"  # for backward compatibility
subpackages="$pkgname-dev $pkgname-doc $pkgname-libs"
source="https://www.lua.org/ftp/lua-$pkgver.tar.gz
	lua-5.1-make.patch
	lua-5.1-module_paths.patch
	lua-5.1-readline.patch
	"
builddir="$srcdir/lua-$pkgver"

prepare() {
	default_prepare

	cd "$builddir"

	# we want packages to find our things
	sed -i 's:/usr/local:/usr:' etc/lua.pc

	# correct lua versioning
	sed -i 's/\(LIB_VERSION = \).*/\16:4:1/' src/Makefile

	# we use libtool
	cat > configure.ac <<-EOF
		top_buildir=.

		AC_INIT(src/luaconf.h)
		AC_PROG_LIBTOOL
		AC_OUTPUT()
	EOF

	libtoolize --force --install && aclocal && autoconf
}

build() {
	cd "$builddir"

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr

	cd "$builddir"/src
	make CFLAGS=" -DLUA_USE_LINUX $CFLAGS" \
		RPATH="/usr/lib" \
		LIB_LIBS="-lpthread -lm -ldl" \
		V=$pkgver \
		alpine_all
}

package() {
	cd "$builddir"

	make V=$pkgver \
		INSTALL_TOP="$pkgdir/usr" \
		INSTALL_INC="$pkgdir/usr/include" \
		INSTALL_LIB="$pkgdir/usr/lib" \
		alpine_install

	local i; for i in "$pkgdir"/usr/bin/*; do
		mv "$i" "$i"$_luaver
	done

	install -D -m 644 etc/lua.pc "$pkgdir"/usr/lib/pkgconfig/lua$_luaver.pc
	install -D -m 644 doc/lua.1 "$pkgdir"/usr/share/man/man1/lua$_luaver.1
	install -D -m 644 doc/luac.1 "$pkgdir"/usr/share/man/man1/luac$_luaver.1

	# Create symlinks without version suffix.
	ln -s lua$_luaver "$pkgdir"/usr/bin/lua
	ln -s luac$_luaver "$pkgdir"/usr/bin/luac
	ln -s lua$_luaver.pc "$pkgdir"/usr/lib/pkgconfig/lua.pc
	ln -s lua$_luaver.1 "$pkgdir"/usr/share/man/man1/lua.1
	ln -s luac$_luaver.1 "$pkgdir"/usr/share/man/man1/luac.1
}

dev() {
	default_dev
	provides="lua-dev"
	replaces="lua-dev"  # for backward compatibility
}

doc() {
	default_doc
	provides="lua-doc"
	replaces="lua-doc"  # for backward compatibility
}

libs() {
	pkgdesc="Lua dynamic library runtime"
	provides="lua-libs"
	replaces="lua lua-libs"  # for backward compatibility

	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/lib "$subpkgdir"/usr/
}

sha512sums="0142fefcbd13afcd9b201403592aa60620011cc8e8559d4d2db2f92739d18186860989f48caa45830ff4f99bfc7483287fd3ff3a16d4dec928e2767ce4d542a9  lua-5.1.5.tar.gz
ec5945f9f73d87fceaaa2418f1dc5c0f1e2ab1392e3a110e9ca737bfd122a951543899cd9b6170771374c35de8dd106f7b51ba9885eae281241c79b47ee58370  lua-5.1-make.patch
6f9ad12b48ce250d23ed5d91b2feb2db43c405efc30df8ea1c818a83d0a4b36d934e8629224a9a3ca575f9bad1337ed4a97624884e4e2bab9b73e3aed5c20c64  lua-5.1-module_paths.patch
a99154258419dc2e582804eae97517687e5b8b5f731dc91722193d1ff470b8522af6ff4e7142c9258afe4734cd52e6987d3c2108ac52b1487a262f1e89f2f332  lua-5.1-readline.patch"
