#!/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, 2010, 2011, 2012, 2013  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 xvvf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || exit 1
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:
#
zcat $CWD/bluez-5.51-obexd_without_systemd-1.patch.gz | patch -p1 --verbose || exit 1

sed -i -e 's|-lreadline|\0 -lncursesw|g' Makefile.{in,tools}
autoreconf -vif

# Configure:
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 || exit 1

# Install into package:
make install DESTDIR=$PKG

# 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
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/

# Fix substitution in /usr/share/dbus-1/services/org.bluez.obex.service:
if [ -x $PKG/usr/libexec/bluetooth/obexd ]; then
  sed -i "s|@libexecdir@|/usr/libexec|g" $PKG/usr/share/dbus-1/services/org.bluez.obex.service
fi

# Install the configuration files
mkdir -vpm755 $PKG/etc/bluetooth $PKG/etc/default
install -vpm644 src/main.conf $PKG/etc/bluetooth/main.conf.new
install -vpm644 profiles/input/input.conf $PKG/etc/bluetooth/input.conf.new
install -vpm644 profiles/network/network.conf $PKG/etc/bluetooth/network.conf.new
install -vpm644 $CWD/config/uart.conf $PKG/etc/bluetooth/uart.conf.new
install -vpm644 $CWD/config/defaultconfig $PKG/etc/default/bluetooth.new

# 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

# 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

# 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


