#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# bluez.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2009-2027  Patrick J. Volkerding, Sebeka, Minnesota, 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.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar xf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || failextract
slackhousekeeping

# If there's no bluetooth header, add it in.
# This was not necessary when built on x86 or ARM soft float, so something changed.
# This header probably ought to be in one of the packages.
if [ ! -f /usr/include/mgmt.h ]; then
   echo "*** Adding /usr/include/mgmt.h ***"
   cp -fav /usr/src/linux/include/net/bluetooth/mgmt.h /usr/include/
   NOINCMGMT=yes
fi

# Apply patches:
#
sed -i -e 's|-lreadline|\0 -lncursesw|g' Makefile.{in,tools}

# GCC15 workaround:
SLKCFLAGS+=" -std=gnu17"

# Configure:
autoreconf -vif
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
LDFLAGS="-L/usr/lib$LIBDIRSUFFIX" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --mandir=/usr/man \
   --localstatedir=/var \
   --docdir=/usr/doc/${PKGNAM}-${VERSION} \
   --enable-library \
   --disable-systemd \
   --enable-deprecated \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package:
make install DESTDIR=$PKG || failinstall

# Autostart mpris-proxy:
mkdir -p $PKG/etc/xdg/autostart
install -vpm644 $CWD/mpris-proxy.desktop $PKG/etc/xdg/autostart

# Use reset=1 for the btusb module, which supposedly fixes reconnect problems:
mkdir -vpm755 $PKG/lib/modprobe.d
install -vpm644 $CWD/btusb.conf $PKG/lib/modprobe.d/

# Add bluetoothd to /usr/sbin
mkdir -p $PKG/usr/sbin
ln -vfs ../libexec/bluetooth/bluetoothd $PKG/usr/sbin

# Move the dbus config file to the system directory (we're replacing it anyway, so this is just in case)
mkdir -vpm755 $PKG/usr/share/dbus-1/system.d/
mv -fv $PKG/etc/dbus-1/system.d/* $PKG/usr/share/dbus-1/system.d/
rmdir --parents $PKG/etc/dbus-1/system.d/

# Install the non-systemd service file. Not all sessions run systemd --user
# (actually, the majority don't even on machines using that thing), so the dbus
# daemon must be able to spawn obexd directly, and to do so it needs the full
# path of the daemon.
mkdir -p $PKG/usr/share/dbus-1/services
cp -a $CWD/config/org.bluez.obex.service $PKG/usr/share/dbus-1/services
chmod 644 $PKG/usr/share/dbus-1/services/org.bluez.obex.service

# Install default options config file:
mkdir -p $PKG/etc/default
cat $CWD/config/defaultconfig > $PKG/etc/default/bluetooth.new

# Add an init script
mkdir -vpm755 $PKG/etc/rc.d
# Let that start by default, unless an existing init script is not executable:
install -vpm755 $CWD/config/rc.bluetooth $PKG/etc/rc.d/rc.bluetooth.new

# Make the other config files .new:
for config in input.conf main.conf network.conf ; do
  mv -fv $PKG/etc/bluetooth/${config} $PKG/etc/bluetooth/${config}.new
done

# If the deprecated utility gatttool was built, go ahead and install it:
if [ -x attrib/gatttool ]; then
  install -vpm755 attrib/gatttool $PKG/usr/bin/
fi

# Copy docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a \
  AUTHORS COPYING* INSTALL README* TODO \
  $PKG/usr/doc/$PKGNAM-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Remove that header if we added it:
if [ ! -z "$NOINCMGMT" ]; then
   echo "*** Removing /usr/include/mgmt.h ***"
   rm -fv /usr/include/mgmt.h
fi

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
