#!/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
# Renamed lua => lua51 and updated by B. Watson

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

# 20241104 bkw: BUILD=2, fix doubleplusungood refs lib64 in .pc file.

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

PRGNAM=lua51
SRCNAM=lua
VERSION=${VERSION:-5.1.5}
BUILD=${BUILD:-2}
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

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz
cd $SRCNAM-$VERSION
chown -R root:root .

V=5.1
PKGBIN=$PKG/usr/bin
LIB=lib$LIBDIRSUFFIX
PKGLIB=$PKG/usr/$LIB
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
PKGMAN1=$PKG/usr/man/man1
PKGINC=$PKG/usr/include

# Fix up a to-be-installed header.
sed -i "s|/usr/local|/usr|" src/luaconf.h
sed -i "s|lib/lua|$LIB/lua|" src/luaconf.h

runmake() {
  make $1 \
    CFLAGS="$SLKCFLAGS \$(MYCFLAGS)" \
    INSTALL_TOP=$2/usr \
    INSTALL_LIB=$2/usr/$LIB \
    INSTALL_LMOD=$2/usr/share/lua/$V \
    INSTALL_CMOD=$2/usr/$LIB/lua/$V
}

runmake linux ""
runmake install $PKG

for bin in lua luac; do
  newbin=${bin}51
  mv $PKGBIN/$bin $PKGBIN/$newbin
  strip $PKGBIN/$newbin
  ln -s $newbin $PKGBIN/${bin}$V
  mv $PKGMAN1/$bin.1 $PKGMAN1/$newbin.1
done

mkdir $PKGINC/lua$V
mv $PKGINC/*.h* $PKGINC/lua$V

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

# Now let's build the shared library
mkdir -p shared
cd shared
  mv $PKGLIB/liblua.a $PKGLIB/liblua$V.a
  ar -x $PKGLIB/liblua$V.a
  gcc -ldl -lreadline -lhistory -lncurses -lm -shared *.o -o liblua$V.so.$VERSION
  strip liblua$V.so.$VERSION
  cp -a liblua$V.so.$VERSION $PKGLIB
  for ext in .so.$V .so.5 .so; do
    ln -s liblua$V.so.$VERSION $PKGLIB/liblua$V$ext
  done
cd ..

# and install the pkgconfig file
mkdir -p $PKGLIB/pkgconfig
sed "s,\<lib\>,$LIB,g" < $CWD/lua51.pc > $PKGLIB/pkgconfig/lua51.pc
ln -s lua51.pc $PKGLIB/pkgconfig/lua$V.pc

mkdir -p $PKGDOC/{extras,html}
cp -a COPYRIGHT HISTORY README $PKGDOC
cp -a doc/*.html doc/logo.gif doc/lua.css $PKGDOC/html
cp -a etc test $PKGDOC/extras
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$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
