#!/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.

# 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}
BUILD=${BUILD:-4}
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.gz
cd $PRGNAM-$VERSION
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 {} +

# 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.
# - use correct app-defaults dir.
patch -p1 < $CWD/sbo.diff

# 20230517 bkw: Jens Guenther's "quick and dirty" patch to make the
# windowmanager's close button work. Many thanks!
patch -p1 < $CWD/window_close.diff

# 20230418 bkw: the -laoss here allows xtrs to make sound via ALSA
# or PulseAudio (via /etc/asound.conf).
make DEBUG="$SLKCFLAGS" EXTRALIBS="-laoss"
mkdir -p $PKG/usr/games $PKG/usr/man/man6
make BINDIR=$PKG/usr/games MANDIR=$PKG/usr/man install

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.
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC/utilities
cp -a ChangeLog README *.html $PKGDOC
cp -a *.ccc *.cmd *.tgz *.jcl *.bas *.z80 *.lst 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
