#!/bin/bash

# Slackware build script for zsnes

# Written by B. Watson (urchlay@slackware.uk)

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

# Note: this is a completely new-from-template znes.SlackBuild,
# written from scratch, for the "LTS" 2.x fork from xyproto. It shares
# no code with the old zsnes.SlackBuild for the 1.51 release.

# 20261016 bkw: update for v2.3.8.
# 20261012 bkw: update for v2.3.7.
# 20260930 bkw: update for v2.3.6, which adds 64-bit support!

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

PRGNAM=zsnes
VERSION=${VERSION:-2.3.8}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

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

# No SLKCFLAGS here. Upstream's got a set of flags they've tested with.

# Always uses MMX extensions so i686 is correct.
# PRINT_PACKAGE_NAME and the actual package name will always say i686.
[ "$ARCH" = "i586" ] && ARCH="i686"

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}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz --no-same-owner
cd $PRGNAM-$VERSION
find . ! -type l    -a \( \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \) \)

# Patch does these things:
# - Install binary to /usr/games
# - Fix install permissions for icon, .desktop, metainfo (no +x).
# - Install man page to /usr/man (not /usr/share/man)
# - Install man page in man6.
# - Use full path to binary in .desktop file.
patch -p1 < $CWD/sbo.diff

# 20261012 bkw: this used to be part of the patch, but it breaks
# on updates.
MANDATE="$( date '+%B %Y' )"
sed -i \
  's/\.TH.*/& ZSNES "6" "'"$MANDATE"'" "'$VERSION'" "SlackBuilds.org"/' \
  man/zsnes.1

# Build options.
# Disabling WITH_AO doesn't seem particularly useful, but allow it.
# Disabling WITH_DEBUGGER might make sense for slow systems.
A=no ; D=no; P=yes
[ "${AO:-yes}"       = "yes" ] && ARGS+="WITH_AO=yes "       && A=yes
[ "${DEBUGGER:-yes}" = "yes" ] && ARGS+="WITH_DEBUGGER=yes " && D=yes
[ "${PIPEWIRE:-no}"  = "no"  ] && ARGS+="WITH_PIPEWIRE=no "  && P=no
echo "::: running: make $ARGS"

# 20260926 bkw: 15.0's pipewire is too old (at least, the build fails).
# Revisit this once 16.0 is out.
# 20261003 bkw: build was failing when ARCH was set in the script's env.
env -u ARCH make $ARGS PIPEWIRE_AVAILABLE=""
env -u ARCH make install DESTDIR=$PKG
gzip -9 $PKG/usr/man/man*/*

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a COPYING* README* TODO* $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
sed -e "s,@A@,$A," -e "s,@D@,$D," < $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
