#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# nethack.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2020-2026  Patrick J. Volkerding, Sebeka, Minnesota, USA
# Copyright 2025, 2026 David Ferrone RI, 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.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Versions of bundled packages and names of source archives.
# To save on maintenance, and since we carry the same versions as x86 Slackware,
# let's pull details from the x86 trunk:
slack_setvarfromupstream SRCVER

# Extract source:
tar xf $CWD/$PKGNAM-*src.tar.!(*sign|*asc|*sig)
cd NetHack*/ || failextract
slackhousekeeping

cd sys/unix
sh setup.sh hints/linux.500 || exit 1
cd ../..

# Do not edit include/config.h if you're using the WANT_WIN options below.
# HACKDIR is the location of the game files. SHELLDIR is location of the launcher (usr/games by default).
sed -i "s|^HACKDIR=\$(PREFIX)/games/lib/nethackdir$|HACKDIR=/var/lib/$PKGNAM|; s|^PREFIX=\$(wildcard ~)/nh/install$|PREFIX=/usr|" src/Makefile util/Makefile Makefile

mkdir -p $PKG/var/lib/$PKGNAM $PKG/usr/games

touch util/makedefs.c

# Prepare sources:
sed -e 's|^/\* \(#define LINUX\) \*/|\1|' -i include/unixconf.h

# Set default directories and install to use setgid games:
sed -e '/^HACKDIR/ s|/games/lib/\$(GAME)dir|/var/lib/nethack/|' \
    -e '/^SHELLDIR/ s|/games|/usr/games|' \
    -e '/^VARDIRPERM/ s|0755|0775|' \
    -e '/^VARFILEPERM/ s|0600|0664|' \
    -e '/^GAMEPERM/ s|0755|02755|' \
    -e 's|\(DSYSCF_FILE=\)\\"[^"]*\\"|\1\\"/var/lib/nethack/sysconf\\"|' \
    -e 's|\(DHACKDIR=\)\\"[^"]*\\"|\1\\"/var/lib/nethack/\\"|' -i sys/unix/hints/linux-minimal

sed -e 's|^#GAMEUID.*|GAMEUID = root|' \
    -e 's|^#GAMEGRP.*|GAMEGRP = games|' \
    -e '/^FILEPERM\s*=/ s|0644|0664|' \
    -e '/^DIRPERM\s*=/ s|0755|0775|' -i sys/unix/Makefile.top

sed -e "/^MANDIR\s*=/s|/usr/man/man6|$PKG/usr/man/man6|" -i sys/unix/Makefile.doc

# Create a few directories:
mkdir -vpm755 $PKG/usr/man/man6
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
mkdir -vpm755 $PKG/var/lib

# Fetch lua:
#make fetch-lua || exit 1
( mkdir lib
  cd lib
  tar xf $CWD/lua-5.4.8.tar.lz || exit 1
) || failextract

# Build and install:
## Uncomment ONE pair of the following make commands, depending on which windowing system you would like to support.

# To build a binary with tty + curses + X11 support, you can use the following build command:
make $NUMJOBS PREFIX=/usr HACKDIR=/var/lib/$PKGNAM WANT_WIN_TTY=1 WANT_WIN_CURSES=1 WANT_WIN_X11=1 || failmake
make $NUMJOBS PREFIX=$PKG/usr HACKDIR=$PKG/var/lib/$PKGNAM WANT_WIN_TTY=1 WANT_WIN_CURSES=1 WANT_WIN_X11=1 install || failinstall

# To build with tty + curses + QT support, use the following command:
#make $NUMJOBS PREFIX=/usr HACKDIR=/var/lib/$PKGNAM WANT_WIN_TTY=1 WANT_WIN_CURSES=1 WANT_WIN_QT=1
#make $NUMJOBS PREFIX=$PKG/usr HACKDIR=$PKG/var/lib/$PKGNAM WANT_WIN_TTY=1 WANT_WIN_CURSES=1 WANT_WIN_QT=1 install

# To build a binary with tty + curses + X11 + Qt support (that is, all the interfaces that are available for unix):
#make $NUMJOBS PREFIX=/usr HACKDIR=/var/lib/$PKGNAM WANT_WIN_ALL=1
#make $NUMJOBS PREFIX=$PKG/usr HACKDIR=$PKG/var/lib/$PKGNAM WANT_WIN_ALL=1 install

# Build a binary with tty only:
#make $NUMJOBS PREFIX=/usr HACKDIR=/var/lib/$PKGNAM
#make $NUMJOBS PREFIX=$PKG/usr HACKDIR=$PKG/var/lib/$PKGNAM install

# Install manpages:
( cd doc
  mkdir -p $PKG/usr/man/man6
  for manpage in dlb.6 makedefs.6 nethack.6 recover.6 ; do
    cat $manpage | ../util/makedefs --grep --input - --output - > $PKG/usr/man/man6/$manpage
  done )

mkdir -p $PKG/usr/share/games/nethack
mv $PKG/var/lib/nethack/{nethack,recover} $PKG/usr/share/games/nethack
mkdir -p $PKG/usr/share/pixmaps $PKG/usr/share/applications
install -vpm644 $CWD/nethack.desktop $PKG/usr/share/applications/
install -vpm644 $CWD/nethack.png $PKG/usr/share/pixmaps/

# Add some documentation:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a \
  Porting README* \
  dat/license \
  doc/Guidebook.txt \
  $PKG/usr/doc/nethack-$VERSION

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall        # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i      # compress man & info pages and delete usr/info/dir
slackslack           # set standard Slackware file/dir permissions and ownerships
slackdesc            # install slack-desc and doinst.sh

# Fix perms:
chown -R root:games $PKG/var/lib/nethack
chown root:games $PKG/usr/share/games/nethack/{nethack,recover}
chmod 2755 $PKG/usr/share/games/nethack/{nethack,recover}

slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
