#!/bin/bash

# Slackware build script for rakarrack

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

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

# 20250807 bkw: BUILD=2, fix compile with fltk-1.4.

# 20241206 bkw:
# - update for last git commit, 0.6.2+20150813_a620840.
# - add some patches, including one that fixes a segfault on startup.
#   apparently, the previous version has segfaulted on startup ever
#   since Slackware 15.0 came out, and nobody mentioned it (and I
#   haven't used rakarrack in a while either).
# - add SSE=no option.
# - touch up README and slack-desc (mention guitar).

# 20211205 bkw: BUILD=3, new-style icons.
# 20140114 bkw: add 64-bit distortion fix and setcap stuff.
# 20151126 bkw: fix build on -current, preparing for 14.2.

# Note: Please don't remove this from the repo just because it's
# no longer maintained upstream. It's useful, it works, it makes
# interesting sounds, and some of us actually use it.

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

PRGNAM=rakarrack
VERSION=${VERSION:-0.6.2+20150813_a620840}
COMMIT=${COMMIT:-a6208406d94a1da978f435605072ee5caefe1491}
BUILD=${BUILD:-2}
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

set -e

SRC=$PRGNAM-git-$COMMIT

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRC
unzip $CWD/$SRC.zip
cd $SRC
chown -R root:root .

# 20250807 bkw: someone forgot to include a fltk header, I dunno if this
# was considered OK in fltk-1.3 or if it just happened to work, but it
# has to be explicit now:
sed -i '/Fl_PNG_Image/a#include <FL/Fl_Shared_Image.H>' src/rakarrack.h

# 20241206 bkw: Every file is +x, do not revert to template.
find . -type f -a -exec chmod 644 {} + -o -type d -a -exec chmod 755 {} +

# 20241206 bkw: patches mostly came from AUR and Debian.
# format-security.diff had to be reworked (some of it had been applied
# upstream, but not all), and 64bit_distortion.diff comes from:
# http://sourceforge.net/p/rakarrack/discussion/778862/thread/e9559a34/
for i in $CWD/patches/*; do
  echo "===> applying $( basename $i )"
  patch -p1 < $i
done

autoreconf -if

# 20241207 bkw: Normally, it autodetects SSE and SSE2. Allow SSE=no
# to disable them both. Not sure why anyone in 2025 wants to run this
# on a box that's old enough to not support SSE... This option is
# provided for building on a box *with* SSE/SSE2, for deployment on a
# box that lacks it.
# Setting SSE to a non-empty value in configure's env is required, to
# make the --disable-sse --disable-sse2 work. No idea why; found this
# in the debian/rules file, and it works.
if [ "${SSE:-yes}" = "no" ]; then
  OPTS="--disable-sse --disable-sse2"
  SSE=" "
else
  SSE=""
fi

# 20241207 bkw: --enable-docdir makes it look in --docdir for the
# HTML help files; otherwise, it looks in /usr/share/doc/rakarrack/,
# and doesn't find them.
SSE="$SSE" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  $OPTS \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --enable-docdir \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --build=$ARCH-slackware-linux

make
make install-strip DESTDIR=$PKG

gzip $PKG/usr/man/man?/*.?

# 20241206 bkw: Upstream now installs icons, but there's no 48x48 one,
# which is still the standard for /usr/share/pixmaps.
mkdir -p $PKG/usr/share/{icons/hicolor/48x48/apps,pixmaps}
convert -resize 48x48 \
        icons/icono_rakarrack_128x128.png \
        $PKG/usr/share/icons/hicolor/48x48/apps/$PRGNAM.png

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

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild
ln -s ../../share/licenses/$PRGNAM/COPYING $PKGDOC/COPYING

# 20241207 bkw: Record in the slack-desc whether SSE/SSE2 are required.
WITHSSE="does not require SSE or SSE2"
grep -q -- -mfpmath=sse config.status && WITHSSE="requires SSE"
grep -q -- -msse2 config.status && WITHSSE="requires SSE and SSE2"

mkdir -p $PKG/install
sed "s,@WITHSSE@,$WITHSSE," < $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

# 20241207 bkw: Only the rakarrack binary needs setcap in doinst.sh.
# The others (rakverb, rakconvert, etc) are file format conversion
# tools, and don't play audio via jack even though they're linked with
# libjack.

# Only add capability stuff if not disabled:
if [ "${SETCAP:-yes}" = "yes" ]; then
  cat $CWD/setcap.sh >> $PKG/install/doinst.sh
  # Only allow execution by audio group
  chown root:audio $PKG/usr/bin/$PRGNAM
  chmod 0750 $PKG/usr/bin/$PRGNAM
fi

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
