# 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=lua51
version=5.1.5
release=2
source=(http://www.lua.org/ftp/lua-$version.tar.gz
  lua.diff lua-5.1-cflags.diff)

build() {
  cd lua-$version

  patch -p1 -i $SRC/lua.diff
  patch -p1 -i $SRC/lua-5.1-cflags.diff
  export CFLAGS="$CFLAGS -fPIC"

  sed \
    -e 's|llua|llua5.1|' \
    -e 's|/include|/include/lua5.1|' \
    -i etc/lua.pc

  sed -r \
    -e '/^LUA_(SO|A|T)=/ s/lua/lua5.1/' \
    -e '/^LUAC_T=/ s/luac/luac5.1/' \
    -i src/Makefile

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

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

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

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

  install -m 0644 -D etc/lua.pc $PKG/usr/lib/pkgconfig/lua5.1.pc

  # fixups
  ln -s liblua5.1.so $PKG/usr/lib/liblua.so.5.1
  ln -s liblua5.1.so $PKG/usr/lib/liblua.so.$version
  cd $PKG/usr/share/man/man1
  mv lua.1 lua5.1.1
  mv luac.1 luac5.1.1
}
