#!/bin/bash

# Slackware build script for pulseaudio-jack

# Copyright 2015  Christoph Willing  Brisbane, Australia
# Copyright 2016, 2018, 2020, 2021  Patrick J. Volkerding, Sebeka, MN, 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.

# Modified 2019, 2020, 2021, 2022 by Eric Hameleers <alien@slackware.com> to build JACK support.

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

PKGNAM=pulseaudio-jack
SRCNAM=pulseaudio
VERSION=${VERSION:-17.0}
BUILD=${BUILD:-2alien}

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$(uname -m)" in
    i?86) ARCH=i586 ;;
    arm*) readelf /usr/bin/file -A | egrep -q "Tag_CPU.*[4,5]" && ARCH=arm || ARCH=armv7hl ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
    *) ARCH=$(uname -m) ;;
  esac
  export ARCH
fi

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PKGNAM-$VERSION-$ARCH-$BUILD.txz"
  exit 0
fi

TMP=${TMP:-/tmp}
PKG=$TMP/package-$PKGNAM

# Set CFLAGS/CXXFLAGS and LIBDIRSUFFIX:
case "$ARCH" in
  i?86)      SLKCFLAGS="-O2 -march=${ARCH} -mtune=i686"
             SLKLDFLAGS=""; LIBDIRSUFFIX=""
             ;;
  x86_64)    SLKCFLAGS="-O2 -fPIC"
             SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"
             ;;
  armv7hl)   SLKCFLAGS="-O2 -march=armv7-a -mfpu=vfpv3-d16"
             SLKLDFLAGS=""; LIBDIRSUFFIX=""
             ;;
  *)         SLKCFLAGS=${SLKCFLAGS:-"-O2"}
             SLKLDFLAGS=${SLKLDFLAGS:-""}; LIBDIRSUFFIX=${LIBDIRSUFFIX:-""}
             ;;
esac

case "$ARCH" in
    arm*)    TARGET=$ARCH-slackware-linux-gnueabi ;;
    *)       TARGET=$ARCH-slackware-linux ;;
esac

NUMJOBS=${NUMJOBS:-" -j$(expr $(nproc) + 1) "}

rm -rf $PKG
mkdir -p $TMP $PKG
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.xz || exit 1
cd $SRCNAM-$VERSION || exit 1
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \+ -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \+

cat $CWD/patches/030_posix-completion.diff | patch -p0 --verbose || exit 1

# https://bugzilla.redhat.com/show_bug.cgi?id=1234710
cat $CWD/patches/0001-client-conf-Add-allow-autospawn-for-root.patch | patch -p1 --verbose || exit 1
cat $CWD/patches/0002-allow-autospawn-for-root-default.diff | patch -p1 --verbose || exit 1

# Do not log a warning every time root uses PulseAudio:
cat $CWD/patches/0003-no-root-warn.diff | patch -p1 --verbose || exit 1

sed -i -e '/@PA_BINARY@/ imkdir -p \$HOME/.config/pulse' src/daemon/start-pulseaudio-x11.in
echo "X-MATE-Autostart-Phase=Initialization" >> src/daemon/pulseaudio.desktop.in

# If autospawn isn't working, start-pulseaudio-x11 should start it manually:
cat $CWD/patches/pulseaudio-autostart.patch | patch -p1 --verbose || exit 1
# When built by meson, this patch also required to fix the above issue:
cat $CWD/patches/pulseaudio-autostart-meson.build.patch | patch -p1 --verbose || exit 1

# Fix 17.0 regressions:
cat $CWD/patches/ed3d4f0837f670e5e5afb1afa5bcfc8ff05d3407.patch | patch -p1 --verbose || exit 1
cat $CWD/patches/f5cacd94abcc47003bd88ad7ca1450de649ffb15.patch | patch -p1 --verbose || exit 1

# Configure, build, and install:
export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"
mkdir meson-build
cd meson-build
meson setup \
  --prefix=/usr \
  --libdir=lib${LIBDIRSUFFIX} \
  --libexecdir=/usr/libexec \
  --bindir=/usr/bin \
  --sbindir=/usr/sbin \
  --includedir=/usr/include \
  --datadir=/usr/share \
  --mandir=/usr/man \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --buildtype=release \
  -Dgsettings=enabled \
  -Dhal-compat=false \
  -Dorc=enabled \
  -Dsystem_user="pulse" \
  -Dsystem_group="pulse" \
  -Daccess_group="audio" \
  -Dbashcompletiondir="/usr/share/bash-completion/completions" \
  .. || exit 1

  "${NINJA:=ninja}" $NUMJOBS || exit 1
  #DESTDIR=$PKG $NINJA install || exit 1
  # We manually install only the JACK support libraries:
  install -d $PKG/usr/lib${LIBDIRSUFFIX}/pulseaudio/modules
  install -m0755 src/modules/module-jack*.so \
     $PKG/usr/lib${LIBDIRSUFFIX}/pulseaudio/modules/
cd ..

# Strip binaries (if any):
find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

# Install documentation:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a \
   COPYING* *GPL LICENSE README* \
  $PKG/usr/doc/$PKGNAM-$VERSION

# Add a package description:
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-required > $PKG/install/slack-required
# Not needed for the JACK libraries:
#cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $TMP/$PKGNAM-$VERSION-$ARCH-$BUILD.txz
cd $TMP
  md5sum $PKGNAM-$VERSION-$ARCH-$BUILD.txz > $PKGNAM-$VERSION-$ARCH-$BUILD.txz.md5
  cat $PKG/install/slack-desc | grep "^${PKGNAM}" > $PKGNAM-$VERSION-$ARCH-$BUILD.txt
  cat $PKG/install/slack-required > $PKGNAM-$VERSION-$ARCH-$BUILD.dep
cd -

