#!/bin/sh
#
# SlackBuild for bluez-utils
# http://www.bluez.org
# By SukkoPera <enjoy.the.silence@iol.it>
# Thanks a lot to CAT for his Slackware package cration
# guide (http://www.slacky.it/misto/tutorial/spunleashed.txt)
#
# Check out
# - http://www.sukkopera.tk
# - http://www.slacky.it
# - http://www.nyft.org
#
# Notes:
# - Many files and the whole PIN management system have been stolen from the
#   Debian package. Thanks.
#
# Rewritten for Slackware 2007-05  <pjv>

NAME=bluez-utils
PKGNAM=$NAME

# Some useful variables about the package
VERSION=${VERSION:-3.26}
BUILD=${BUILD:-1}

. /etc/pkghelpers
pkghelpers_env

rm -rf $PKG
mkdir -p $PKG
mkdir -p $PKG/etc/rc.d $PKG/etc/bluetooth/passkeys $PKG/usr/lib$LIBSUFFIX/bluetooth
# Is there any reason for a non-root user to be able to see these?
chmod 700 $PKG/etc/bluetooth/passkeys
cd $TMP
rm -rf $NAME-$VERSION
tar xjvf $CWD/${NAME}-${VERSION}.tar.bz2 || exit 1
cd $NAME-$VERSION

pkghelpers_permissions

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr \
--libdir=/usr/lib$LIBSUFFIX \
--sysconfdir=/etc \
--mandir=/usr/man \
--localstatedir=/var \
--enable-all \
--build=$ARCH-$DISTRO-linux

make -j4 || exit 1
make install DESTDIR=$PKG

mkdir -p $PKG/lib/udev
mv $PKG/usr/libexec/udev/bluetooth_serial $PKG/lib/udev/bluetooth_serial
rmdir $PKG/usr/libexec/udev
rmdir $PKG/usr/libexec

mkdir -p $PKG/etc/udev/rules.d
mv $PKG/etc/udev/bluetooth.rules $PKG/etc/udev/rules.d/60-bluetooth.rules

if grep -q ATTR $PKG/etc/udev/rules.d/60-bluetooth.rules ; then
  # Use fixed rules file for now:
  cat $CWD/60-bluetooth.rules > $PKG/etc/udev/rules.d/60-bluetooth.rules
fi

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$LIBSUFFIX/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
install -m 644 -o root -g root \
  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
CONF=/etc/rc.d/rc.bluetooth.conf
#mv $PKG/etc/init.d/bluetooth $PKG/etc/rc.d/rc.bluetooth
cp $CWD/rc.bluetooth $PKG/etc/rc.d/rc.bluetooth
mv $PKG/etc/default/bluetooth $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
rm -rf $PKG/etc/{init.d,default}

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

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

cd $PKG
pkghelpers_fixup
pkghelpers_makepkg

