#!/bin/bash

# Slackware build script for calf

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

# 20260728 bkw: update for v0.90.9.
# - Upstream switched to cmake.
# - 0.90.9 supposedly supports WANT_SSE=OFF, but this causes the build
#   to fail, so SSE is always enabled.

# 20211204 bkw: BUILD=3, move html docs to subdir.
# 20200117 bkw: demote lash from required to optional dependency.
# 20191208 bkw: update for v0.90.3.

# 20180709 bkw:
# - Update to latest release (less than a day old).
# - Use correct upstream homepage.

# 20170622 bkw:
# - Update to latest git. It's been a long time since the last release.
# - Actually make the SSE=yes option work (d'oh!)

# 20170301 bkw: use long-format github URL, no more $VERSION.tar.gz

# 20151106 bkw:
# Switch to -master and upgrade to v0.0.60. No more LADSPA or DSSI
# support (upstream dropped it). But if you need LADSPA, there's a
# separate calf-ladspa build now. If there's a popular demand for
# DSSI, I'll add it to calf-ladspa, not here.
# fluidsynth is now required, because the build fails without it, even
# though it's listed as experimental and there's a --disable-experimental
# option. Since it's required anyway, might as well --enable-experimental.

# 20141030 bkw:
# Finally getting around to submitting this, there have been no code
# changes upstream since 20140308.

# 20140308 bkw:
# - Switched to falkTX's KXStudio fork of CALF. falkTX doesn't do release
#   tarballs, so this is a git checkout from 20140308.
# - Added capability stuff.

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

PRGNAM=calf
VERSION=${VERSION:-0.90.9}
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

# 20260728 bkw: this is necessary because the stupid thing deletes
# all the *.ttl files from /usr when installing (even though it's
# smart enough to generate them in DESTDIR/usr).
if [ -e /usr/bin/calfjackhost ]; then
  cat <<EOF 1>&2
*********************************************************
* calf is already installed. Remove it before building. *
*********************************************************

This command should do the job:

removepkg calf

EOF
  exit 1
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
SLKCFLAGS+=" -DNDEBUG"

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$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 {} + \)

# 20260728 bkw: we don't support version 1.5 of the .desktop spec in 15.0:
sed -i '/^Version/d' calf.desktop

mkdir -p build
cd build
  cmake \
    -DWANT_SSE=ON \
    -DWANT_EXPERIMENTAL=ON \
    -DCMAKE_C_FLAGS_RELEASE="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS_RELEASE="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_MANDIR=man \
    -DCMAKE_INSTALL_DOCDIR=doc/$PRGNAM-$VERSION \
    -DCMAKE_BUILD_TYPE=Release ..
  make VERBOSE=1
  export DESTDIR=$PKG # this has to be in the environment!
  make install/strip DESTDIR=$PKG
cd ..
gzip -9 $PKG/usr/man/man*/*

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a AUTHORS COPYING* ChangeLog README* TODO $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

# This shouldn't be in the package:
rm -f $PKG/usr/share/icons/hicolor/icon-theme.cache

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

# 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
