name=lua
majver=5.3
version=$majver.4
update_url="http://www.lua.org/download.html"

dlextract "http://www.lua.org/ftp/$name-$version.tar.gz" \
          '53a9c68bcc0eda58bdc2095ad5cdfc63'
header_end

cd "$name-$version"

# Fix paths
sed -i -e "/LUA_ROOT/s/usr\/local/$(safe_sed "$dir_prefix")/" \
       -e "/LUA_LDIR/s/share/$(safe_sed "$dir_datadir")/" \
       -e "/LUA_CDIR/s/lib/$(safe_sed "$dir_libdir")/" src/luaconf.h

make linux \
    MYLIBS="-lncurses" \
    MYCFLAGS="$CFLAGS -fPIC" \
    MYLDFLAGS="$LDFLAGS"

make install \
    INSTALL_TOP="$PWD/install" \
    INSTALL_BIN="\$(INSTALL_TOP)/$dir_bindir" \
    INSTALL_LIB="\$(INSTALL_TOP)/$dir_libdir" \
    INSTALL_INC="\$(INSTALL_TOP)/$dir_includedir" \
    INSTALL_MAN="\$(INSTALL_TOP)/$dir_mandir/man1" \
    INSTALL_LMOD="\$(INSTALL_TOP)/$dir_datadir/$majver" \
    INSTALL_CMOD="\$(INSTALL_TOP)/$dir_libdir/$majver"

# Check the build log to find this
libs="-lm -Wl,-E -ldl -lreadline -lncurses"

# Build the shared library
( cd "install/$dir_libdir"
    ar x liblua.a
    rm liblua.a
    cc -shared "-Wl,-soname,liblua.so.$majver" *.o $libs -o "liblua.so.$version"
    rm *.o
    ln -s "liblua.so.$version" "liblua.so.$majver"
    ln -s "liblua.so.$version" liblua.so
)

# Link the binary against the shared library
rm "install/$dir_bindir/lua"
cc src/lua.o "install/$dir_libdir/liblua.so" $libs -o "install/$dir_bindir/lua"
rm "install/$dir_bindir/luac"
cc src/luac.o src/lopcodes.o src/ldump.o "install/$dir_libdir/liblua.so" $libs -o "install/$dir_bindir/luac"

# Actually install
mkdir -p "$dir_install"
cp -Ta install "$dir_install/$dir_prefix"

# vim:set tabstop=4 shiftwidth=4 syntax=sh expandtab: