#!/bin/bash

# Slackware build script for xtrs - the X Windows TRS-80 emulator

# Originally written by Niels Horn <email removed>.
# Highly modified and now maintained by B. Watson <urchlay@slackware.uk>.
# There was no license on the original version. Modified version is
# licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20260109 bkw: update to latest git, 4.9d+20250818_3a2180c.
# - rework sbo.diff.
# - remove window_close.diff (no longer needed).
# - add new gxtrs binary to the package.
# - .desktop file now launches gxtrs.

# 20230517 bkw: BUILD=4
# - include patch from Jens Guenther (sdltrs upstream maintainer) to
#   make the window close button work.

# 20230418 bkw: BUILD=3
# - new maintainer.
# - use ROMs from trs80-roms package by default.
# - binaries in /usr/games; man pages in section 6.
# - replace old 32x21 icon.
# - remove ROMs from package (they live in trs80-roms now).
# - include all the trs-80 native stuff in the doc dir, like
#   sdltrs does. basically it's the same stuff, too.
# - link with libaoss, to get audio working.

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

PRGNAM=xtrs
VERSION=${VERSION:-4.9d+20250818_3a2180c}
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"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
else
  SLKCFLAGS="-O2"
fi

set -e

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

# 20260119 bkw: include the tiny ROMs with the build, so we don't have
# a hard dep on zmac.
cp $CWD/prebuilt-roms/*.hex .

chown -R root:root .
# upstream permissions are wonky, do not revert to template.
find . -type f -a -exec chmod 644 {} + -o \
       -type d -a -exec chmod 755 {} +

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
SHARE=$PKG/usr/share/$PRGNAM

# 20230418 bkw: patch does all this:
# - use system-wide ROMs from trs80-roms package.
# - install binaries to /usr/games and man pages to section 6.
# - strip binaries.
patch -p1 < $CWD/sbo.diff

# 20260109 bkw: Whatever html2text command the author's using, it's
# not compatible with the one on SBo. We're not even packaging the
# *.txt versions of the docs, so just touch these to keep the build
# from failing.
touch cpmutil.txt dskspec.txt

# 20230418 bkw: the -laoss here allows xtrs to make sound via ALSA
# or PulseAudio (via /etc/asound.conf).
# 20260109 bkw: "make" by itself doesn't build gxtrs...
make DEBUG="$SLKCFLAGS" EXTRALIBS="-laoss" default gxtrs
mkdir -p $PKG/usr/games $PKG/usr/man/man6
make BINDIR=$PKG/usr/games \
     MANDIR=$PKG/usr/man \
     DOCDIR=$PKGDOC \
     SHAREDIR=$SHARE \
     install install-gxtrs

# 20260109 bkw: these are the 0-length dummies we created above.
rm -f $PKGDOC/{cpmutil,dskspec}.txt

# 20260109 bkw: this ROM ships with xtrs, but doesn't get installed
# by 'make install'.
cp -a esfrom.hex $SHARE

gzip -9 $PKG/usr/man/man*/*

# 20230418 bkw: made this icon by combining the sdltrs icon
# and the X logo (tinted red). not beautiful, but distinctive.
for i in $CWD/icons/*.png; do
  px="$( basename $i .png )"
  size="${px}x${px}"
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  cat $i > $dir/$PRGNAM.png
done

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

mkdir -p $PKG/usr/share/applications
cat $CWD/xtrs.desktop > $PKG/usr/share/applications/xtrs.desktop

# 20230418 bkw: the .txt files are just rendered versions of the man
# pages (with ddoouubblleedd characters), do not package.
mkdir -p $PKGDOC/utilities
cp -a ChangeLog README *.html $PKGDOC
cp -a *.ccc *.cmd *.tgz *.jcl *.bas *.z80 xtrsemt.h *.hex *.README \
      $PKGDOC/utilities
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$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
