# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua5.2
pkgver=5.2.4
_luaver=${pkgname#lua}
pkgrel=13
pkgdesc="Powerful light-weight programming language"
url="https://www.lua.org/"
arch="all"
license="MIT"
ldpath="/usr/lib/$pkgname"
makedepends="libtool autoconf automake"
provides="lua"
provider_priority=200
subpackages="$pkgname-dbg $pkgname-dev $pkgname-doc $pkgname-libs"
source="https://www.lua.org/ftp/lua-$pkgver.tar.gz
	lua-5.2-make.patch
	lua-5.2-module_paths.patch
	"
builddir="$srcdir"/lua-$pkgver

prepare() {
	default_prepare

	# disable readline
	sed -i -e '/#define LUA_USE_READLINE/d' src/luaconf.h

	# 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() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr
	cd src
	make V=$pkgver \
		CFLAGS="$CFLAGS -DLUA_USE_LINUX -DLUA_COMPAT_ALL" \
		SYSLDFLAGS="$LDFLAGS" \
		RPATH="/usr/lib" \
		LIB_LIBS="-lpthread -lm -ldl" \
		alpine_all
}

check() {
	make test
}

package() {
	make V=$pkgver \
		INSTALL_TOP="$pkgdir"/usr \
		INSTALL_INC="$pkgdir"/usr/include/$pkgname \
		INSTALL_LIB="$pkgdir"/usr/lib/$pkgname \
		alpine_install

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

	for i in "$pkgdir"/usr/lib/$pkgname/*.so.*; do
		ln -s $pkgname/${i##*/} "$pkgdir"/usr/lib/${i##*/}
	done

	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

	mkdir -p "$pkgdir"/usr/share/doc/$pkgname/
	install -m644 doc/*.html doc/*.css doc/logo.gif doc/osi-certified-72x60.png \
		"$pkgdir"/usr/share/doc/$pkgname/

	install -d "$pkgdir"/usr/lib/pkgconfig
	cat > "$pkgdir"/usr/lib/pkgconfig/lua$_luaver.pc <<EOF
# lua.pc -- pkg-config data for Lua

# vars from install Makefile

# grep '^V=' ../Makefile
V= ${_luaver}
# grep '^R=' ../Makefile
R= ${pkgver}

# grep '^INSTALL_.*=' ../Makefile | sed 's/INSTALL_TOP/prefix/'
prefix= /usr
INSTALL_BIN= \${prefix}/bin
INSTALL_INC= \${prefix}/include
INSTALL_LIB= \${prefix}/lib
INSTALL_MAN= \${prefix}/man/man1
INSTALL_LMOD= \${prefix}/share/lua/\${V}
INSTALL_CMOD= \${prefix}/lib/lua/\${V}

# canonical vars
exec_prefix=\${prefix}
libdir=\${exec_prefix}/lib/$pkgname
includedir=\${prefix}/include/$pkgname

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

# (end of lua$_luaver.pc)
EOF

	# compat names since we are unique
	# other distros name it lua-5.2 or lua52. pick the former as a compat name..
	ln -sv lua5.2.pc "$pkgdir"/usr/lib/pkgconfig/lua-5.2.pc
}

dev() {
	depends_dev=$pkgname
	default_dev
	mkdir -p "$subpkgdir"/usr/$pkgname "$subpkgdir"/usr/lib/$pkgname
	ln -s ../include/$pkgname "$subpkgdir"/usr/$pkgname/include
	ln -s ../lib/$pkgname "$subpkgdir"/usr/$pkgname/lib
	mv "$pkgdir"/usr/lib/$pkgname/liblua.so "$subpkgdir"/usr/lib/$pkgname/
}

libs() {
	pkgdesc="Lua dynamic library runtime"
	replaces="lua"
	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/lib "$subpkgdir"/usr/
}

sha512sums="
cd77148aba4b707b6c159758b5e8444e04f968092eb98f6b4c405b2fb647e709370d5a8dcf604176101d3407e196a7433b5dcdce4fe9605c76191d3649d61a8c  lua-5.2.4.tar.gz
f1d5e0a1db0790fae82dd6de5742631c6c4ca009752630a612d864746990a00c8f2eacc7dbf07be2bce94d04da4ad185ed10dc317d7bf093df698bf84fea682e  lua-5.2-make.patch
ca11e6b92acbdb3430faeeaa45fe1326043c3fff19da22a5d22ca38bff98635df3e33266046b1bf02302090f222dd5ab28759b4ed587da26ceb6e1dae6859829  lua-5.2-module_paths.patch
"
