#!/bin/bash

# Slackware build script for LÖVE (legacy 0.8 version)

# Written by Dugan Chen (thedoogster [at] gmail [dot] com)

# Modified for 0.8.0 legacy version by B. Watson (urchlay@slackware.uk)

# If you're packaging up a LÖVE game that needs this version (won't work
# with 0.9.x), make sure the shebang line in the .love file says:
#  #!/usr/bin/env love-0.8.0
# See mari0.SlackBuild for an example.

# 20241107 bkw: BUILD=3, use lua51 (because this won't build with luajit-2.1.x)
# 20241018 bkw: BUILD=2, REQUIRES=luajit instead of lua.
# 20210903 bkw: fix build for current/pre15.

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

PRGNAM=love-legacy080
VERSION=${VERSION:-0.8.0}
BUILD=${BUILD:-3}
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"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

SRCNAM=love

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION-linux-src.tar.gz
cd $SRCNAM-$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 {} \+

# this is the same patch as love-legacy072
zcat $CWD/newmesafix.diff.gz | patch -p1

SLKCFLAGS+=" $(pkg-config --cflags lua51)"

LDFLAGS="$(pkg-config --libs lua51)" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --program-suffix=-0.8.0 \
  --without-luajit \
  --build=$ARCH-slackware-linux

make
make install-strip DESTDIR=$PKG

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a changes.txt license.txt readme.md $PKG/usr/doc/$PRGNAM-$VERSION
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
