#!/bin/bash

# Slackware build script for sfarkxtc

# Originally by Ben Colliver. Rewritten and now maintained by B. Watson
# (urchlay@slackware.uk).

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

# Note: there's a .sfark file to test with, here:
# https://slackware.uk/~urchlay/src/OPL-3_FM_128M.sfArk
# Originally downloaded from:
# https://woolyss.com/chipmusic-soundfonts.php

# 20260301 bkw:
# - take over maintenance.
# - add license (WTFPL).
# - build from latest (2018) source rather than use a binary from 2007.
# - add man page.
# - since we're building from source, x86_64 is now supported.

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

PRGNAM=sfarkxtc
VERSION=${VERSION:-20181208}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

COMMIT=${COMMIT:-4ed577d5779a68422b816f96a40e8e4abe65894a}
LIBNAM=sfArkLib
LIBCOMMIT=${LIBCOMMIT:-c710d851a9b9c1759ac8597e12f55ea290a3f007}

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}

LIBDIR=/usr/lib
if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" -o "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIR=/usr/lib64
fi

set -e

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
TOPDIR=$TMP/$PRGNAM-$VERSION-build

rm -rf $PKG $TOPDIR
mkdir -p $TOPDIR $PKGDOC $PKG/$LIBDIR $PKG/usr/bin $PKG/usr/include $OUTPUT
cd $TOPDIR
rm -rf $PRGNAM-$COMMIT $LIBNAM-$LIBCOMMIT
tar xvf $CWD/$PRGNAM-$COMMIT.tar.gz
tar xvf $CWD/$LIBNAM-$LIBCOMMIT.tar.gz
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 {} + \)

# First, the library:
cd $LIBNAM-$LIBCOMMIT
sed -i "/^CXXFLAGS/s,\$, $SLKCFLAGS," Makefile
make
strip libsfark.so.*.*
make install DESTDIR=$PKG PREFIX=/usr LIBDIR=$LIBDIR
# 'install' creates this +x, ugh.
chmod 644 $PKG/usr/include/*.h
cat README.md > $PKGDOC/$LIBNAM.md

# Now the program, link with the just-built library:
cd $TOPDIR/$PRGNAM-$COMMIT
make CXXFLAGS="$SLKCFLAGS -I$PKG/usr/include" LDFLAGS="-L$PKG/$LIBDIR"
install -s $PRGNAM $PKG/usr/bin

# Man page written by maintainer. Debian has a man page but it's
# outdated (doesn't mention that the output-file is optional) and
# almost content-free anyway.
mkdir -p $PKG/usr/man/man1
gzip -9c < $CWD/$PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

# Docs. The library's COPYING is identical, don't need 2 copies.
cp -a COPYING* README* $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

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