#!/bin/bash

# Slackware build script for Lua

# Written by Menno Duursma
# Modified by the SlackBuilds.org project
# Modified by Aaron W. Hsu
# Updated by Matteo Bernardini
# Updated (for lua-5.4) by B. Watson, based on Patrick Volkerding's
# lua.SlackBuild for Slackware-current.

# 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.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=lua
VERSION=${VERSION:-5.4.7}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
fi

set -e

# 20241017 bkw: this stuff makes the code slightly less eye-hurting.
SHORTVER="$( echo $VERSION | cut -d. -f1,2 )"   # e.g. 5.4
VERMAJOR="$( echo $VERSION | cut -d. -f1   )"   # e.g. 5
LIB=lib$LIBDIRSUFFIX
ULIB=/usr/$LIB
PKGLIB=$PKG$ULIB

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-${VERSION}.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} + -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} +

# Also search paths under /usr/local:
patch -p1 < $CWD/lua.usr.local.patch

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

make linux \
  MYCFLAGS="$SLKCFLAGS" \
  INSTALL_TOP=/usr \
  INSTALL_LIB=$ULIB \
  INSTALL_LMOD=/usr/share/lua/$SHORTVER \
  INSTALL_CMOD=$ULIB/lua/$SHORTVER

make linux install \
  MYCFLAGS="$SLKCFLAGS" \
  INSTALL_TOP=$PKG/usr \
  INSTALL_LIB=$PKGLIB \
  INSTALL_LMOD=$PKG/usr/share/lua/$SHORTVER \
  INSTALL_CMOD=$PKGLIB/lua/$SHORTVER

strip $PKG/usr/bin/*

# Now let's build the shared library
mkdir -p shared
cd shared
  ar -x $PKGLIB/liblua.a
  gcc -ldl -lreadline -lhistory -lncurses -lm -shared *.o -o liblua.so.$VERSION
  install -s -m0755 liblua.so.$VERSION $PKGLIB
  ( cd $PKGLIB
    ln -s liblua.so.$VERSION liblua.so.$SHORTVER
    ln -s liblua.so.$VERSION liblua.so.$VERMAJOR
    ln -s liblua.so.$VERSION liblua.so
  )
cd ..

# and install the pkgconfig file
mkdir -p $PKGLIB/pkgconfig
sed -e "s/%V%/$SHORTVER/" \
    -e "s/%R%/$VERSION/" \
    -e "s/%LIBDIRSUFFIX%/$LIBDIRSUFFIX/g" \
    < $CWD/lua.pc \
    > $PKGLIB/pkgconfig/lua.pc

gzip -9 $PKG/usr/man/man*/*

# 20241017 bkw: here is the only real difference between this and PV's:
# we don't create an empty extras/ under the doc dir.
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION/html
cp -a README* $PKG/usr/doc/$PRGNAM-$VERSION
cp -a doc/*.html doc/logo.gif doc/lua.css $PKG/usr/doc/$PRGNAM-$VERSION/html
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
