#!/bin/sh
# Copyright (c) 2018-2021 Andrew Strong, Blue Mountains, Australia.
#
#   Permission to use, copy, modify, and distribute this software for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS 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.
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild script 
# ===========================
# By:        Andrew Strong <andrew.david.strong@gmail.com>
# For:       dxx-rebirth (git snapshot)
# Descr:     'Rebirth' of Descent 1 and Descent 2!!
# URL:       https://www.dxx-rebirth.com/
# Requires:  1. physfs (available on SBo): Tested with version 3.0.2
#            2. https://www.dxx-rebirth.com/download/dxx/res/d1xr-hires.dxa
#            3. https://www.dxx-rebirth.com/download/dxx/res/d1xr-sc55-music.dxa         
#            4. https://www.dxx-rebirth.com/download/dxx/res/d2xr-sc55-music.dxa
#            (The 'hires' graphics and the 2 music files really transform the games!!)
# Slack:     Tested under slackware -current
# Latest:    October 10th 2021: New git snapshot...
#            
# -----------------------------------------------------------------------------

PRGNAM=dxx-rebirth

# I am using the date convention 'yyyymmdd', this being the date of the git
# snapshot. So this version is taken from git October 10th 2021.
VERSION=${VERSION:-20211010}

# This variable is the long hash for the git commit from which the tarball
# was drawn, done with 'git rev-parse HEAD' from the git source:
COMMIT=b42ab2aa2ff1ae1ddf43b872a1d1b1a171590d5d

BUILD=${BUILD:-1}
TAG=${TAG:-_ads}

NUMJOBS=${NUMJOBS:-" -j$(expr $(nproc) + 1) "}

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

CWD=$(pwd)
TMP=${TMP:-/tmp/ads}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

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

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$COMMIT
tar xvf $CWD/$PRGNAM-$COMMIT.tar.gz
cd $PRGNAM-$COMMIT

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 {} \;

# Build both d1x-rebirth and d2x-rebirth:

scons $NUMJOBS install DESTDIR=$PKG \
       CXXFLAGS="$SLKCFLAGS" \
       verbosebuild=yes \
       prefix=/usr \
       d1x_sharepath=/usr/share/games/d1x-rebirth \
       d2x_sharepath=/usr/share/games/d2x-rebirth \
       screenshot=png \
       sdl2=yes

# Create the 2 'sharepaths' for multi-user system game content files, remembering
# that user-level files can go in ~/.d1x-rebirth and ~/.d2x-rebirth:
mkdir -p $PKG/usr/share/games/d1x-rebirth/{demos,missions}
mkdir -p $PKG/usr/share/games/d2x-rebirth/{demos,missions}

# Copy the extra music and 'hires' files to the correct locations:
cp $CWD/{d1xr-hires.dxa,d1xr-sc55-music.dxa} $PKG/usr/share/games/d1x-rebirth
cp $CWD/d2xr-sc55-music.dxa $PKG/usr/share/games/d2x-rebirth

# Desktop files and icons are available, so we will install them:
mkdir -p $PKG/usr/share/pixmaps
mv d1x-rebirth/d1x-rebirth.xpm d2x-rebirth/d2x-rebirth.xpm $PKG/usr/share/pixmaps
mkdir -p $PKG/usr/share/applications/
mv d1x-rebirth/d1x-rebirth.desktop d2x-rebirth/d2x-rebirth.desktop $PKG/usr/share/applications/

# Strip the 2 executables:
strip --strip-unneeded $PKG/usr/bin/{d1x-rebirth,d2x-rebirth}

# The documentation has been organised into 3 directories, I hope somebody will
# actually read these docs now that I have gone to this extra, pedantic trouble:)

# Create a place to store three sets of docs:
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION/{d1x-rebirth-$VERSION,d2x-rebirth-$VERSION}

# First store the d1x-rebirth docs:
cp -a \
  d1x-rebirth/{CHANGELOG.txt,INSTALL.txt,RELEASE-NOTES.txt,d1x.ini} \
  $PKG/usr/doc/$PRGNAM-$VERSION/d1x-rebirth-$VERSION

# Next store the d2x-rebirth docs:
cp -a \
  d2x-rebirth/{CHANGELOG.txt,INSTALL.txt,RELEASE-NOTES.txt,d2x.ini} \
  $PKG/usr/doc/$PRGNAM-$VERSION/d2x-rebirth-$VERSION

# Finally store the generic dxx-rebirth docs:
cp -a COPYING.txt GPL-3.txt $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
cat $CWD/doinst.sh > $PKG/install/doinst.sh

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