#!/bin/bash

# Slackware build script for xemu

# 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=xemu
BRANCH=${BRANCH:-master}
VERSION=${VERSION:-0.7.86}
COMMIT=${COMMIT:-4a99fd0f1855fc52e15885812abd3ee4bb2b3847}
BSOFTFLOAT3=${BSOFTFLOAT3:-b64af41c3276f97f0e181920400ee056b9c88037}
BTESTFLOAT3=${BTESTFLOAT3:-5a59dcec19327396a011a17fd924aed4fec416b3}
GENCONFIG=${GENCONFIG:-44bab849ce87fceafd74703bfcf2b61a1a1b738f}
IMGUI=${IMGUI:-c71a50deb5ddf1ea386b91e60fa2e4a26d080074}
IMPLOT=${IMPLOT:-b47c8bacdbc78bc521691f70666f13924bb522ab}
KEYCODEMAPDB=${KEYCODEMAPDB:-d21009b1c9f94b740ea66be8e48a1d8ad8124023}
NV2AVSHCPU=${NV2AVSHCPU:-d5a7308809a80e1b01b5c016127d4f1b91c8673b}
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}

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

tar xvf $CWD/berkeley-softfloat-3-$BSOFTFLOAT3.tar.gz -C \
  $TMP/$PRGNAM-$VERSION/tests/fp/berkeley-softfloat-3 --strip-components 1
tar xvf $CWD/berkeley-testfloat-3-$BTESTFLOAT3.tar.gz -C \
  $TMP/$PRGNAM-$VERSION/tests/fp/berkeley-testfloat-3 --strip-components 1
tar xvf $CWD/genconfig-$GENCONFIG.tar.gz -C \
  $TMP/$PRGNAM-$VERSION/genconfig --strip-components 1
tar xvf $CWD/imgui-$IMGUI.tar.gz -C \
  $TMP/$PRGNAM-$VERSION/ui/thirdparty/imgui --strip-components 1
tar xvf $CWD/implot-$IMPLOT.tar.gz -C \
  $TMP/$PRGNAM-$VERSION/ui/thirdparty/implot --strip-components 1
tar xvf $CWD/keycodemapdb-$KEYCODEMAPDB.tar.gz -C \
  $TMP/$PRGNAM-$VERSION/ui/keycodemapdb --strip-components 1
tar xvf $CWD/nv2a_vsh_cpu-$NV2AVSHCPU.tar.gz -C \
  $TMP/$PRGNAM-$VERSION/hw/xbox/nv2a/thirdparty/nv2a_vsh_cpu --strip-components 1

cd $PRGNAM-$VERSION

mkdir -p tomlplusplus/include
sed -i '7d;8d;9d;10d;11d;12d;13d;14d;15d;16d;17d;18d;19d;20d;21d;22d;23d;24d;25d;26d;27d' \
  scripts/xemu-version.sh
sed -i "7iXEMU_COMMIT=${COMMIT}" scripts/xemu-version.sh
sed -i "8iXEMU_BRANCH=${BRANCH}" scripts/xemu-version.sh
sed -i "9iXEMU_VERSION=${VERSION}" scripts/xemu-version.sh
sed -i "s/self._version = self.submodule.head/self._version = '$COMMIT'/g" scripts/gen-license.py
python3 scripts/gen-license.py > XEMU_LICENSE

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

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --disable-debug-info \
  --enable-slirp=system \
  --extra-cflags="-DXBOX=1" \
  --target-list=i386-softmmu \
  --with-git-submodules=ignore \
  --ninja=ninja \
  --build=$ARCH-slackware-linux
make qemu-system-i386

install -vDm755 build/qemu-system-i386 $PKG/usr/bin/$PRGNAM
install -vDm644 ui/${PRGNAM}.desktop -t $PKG/usr/share/applications
install -vDm644 ui/icons/${PRGNAM}.svg -t $PKG/usr/share/icons/hicolor/scalable/apps
for SIZE in 16 24 32 48 64 128 256 512; do
  install -vDm644 ui/icons/xemu_${SIZE}x${SIZE}.png \
    $PKG/usr/share/icons/hicolor/${SIZE}x${SIZE}/apps/$PRGNAM.png
done

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 \
  COPYING COPYING.LIB LICENSE MAINTAINERS README.* \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
install -vDm644 $TMP/$PRGNAM-$VERSION/XEMU_LICENSE $PKG/usr/doc/$PRGNAM-$VERSION

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
