#!/bin/bash

# Slackware build script for doomseeker

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

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

# 20260802 bkw: updated for v1.5.3. original site down, use github.
# 20230111 bkw: updated for v1.3.3.
# 20211102 bkw:
# - updated for v1.3.2.
# - new-style icons.
# - /usr/share/doomseeker => /usr/share/games/doomseeker.

# 20201030 bkw: updated for v1.3.1
# 20200122 bkw: updated for v1.3+20200119_b623ab1. Latest git fixes
# a segfault issue that normally only occurs when starting doomseeker
# without ~/.config/doomseeker/* (so the previous version worked for me,
# but wouldn't for a new user).
# 20191211 bkw: updated for v1.3. qt4 support was dropped, qt5 now required.
# 20181218 bkw: updated for v1.2. Upstream has changed things around,
# so this script won't build 1.1 any longer.

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

PRGNAM=doomseeker
SRCNAM=Doomseeker
VERSION=${VERSION:-1.5.3}
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"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi
SLKCFLAGS+=" -DNDEBUG"

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP

rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz
cd $SRCNAM-$VERSION

chown -R root:root .
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 {} + \)

sed -i 's,Exec=,&/usr/games/,' media/*.desktop

sed -i 's,share/doomseeker,share/games/doomseeker,' src/core/datapaths.cpp

# 20260802 bkw: gitinfo.h came from cloning the repo and building
# with -DCMAKE_BUILD_TYPE=Release. Lame.
# 20260802 bkw: not gonna bother with an option for Qt6. if you're
# smart enough to read this comment, you're smart enough to edit the
# -DQt_PACKAGE line yourself. just don't complain to me if it doesn't
# work because I'm not supporting Qt6 in this build.
mkdir -p build
cd build
  cmake \
    -DQt_PACKAGE=Qt5 \
    -DCMAKE_C_FLAGS_REVISION="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS_REVISION="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_BINDIR=games \
    -DCMAKE_INSTALL_LIBDIR=lib${LIBDIRSUFFIX} \
    -DCMAKE_BUILD_TYPE=Release ..
  cat $CWD/gitinfo.h > src/core/gitinfo.h
  make VERBOSE=1
  make install/strip DESTDIR=$PKG
cd ..

# This is a game (or anyway a game utility), the binary
# belongs in /usr/games. cmake accepts and totally ignores
# "-DCMAKE_INSTALL_BINDIR=games" so just move the damn thing after
# install.
mv $PKG/usr/bin $PKG/usr/games

# cmake is also ignoring -DCMAKE_INSTALL_DOCDIR.
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
if [ -e $PKG/usr/share/doc/$PRGNAM/ ]; then
  mv $PKG/usr/share/doc/$PRGNAM/* $PKGDOC
  rm -rf $PKG/usr/share/doc
fi

# upstream installs their *giant* icon directly in /usr/share/icons...
rm -rf $PKG/usr/share/icons
for px in 16 32 48 64 128; do
  size=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  convert -resize $size media/icon.png $dir/$PRGNAM.png
done

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

cp -a LICENSE *.md $PKGDOC
sed -i 's,\r,,' $PKGDOC/*
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
