# Description: A programming language designed for extending applications
# URL: https://www.lua.org
# Maintainer: Tim Biermann, tbier at posteo dot de
# Depends on: readline

name=lua53
version=5.3.6
release=5
source=(https://www.lua.org/ftp/lua-$version.tar.gz
  liblua.so.patch lua.pc)

build() {
  cd lua-$version

  patch -p1 -i $SRC/liblua.so.patch

  sed -e "s/%VER%/${version%.*}/g;s/%REL%/$version/g" $SRC/lua.pc > lua.pc

  sed -e 's:llua:llua5.3:' -e 's:/include:/include/lua5.3:' -i lua.pc
  sed -r \
    -e '/^LUA_(SO|A|T)=/ s/lua/lua5.3/' \
    -e '/^LUAC_T=/ s/luac/luac5.3/' \
    -i src/Makefile

  sed -i '/#define LUA_ROOT/s:/usr/local/:/usr/:' src/luaconf.h

  make MYCFLAGS="$CFLAGS -fPIC" MYLDFLAGS="$LDFLAGS" linux

  install -d $PKG/usr/include/lua5.3 $PKG/usr/share/man/man1 $PKG/usr/lib

  make \
    TO_BIN="lua5.3 luac5.3" \
    TO_LIB="liblua5.3.a liblua5.3.so liblua5.3.so.5.3 liblua5.3.so.$version" \
    INSTALL_DATA='cp -d' \
    INSTALL_TOP="$PKG/usr" \
    INSTALL_INC="$PKG/usr/include/lua5.3" \
    INSTALL_MAN="$PKG/usr/share/man/man1" \
    install

  install -m 0644 -D lua.pc $PKG/usr/lib/pkgconfig/lua${version:0:1}${version:2:1}.pc
  ln -s lua${version:0:1}${version:2:1}.pc $PKG/usr/lib/pkgconfig/lua${version:0:3}.pc
  ln -s lua${version:0:1}${version:2:1}.pc $PKG/usr/lib/pkgconfig/lua-${version:0:3}.pc

  ln -s liblua5.3.so $PKG/usr/lib/liblua.so.5.3
  ln -s liblua5.3.so $PKG/usr/lib/liblua.so.$version

  cd $PKG/usr/share/man/man1
  mv lua.1 lua5.3.1
  mv luac.1 luac5.3.1
}
