#!/bin/bash

# bluez-utils.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
#
# Copyright 2006, 2007  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.

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

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Create framework:
mkdir -p $PKG/etc/rc.d $PKG/etc/bluetooth/passkeys $PKG/usr/lib/bluetooth
# Is there any reason for a non-root user to be able to see these?
chmod 700 $PKG/etc/bluetooth/passkeys

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.bz2
cd $PKGNAM-$VERSION
slackhousekeeping

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr \
--sysconfdir=/etc \
--mandir=/usr/man \
--localstatedir=/var \
--enable-tools\
--enable-bccmd \
--enable-hid2hci \
--enable-dfutool \
--enable-hidd \
--enable-pand \
--enable-dund \
--enable-cups \
--enable-manpages \
--enable-configfiles \
--build=$ARCH-slackware-linux || failconfig

make $NUMJOBS || exit 1
make install DESTDIR=$PKG

mkdir -p $PKG/lib/udev
cp -a scripts/bluetooth_serial $PKG/lib/udev/bluetooth_serial || exit 1
chmod 755 $PKG/lib/udev/bluetooth_serial

mkdir -p $PKG/lib/udev/rules.d
cp -a scripts/bluetooth.rules $PKG/lib/udev/rules.d/60-bluetooth.rules || exit 1
chmod 644 $PKG/lib/udev/rules.d/60-bluetooth.rules

# This appears to be gone/obsolete:
#if [ ! -e $PKG/etc/bluetooth/transfer.service ]; then
#  cat transfer/transfer.service > $PKG/etc/bluetooth/transfer.service
#fi
#if [ ! -e $PKG/usr/lib/bluetooth/bluetoothd-service-transfer -a -e transfer/bluetoothd-service-transfer ]; then
#  cat transfer/bluetoothd-service-transfer > $PKG/usr/lib/bluetooth/bluetoothd-service-transfer
#  chmod 755 $PKG/usr/lib/bluetooth/bluetoothd-service-transfer
#fi

# Install Debian's add-passkey program:
DEBDEST=/usr/lib/bluetooth
gcc $(pkg-config --libs --cflags dbus-1) -DDBUS_API_SUBJECT_TO_CHANGE \
	$CWD/add-passkey.c -o $PKG/$DEBDEST/add-passkey || exit 1
cp $CWD/register-passkeys $PKG/$DEBDEST
chmod a+x $PKG/$DEBDEST/*

# Default PIN (wow, does this seem insecure!)
#echo "1234" > $PKG/etc/bluetooth/passkeys/default
touch $PKG/etc/bluetooth/passkeys/default

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a \
  AUTHORS ChangeLog COPYING* INSTALL NEWS README $CWD/README.SLACKWARE \
  $PKG/usr/doc/$NAME-$VERSION
( cd $PKG/etc/bluetooth
  ln -sf /usr/doc/$NAME-$VERSION/README.SLACKWARE .
)

# Arrange init scripts and config files:
CONF=/etc/rc.d/rc.bluetooth.conf
mkdir -p $PKG/etc/rc.d
cp $CWD/rc.bluetooth $PKG/etc/rc.d/rc.bluetooth
cp -a $CWD/rc.bluetooth.conf $PKG/etc/rc.d/rc.bluetooth.conf
sed -i "s:\(HIDD_ENABLE=\).*:\1false:" $PKG/$CONF || exit 1
sed -i "s:\(HID2HCI_ENABLE=\).*:\1false:" $PKG/$CONF || exit 1

mkdir -p $PKG/etc/bluetooth
cp -a audio/audio.conf $PKG/etc/bluetooth
cp -a audio/asound.conf $PKG/etc/bluetooth
cp -a input/input.conf $PKG/etc/bluetooth
cp -a hcid/hcid.conf $PKG/etc/bluetooth
cp -a network/network.conf $PKG/etc/bluetooth
cp -a rfcomm/rfcomm.conf $PKG/etc/bluetooth
chmod 644 $PKG/etc/bluetooth/*.conf

# Do not overwrite configuration
( cd $PKG
  for file in \
    etc/bluetooth/audio.conf \
    etc/asound.conf \
    etc/bluetooth/asound.conf \
    etc/bluetooth/input.conf \
    etc/bluetooth/hcid.conf \
    etc/bluetooth/network.conf \
    etc/bluetooth/rfcomm.conf \
    etc/bluetooth/passkeys/default \
    etc/rc.d/rc.bluetooth \
    etc/rc.d/rc.bluetooth.conf ; do
      mv ${file} ${file}.new
  done
)

# Replace the bluetooth.conf file with one from BlueZ4 that works with the newest dbus:
cat $CWD/bluetooth.conf > $PKG/etc/dbus-1/system.d/bluetooth.conf

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
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
