#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# lua.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
#
# Written by Menno Duursma
# Modified by the SlackBuilds.org project
# Modified by Aaron W. Hsu
# Updated by Matteo Bernardini
# Upgraded by Patrick Volkerding

# This program is free software. It comes without any warranty.
# Granted WTFPL, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Detect whether we're building for /patches:
# This function sets the variable 'SLACKPATCHING'
slack_findpkgstore_is_stablerelease

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar xf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || failextract
slackhousekeeping

# Also search paths under /usr/local:
cat $CWD/lua.usr.local.patch | patch -p1 --verbose || exit 1

# Fix luaconf.h header to use lib$LIBDIRSUFFIX:
sed -i "s|lib/lua|lib$LIBDIRSUFFIX/lua|" src/luaconf.h

# Build:
make linux \
  MYCFLAGS="$SLKCFLAGS" \
  INSTALL_TOP=/usr \
  INSTALL_LIB=/usr/lib${LIBDIRSUFFIX} \
  INSTALL_LMOD=/usr/share/lua/$(echo $VERSION | cut -f 1-2 -d .) \
  INSTALL_CMOD=/usr/lib${LIBDIRSUFFIX}/lua/$(echo $VERSION | cut -f 1-2 -d .) || failmake

# Install into package:
make linux install \
  MYCFLAGS="$SLKCFLAGS" \
  INSTALL_TOP=$PKG/usr \
  INSTALL_LIB=$PKG/usr/lib${LIBDIRSUFFIX} \
  INSTALL_LMOD=$PKG/usr/share/lua/$(echo $VERSION | cut -f 1-2 -d .) \
  INSTALL_CMOD=$PKG/usr/lib${LIBDIRSUFFIX}/lua/$(echo $VERSION | cut -f 1-2 -d .) || failinstall

# Now let's build the shared library
mkdir -p shared
cd shared
  ar -x $PKG/usr/lib${LIBDIRSUFFIX}/liblua.a || failinstall
  gcc -ldl -lreadline -lhistory -lncurses -lm -shared -Wl,-soname,liblua.so.$(echo $VERSION | cut -f 1 -d .) *.o -o liblua.so.$VERSION || failmake
  cp -a liblua.so.$VERSION $PKG/usr/lib${LIBDIRSUFFIX} || failinstall
  ( cd $PKG/usr/lib${LIBDIRSUFFIX}
    ln -vfs liblua.so.$VERSION liblua.so.$(echo $VERSION | cut -f 1-2 -d .)
    ln -vfs liblua.so.$VERSION liblua.so.$(echo $VERSION | cut -f 1 -d .)
    ln -vfs liblua.so.$VERSION liblua.so
  )
cd ..

## On second thought, this *might* be useful
## Don't ship the static library:
#rm -f $PKG/usr/lib${LIBDIRSUFFIX}/liblua.a

# and install the pkgconfig file
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig
cat $CWD/lua.pc | sed "s/%V%/$(echo $VERSION | cut -f 1-2 -d .)/g" | sed "s/%R%/$VERSION/g" | sed "s/%LIBDIRSUFFIX%/$LIBDIRSUFFIX/g" > $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig/lua.pc

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION/{extras,html}
cp -fav \
  COPYRIGHT* HISTORY* INSTALL* README* \
  $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav doc/*.html doc/logo.gif doc/lua.css $PKG/usr/doc/$PKGNAM-$VERSION/html
cp -fav etc test $PKG/usr/doc/$PKGNAM-$VERSION/extras
#changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall        # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i      # compress man & info pages and delete usr/info/dir
slackslack           # set standard Slackware file/dir permissions and ownerships
slackdesc            # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
