#!/bin/bash

# Slackware build script for iortcw

# Copyright 2016-2019 Sébastien Ballet, France
# 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.

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

PRGNAM=iortcw
VERSION=${VERSION:-1.51c}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

INSTALL_COMMON_DATA=${INSTALL_COMMON_DATA:-yes}

 # When INSTALL_COMMON_DATA=yes, define which language-specific
 # patches (for single player) must be installed. Can be :
 #   no (default) : do not install any language-specific patches
 #   de           : install german language-specific patches
 #   fr           : install french language-specific patches
 #   es           : install spanish language-specific patches
 #   it           : install italian language-specific patches
 #
LANG=${LANG:-no}

 # Define whether iortcw must be build with internal libraries
 # instead of dynamically linking against system libraries. Can
 # be set to yes or no (default).
 #
 # Define whether iortcw must be or not build and linked against
 # internal libraries (ie. zlib, jpeg, ogg, opus). Can be set
 # to yes or no (default).
 #
_USE_INTERNAL_LIBS=${_USE_INTERNAL_LIBS:-no}


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

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
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=""
  BUILDARCH="x86"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
  BUILDARCH="x86"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
  BUILDARCH="x86_64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
  BUILDARCH="$ARCH"
fi

set -e

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

if [ "$INSTALL_COMMON_DATA" = "yes" ] ; then

   # Extract common game data file in ./common-game-data :
   #
   #   * game data files to extract from $CWD/patch-data-141.zip archive :
   #       main/mp_bin.pk3
   #       main/mp_pak3.pk3
   #       main/mp_pak4.pk3
   #       main/mp_pak5.pk3
   #       main/mp_pakmaps0.pk3
   #       main/mp_pakmaps1.pk3
   #       main/mp_pakmaps2.pk3
   #       main/mp_pakmaps3.pk3
   #       main/mp_pakmaps4.pk3
   #       main/mp_pakmaps5.pk3
   #       main/mp_pakmaps6.pk3
   #       main/rotate.cfg
   #       main/scripts/translation.cfg
   #       main/sp_pak3.pk3
   #       main/sp_pak4.pk3
   #
   #   * game data files to extract from $CWD/patch-data-SP_deutsch.zip when LANG=de :
   #       main/sp_pak3.pk3
   #       main/language.cfg
   #
   #   * game data files to extract from $CWD/patch-data-SP_espanol.zip when LANG=es :
   #       main/sp_pak3.pk3
   #       main/Language.cfg
   #
   #   * game data files to extract from $CWD/patch-data-SP_francais.zip when LANG=fr :
   #       main/sp_pak3.pk3
   #       main/language.cfg
   #
   #   * game data files to extract from $CWD/patch-data-SP_italiano.zip when LANG=it :
   #       main/sp_pak3.pk3
   #       main/Language.cfg

  unzip $CWD/patch-data-141.zip "main/*.pk3" "main/*.cfg"  -d ./common-game-data

  case $LANG in
    de)  unzip -o $CWD/patch-data-SP_deutsch.zip -d ./common-game-data
    ;;

    es) unzip -o $CWD/patch-data-SP_espanol.zip -d ./common-game-data
    ;;

    fr)  unzip -o $CWD/patch-data-SP_francais.zip -d ./common-game-data
    ;;

    it)  unzip -o $CWD/patch-data-SP_italiano.zip -d ./common-game-data
    ;;
  esac
fi

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

 # Reminder :
 #  * targets defined in SP/MP makefiles :
 #      debug, release, makedirs, clean-debug, clean-release
 #      clean2, toolsclean-debug, toolsclean-release, toolsclean2
 #      dist
 #
 #  * Variables used by the SP/MP makefiles and their default values :
 #
 #        Variables                !          Default values
 #                                 !    single player ! multiplayer
 #     ----------------------------+------------------+-----------------
 #       BUILD_STANDALONE          !            -     !      -
 #       BUILD_CLIENT              !            -     !      -
 #       BUILD_SERVER              !            0     !      -
 #       BUILD_GAME_SO             !            -     !      -
 #       BUILD_GAME_QVM            !            0     !      -
 #       BUILD_BASEGAME            !            -     !      -
 #       BUILD_RENDERER_REND2      !            -     !      -
 #       BUILD_ARCHIVE             !            0     !      0
 #       CLIENTBIN                 !        iowolfsp  ! iowolfmp
 #       SERVERBIN                 !      iowolfspded ! iowolfded
 #       BASEGAME                  !          main    !    main
 #       BASEGAME_CFLAGS           !           -      !      -
 #       COPYDIR                   !        /usr/local/games/wolf
 #       TEMPDIR                   !                  /tmp
 #       USE_OPENAL                !         1        !      1
 #       USE_OPENAL_DLOPEN         !         1        !      1
 #       USE_CURL                  !         0        !      0
 #       USE_CURL_DLOPEN           !         1        !      1
 #       USE_CODEC_VORBIS          !         1        !      1
 #       USE_CODEC_OPUS            !         1        !      1
 #       USE_MUMBLE                !         1        !      1
 #       USE_VOIP                  !         1        !      1
 #       USE_FREETYPE              !         1        !      1
 #       USE_INTERNAL_LIBS         !         1        !      1
 #       USE_INTERNAL_OGG          !        ${USE_INTERNAL_LIBS}
 #       USE_INTERNAL_VORBIS       !        ${USE_INTERNAL_LIBS}
 #       USE_INTERNAL_OPUS         !        ${USE_INTERNAL_LIBS}
 #       USE_INTERNAL_ZLIB         !        ${USE_INTERNAL_LIBS}
 #       USE_INTERNAL_JPEG         !        ${USE_INTERNAL_LIBS}
 #       USE_INTERNAL_FREETYPE     !        ${USE_INTERNAL_LIBS}
 #       USE_LOCAL_HEADERS         !        ${USE_INTERNAL_LIBS}
 #       USE_RENDERER_DLOPEN       !         1        !      1
 #       USE_XDG                   !         0        !      0
 #       USE_YACC                  !         0        !      0
 #       DEBUG_CFLAGS              !        -g -O0    ! -g -O0
 #       USE_BLOOM                 !         1        !      1
 #       USE_OPENGLES              !         0        !      0
 #       RASPBERRY_PI              !         0        !      0
 #       USE_AUTHORIZE_SERVER      !         0        !      0
 #       V   (ie. verbose)         !         0        !      0

[ "$_USE_INTERNAL_LIBS" = "no" ] && _USE_INTERNAL_LIBS=0 || _USE_INTERNAL_LIBS=1

 # define args that must be passed to make for better readability.
 #
 # Nota: even if BUILD_GAME_QVM is set to 1, qvm files are not
 # generated on x86_64 systems.
 #
MAKE_ARGS="ARCH=$BUILDARCH \
BUILD_CLIENT=1 \
BUILD_SERVER=1 \
BUILD_GAME_SO=1 \
BUILD_GAME_QVM=1 \
BUILD_BASEGAME=1 \
BUILD_RENDERER_REND2=1 \
USE_CURL=1 \
USE_CURL_DLOPEN=0 \
USE_OPENAL=1 \
USE_OPENAL_DLOPEN=0 \
USE_INTERNAL_LIBS=${_USE_INTERNAL_LIBS} \
USE_CODEC_VORBIS=1 \
USE_CODEC_OPUS=1 \
USE_FREETYPE=1 \
COPYDIR=\"$PKG/usr/share/games/rtcw\""

 # Build single player game binaries
cd SP
CFLAGS="$SLKCFLAGS" \
make $MAKE_ARGS release copyfiles

 # Build multi player game binaries
cd ../MP
CFLAGS="$SLKCFLAGS" \
make $MAKE_ARGS release copyfiles

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

 # go back into rtcw source root directory, generate pk3
 # files required by alternate renderer (ie. mp_rend2_shaders0.pk3
 # and sp_rend2_shaders0.pk3), in $PKG/usr/share/games/rtcw/main
 #
cd ..

(
  cd SP/media/
  zip -r $PKG/usr/share/games/rtcw/main/sp_rend2_shaders0.pk3 .
)

(
  cd MP/media/
  zip -r $PKG/usr/share/games/rtcw/main/mp_rend2_shaders0.pk3 .
)


if [ "$INSTALL_COMMON_DATA" = "yes" ] ; then
   # Copy content of ./common-game-data into $PKG/usr/share/games/rtcw
   #
  cp -a ./common-game-data/* $PKG/usr/share/games/rtcw
fi

mkdir -p $PKG/usr/share/{applications,pixmaps}
cat $CWD/iortcw.desktop > $PKG/usr/share/applications/iortcw.desktop
cat $CWD/iortcw-mp.desktop > $PKG/usr/share/applications/iortcw-mp.desktop
install -m 644 $CWD/iortcw.png $PKG/usr/share/pixmaps/iortcw.png

# Add launchers
mkdir $PKG/usr/games
cat $CWD/launcher.iortcw > $PKG/usr/games/iortcw
cat $CWD/launcher.iortcw-mp > $PKG/usr/games/iortcw-mp
chmod 0755 $PKG/usr/games/iortcw
chmod 0755 $PKG/usr/games/iortcw-mp


mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION/{SP,MP}

cp -a	HOWTO-Build.txt \
	README_RASPBERRYPI \
	iortcw-command-changes.txt \
	rend2-cvars.txt \
	iortcw-cvars-new.txt \
	rend2-readme.md \
	README.md \
	iortcw-cvars-removed.txt \
	voip-readme.txt \
  $PKG/usr/doc/$PRGNAM-$VERSION

cp -a SP/COPYING.txt SP/README.txt \
  $PKG/usr/doc/$PRGNAM-$VERSION/SP

cp -a MP/COPYING.txt MP/README.txt \
  $PKG/usr/doc/$PRGNAM-$VERSION/MP

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
