#!/bin/bash

# Slackbuild for vbam

# Currently maintained by B. Watson <urchlay@slackware.uk>.
# Previously maintained by Dugan Chen <email removed>.
# Modified by the SlackBuilds.org project.
# Written by JK Wood <email removed>.

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20250914 bkw: update for v2.2.2.
# 20240925 bkw:
# - update for v2.1.11.
# - disable x86 asm optimizations (broken for now).

# 20240215 bkw: update for v2.1.9.
# 20231219 bkw: *finally* update, for v2.1.8.

# 20230714 bkw: no changes, just a note to myself: v2.1.6 won't build,
# and nothing in post-2.1.6 git helps. Visit again later.

# 20230107 bkw: update for v2.1.5.

# 20211117 bkw: note to self: there's been recent activity in
# upstream's git. If another 6-12 months goes by without a release,
# consider updating this for the latest git.

# 20211117 bkw: BUILD=2
# - fix build for -current and recent wxGTK3 update.
# - relicense as WTFPL with permission from JK Wood.
# - old-style icon symlink.
# - call executable with full path in .desktop.
# - add missing changelog/readme/todo to docdir.
# - get rid of FFMPEG option (it's always enabled now).
# - disable checking for updates (there haven't been any in
#   5 years anyway, and this is a bit of a privacy concern for
#   some users).
# - make wxGTK3 optional.
# - only include .desktop and doinst in package if wxGTK3 enabled.
# - make SFML optional. it's actually autodetected in v2.1.4,
#   so all this means is removing it from REQUIRES and adding
#   a note to README.
# - always build the SDL UI executable (/usr/games/vbam).
# - include vbam.ini.sample in docdir.

# 20191129 bkw: updated for 2.1.4.
# 20180702 bkw: updated for 2.1.0.
# 20180612 bkw: updated for 2.0.2.
# 20171219 bkw: updated for 2.0.1.
# 20170302 bkw: use long-format github URL

# 20170126 bkw:
# - New maintainer, remove old maintainers' email addresses.
# - Update for 2.0.0beta3. This adds SDL2 and SFML as deps.
#   Actually I could make SFML optional, it's only used for emulating
#   the GBA link cable. If anyone *really* wants to do this, add
#   -DENABLE_LINK=OFF to the cmake command. Not going to add it as
#   an option unless someone requests it.
# - Install binary in /usr/games, not /usr/bin.
# - Use correct wx-config, even if /usr/bin/wx-config symlink points
#   to 2.8 instead of 3.0.
# - Get rid of Windows-only doc.
# - Binary & source name has changed to visualboyadvance-m, keep the
#   old vbam name as symlinks.
# - Correct README, plus cosmetic reformatting.

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

PRGNAM=vbam
VERSION=${VERSION:-2.2.2}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

SRCVER="$VERSION"
SRCNAM="visualboyadvance-m"

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=""
  #ASM=ON # x86 ASM optimizations are available (but currently broken)
  ASM=OFF
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
  #ASM=ON
  ASM=OFF
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
  ASM=OFF
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
  ASM=OFF
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$SRCVER
tar xvf $CWD/$SRCNAM-$SRCVER.tar.gz
cd $SRCNAM-$SRCVER
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} + -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} +

# 20250914 bkw: upstream removed this file from its repo before
# the 2.2.2 release, assuming distros would have it. after
# the release (commit 9fcc0ad), they restored it. So get the
# file from their git repo (also, it has the wrong case, so fix that).
[ -e cmake/FindFFMPEG.cmake ] || \
  cat $CWD/FindFFmpeg.cmake > cmake/FindFFMPEG.cmake

# 20250914 bkw: our SDL 2.0 doesn't have this hint. not needed
# anyway, SDL knows to use X.
sed -i '/SDL_HINT_VIDEODRIVER/d' src/wx/panel.cpp

# 20230107 bkw: doubleplusungood:
# sbrun: WARNING: files altered outside the sandbox:
# u /root/.gnupg/trustdb.gpg
# I don't know why it's doing this, but this will prevent it.
export CCACHE_DIR=${CCACHE_DIR:-$HOME/.ccache}
mkdir -p fakehome
export HOME=$( pwd )/fakehome

# if both wx(Python|GTK) 2 and 3 are installed, /usr/bin/wx-config
# is a symlink to the config for whichever was installed last. Avoid
# confusion, force the correct version.
# 20211117 bkw: willysr's wxGTK3 has switched from gtk2 to gtk3, so
# this line had to be adjusted slightly. With gtk2 this was failing
# to build on -current anyway...
# 20250914 bkw: wxGTK3 no longer works, we need wxWidgets.
WXCONF=${WXCONF:-/usr/lib$LIBDIRSUFFIX/wx/config/gtk3-unicode-3.2}

# 20211117 bkw: upstream doesn't autodetect wx, we have to explicitly
# set it to ON or OFF.
# 20220221 bkw: this had to be fixed again, it started failing when
# /usr/bin/wx-config was the wxPython one.
WXDEF="-DENABLE_WX=OFF"
if [ "${WX:-yes}" = "yes" -a -x "$WXCONF" ]; then
  WXDEF="-DENABLE_WX=ON"
  mkdir wxtmp
  ln -s $WXCONF wxtmp/wx-config
  export PATH=$(pwd)/wxtmp:$PATH
fi

# 20211117 bkw: lirc doesn't autodetect.
LIRCOPT=OFF
[ "${LIRC:-yes}" = "yes" ] && pkg-config --exists lirc && LIRCOPT=ON

# 20211117 bkw: no handy -Dwhatever to disable this, surgery required.
# At first glance it looks like -DENABLE_LINK=OFF would work, but it
# doesn't: it disables the GBA Link emulation, but the vbam binaries
# still get linked with the SFML shared libs. Derp.
[ "${SFML:-yes}" = "no" ] && sed -i '/^find_package(SFML/d' CMakeLists.txt

# upstream cleverly parses their own ChangeLog to get the version number
# that gets compiled into the binary. They un-cleverly failed to follow
# their own formatting rules for v2.0.2, meaning the 2.0.2 executable
# says 2.0.1 in its window title. So:
sed -i '/\[2\.0\.2\]/s,Bug fix release,-,' CHANGELOG.md

# 20211117 bkw: fix the SDL UI build.
sed -i 's,KMOD_META,KMOD_ALT,g' src/sdl/SDL.cpp

# 20211117 bkw: -fuse-ld=gold works, but doesn't speed things up much.
# 20250914 bkw: 2.2.2 adds support for transparently decompressing
# ROMs compressed with LZMA... but Slackware 15.0's liblzma (from
# the xz package) is too old, so I had to disable it. I doubt anyone
# misses it, anyway. Slackware -current's xz is new enough, so I'll
# remove -DENABLE_LZMA=OFF when 15.1 is released.

mkdir -p build
cd build
  cmake \
    -DENABLE_SDL=ON \
    "$WXDEF" \
    -DENABLE_LZMA=OFF \
    -DENABLE_LINK=$LINKOPT \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DENABLE_ASM_CORE=${ASM} \
    -DENABLE_ASM_SCALERS=${ASM} \
    -DENABLE_FFMPEG=ON \
    -DENABLE_ONLINEUPDATES=OFF \
    -DENABLE_LIRC=$LIRCOPT \
    -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=gold" \
    -DCMAKE_SHARED_LINKER_FLAGS="-fuse-ld=gold" \
    -DCMAKE_INSTALL_BINDIR=games \
    -DCMAKE_INSTALL_MANDIR=man \
    -DCMAKE_INSTALL_SYSCONFDIR=/etc \
    -DCMAKE_BUILD_TYPE=Release ..
  make VERBOSE=1
  make install DESTDIR=$PKG
cd ..

strip $PKG/usr/games/*
gzip $PKG/usr/man/man6/*.6

mkdir -p $PKG/install

if [ -x $PKG/usr/games/$SRCNAM ]; then
  # in case someone's missing /usr/games from $PATH:
  sed -i 's,^Exec=,&/usr/games/,' $PKG/usr/share/applications/*desktop

  # old-style windowmanagers might need this:
  mkdir -p $PKG/usr/share/pixmaps

  ln -s ../icons/hicolor/48x48/apps/$SRCNAM.png $PKG/usr/share/pixmaps/$SRCNAM.png

  # only need the doinst.sh if the WX GUI was built.
  cat $CWD/doinst.sh > $PKG/install/doinst.sh
fi

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
# This config file gets installed, but never actually used by vbam...
mv $PKG/etc/$PRGNAM.cfg $PKGDOC/$PRGNAM.cfg.sample
rmdir $PKG/etc
rm -f doc/ReadMe.MFC.txt # this is for Windows only
cp -a *.md doc/* $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

# 20211117 bkw: actually look at what we just built, to see what's
# enabled (instead of trusting that everything got included correctly).
# It's better to use objdump than ldd here IMO.
SFYN=no; LIRCYN=no; WXYN=no
objdump -p $PKG/usr/games/$PRGNAM | grep -q 'NEEDED *libsfml' && SFYN=yes
strings $PKG/usr/games/$PRGNAM | grep -q lircrc && LIRCYN=yes
[ -x $PKG/usr/games/$SRCNAM ] && WXYN=yes

sed -e "s,@SFYN@,$SFYN," \
    -e "s,@LIRCYN@,$LIRCYN," \
    -e "s,@WXYN@,$WXYN," \
    $CWD/slack-desc > $PKG/install/slack-desc

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