#!/bin/bash

# Slackware build script for oolite

# Copyright 2026 - 2026 Antonio Leal, Porto Salvo, Oeiras, Portugal
# Copyright 2016 - 2022 Johannes Schoepfer, Germany
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Modified by the SlackBuilds.org project.

# NOTE: the source zip is self-contained (all git submodules, including the
# SpiderMonkey JS engine, are bundled). Nothing is downloaded during the build.

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

PRGNAM=oolite
VERSION=${VERSION:-1.92.1}
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}

# Upstream only ships a prebuilt static SpiderMonkey for x86_64
# (deps/Linux-deps/x86_64/mozilla/libjs_static.a), and GNUmakefile
# hardcodes that path.
if [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
else
  echo "$PRGNAM: ARCH $ARCH is not supported, only x86_64." >&2
  exit 1
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-source-$VERSION
# the zip has no top-level directory
mkdir $PRGNAM-source-$VERSION
cd $PRGNAM-source-$VERSION
unzip -q $CWD/$PRGNAM-source-$VERSION.zip
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# Make sure only system libs will be used (the bundled static
# SpiderMonkey in deps/Linux-deps/x86_64/mozilla is still required)
rm -f deps/Linux-deps/x86*/lib/*
rm -rf deps/Linux-deps/include/*

chmod +x tools/mkmanifest.sh ShellScripts/common/*.sh
source /usr/share/GNUstep/Makefiles/GNUstep.sh
export GNUSTEP_MAKEFILES=/usr/share/GNUstep/Makefiles
sed -i \
  -e "/ADDITIONAL_OBJCFLAGS.*-DLINUX -DXP_UNIX/s/+=/+= ${SLKCFLAGS}/" \
  -e "/ADDITIONAL_CFLAGS.*-DLINUX/s/+=/+= ${SLKCFLAGS}/" \
  GNUmakefile

# VER_FULL is normally derived from git metadata, which the release zip
# does not contain, so set it explicitly.
make -f Makefile release-deployment VER_FULL=$VERSION

mkdir -p $PKG/usr/share/oolite
cp -a oolite.app/. $PKG/usr/share/oolite/
# We use our own launcher; drop upstream's wrapper and its prebuilt
# splash-screen binary.
rm -f $PKG/usr/share/oolite/{run_oolite.sh,splash-launcher}

mkdir -p $PKG/usr/share/applications
sed -e 's/\r$//' -e 's/^Exec=.*/Exec=oolite/' \
  installers/FreeDesktop/space.oolite.Oolite.desktop \
  > $PKG/usr/share/applications/space.oolite.Oolite.desktop
install -D -m 644 Resources/Binary/Textures/oolite-logo1.png \
  $PKG/usr/share/icons/hicolor/256x256/apps/space.oolite.Oolite.png
mkdir -p $PKG/usr/share/pixmaps
ln -s ../icons/hicolor/256x256/apps/space.oolite.Oolite.png \
  $PKG/usr/share/pixmaps/space.oolite.Oolite.png

find $PKG -type f -exec file {} + | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/games
cat << 'EOF' > $PKG/usr/games/oolite
#!/bin/bash
source /usr/share/GNUstep/Makefiles/GNUstep.sh
[ ! -d ~/.Oolite/AddOns ] && mkdir -p ~/.Oolite/AddOns
[ ! -d ~/GNUstep/Library/ApplicationSupport/Oolite/ManagedAddOns ] && \
  mkdir -p ~/GNUstep/Library/ApplicationSupport/Oolite/ManagedAddOns
exec /usr/share/oolite/oolite "$@"
EOF
chmod 755 $PKG/usr/games/oolite

mkdir -p $PKG/usr/man/man6
cp debian/oolite.6 $PKG/usr/man/man6
find $PKG/usr/man -type f -exec gzip -9 {} +
for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  README.md LICENSE.md Doc/{*.txt,*.TXT,*.pdf} \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

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

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