#!/bin/bash

# Slackware build script for rpcs3

# Copyright 2022-2023 Steven Voges <Oregon, USA>
# 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=rpcs3
VERSION=${VERSION:-0.0.27}
COMMITVER=${COMMITVER:-0178b20}
COMMITNUM=${COMMITNUM:-14783}
ASMJIT=${ASMJIT:-06d0badec53710a4f572cf5642881ce570c5d274}
CUBEB=${CUBEB:-70b4e3db7822de4d534959885cda109d6edbee36}
GLSLANG=${GLSLANG:-10423ec659d301a0ff2daac8bbf38980abf27590}
HIDAPI=${HIDAPI:-ecf1b62882c1b6ca1da445fa94ee8dae42cf5961}
ITTAPI=${ITTAPI:-3.18.12}
LLVM=${LLVM:-9b52b6c39ae9f0759fbce7dd0db4b3290d6ebc56}
MINIUPNP=${MINIUPNP:-f4a739d73083bee207af30b8aa3e668383ee070e}
SOUNDTOUCH=${SOUNDTOUCH:-83cfba67b6af80bb9bfafc0b324718c4841f2991}
SPIRVHEADERS=${SPIRVHEADERS:-85a1ed200d50660786c1a88d9166e871123cce39}
SPIRVTOOLS=${SPIRVTOOLS:-eb0a36633d2acf4de82588504f951ad0f2cecacb}
WOLFSSL=${WOLFSSL:-4fbd4fd36a21efd9d1a7e17aba390e91c78693b1}
YAMLCPP=${YAMLCPP:-0b67821f307e8c6bf0eba9b6d3250e3cf1441450}
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}
DEPS=$TMP/$PRGNAM-$VERSION/3rdparty

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

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

mkdir -p $TMP/$PRGNAM-$VERSION/build/3rdparty/llvm_build/ittapi
tar xvf $CWD/SPIRV-Headers-$SPIRVHEADERS.tar.gz -C \
  $DEPS/SPIRV/SPIRV-Headers --strip-components 1
tar xvf $CWD/SPIRV-Tools-$SPIRVTOOLS.tar.gz -C \
  $DEPS/SPIRV/SPIRV-Tools --strip-components 1
tar xvf $CWD/asmjit-$ASMJIT.tar.gz -C \
  $DEPS/asmjit/asmjit --strip-components 1
tar xvf $CWD/cubeb-$CUBEB.tar.gz -C \
  $DEPS/cubeb/cubeb --strip-components 1
tar xvf $CWD/glslang-$GLSLANG.tar.gz -C \
  $DEPS/glslang/glslang --strip-components 1
tar xvf $CWD/hidapi-$HIDAPI.tar.gz -C \
  $DEPS/hidapi/hidapi --strip-components 1
tar xvf $CWD/ittapi-$ITTAPI.tar.gz -C \
  $TMP/$PRGNAM-$VERSION/build/3rdparty/llvm_build/ittapi --strip-components 1
tar xvf $CWD/llvm-mirror-$LLVM.tar.gz -C \
  $DEPS/../llvm --strip-components 1
tar xvf $CWD/miniupnp-$MINIUPNP.tar.gz -C \
  $DEPS/miniupnp/miniupnp --strip-components 1
tar xvf $CWD/soundtouch-$SOUNDTOUCH.tar.gz -C \
  $DEPS/SoundTouch/soundtouch --strip-components 1
tar xvf $CWD/wolfssl-$WOLFSSL.tar.gz -C \
  $DEPS/wolfssl/wolfssl --strip-components 1
tar xvf $CWD/yaml-cpp-$YAMLCPP.tar.gz -C \
  $DEPS/yaml-cpp/yaml-cpp --strip-components 1

#Bypass git checkout check for ittapi
sed -i 's/if(NOT GIT_CHECKOUT_RESULT EQUAL "0")/if(GIT_CHECKOUT_RESULT EQUAL "0")/g' \
  llvm/lib/ExecutionEngine/IntelJITEvents/CMakeLists.txt

#Set correct version
sed -i "s/set(RPCS3_GIT_VERSION \"local_build\")/set(RPCS3_GIT_VERSION \"$COMMITNUM-$COMMITVER\")/g" \
  rpcs3/git-version.cmake
sed -i 's/set(RPCS3_GIT_BRANCH "local_build")/set(RPCS3_GIT_BRANCH "master")/g' \
  rpcs3/git-version.cmake

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

cd build
  cmake \
    -Wno-dev \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DUSE_SYSTEM_CURL=ON \
    -DUSE_SYSTEM_FAUDIO=ON \
    -DUSE_SYSTEM_FFMPEG=ON \
    -DUSE_SYSTEM_FLATBUFFERS=ON \
    -DUSE_SYSTEM_LIBPNG=ON \
    -DUSE_SYSTEM_LIBUSB=ON \
    -DUSE_SYSTEM_PUGIXML=ON \
    -DUSE_SYSTEM_XXHASH=ON \
    -DUSE_SYSTEM_ZLIB=ON \
    -DCMAKE_BUILD_TYPE=Release ..
  make
  make install/strip DESTDIR=$PKG
cd ..

rm -r $PKG/usr/share/$PRGNAM/git

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

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  LICENSE 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
cat $CWD/doinst.sh > $PKG/install/doinst.sh

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