APP=bluez
VERSION=5.40
BUILD=1sml
HOMEPAGE="http://www.bluez.org"
DOWNLOAD=""
DESC="Bluetooth libraries and utilities"
REQUIRES="libical alsa-lib json-c glib eudev dbus icu"

build() {
  mkandenterbuilddir
  rm -rf $APP-$VERSION

  tar xf $SRCDIR/$APP-$VERSION.tar.?z*
  cd $APP-$VERSION
  fixbuilddirpermissions

  patch -p1 < $SRCDIR/bluez-5.30-obexd_without_systemd-1.patch
  [ "$ARCH" = "aarch64" ] && patch -p1 < $SRCDIR/bluez-5.40-rpi-fixes.patch

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

  ./configure \
    --prefix="" \
    --sbindir=/bin \
    --sysconfdir=/etc \
    --libexecdir=/lib \
    --enable-library \
    --disable-systemd \
    --enable-deprecated 

  make 
  make install DESTDIR=$PKG

  cp COPYING* $PKGDOCS/

  # Use reset=1 for the btusb module, which supposedly fixes reconnect problems:
  install -Dm 644 $SRCDIR/btusb.conf $PKG/etc/modprobe.d/btusb.conf

  # Install the configuration files
  mkdir -p $PKG/etc/bluetooth $PKG/etc/default
  cat src/main.conf > $PKG/etc/bluetooth/main.conf.new
  cat profiles/input/input.conf > $PKG/etc/bluetooth/input.conf.new
  cat profiles/network/network.conf > $PKG/etc/bluetooth/network.conf.new
  cat profiles/proximity/proximity.conf > $PKG/etc/bluetooth/proximity.conf.new
  
  cat $SRCDIR/uart.conf > $PKG/etc/bluetooth/uart.conf.new
  cat $SRCDIR/defaultconfig > $PKG/etc/default/bluetooth.new

  if [ "$ARCH" = "aarch64" ]; then
    cat $SRCDIR/uart.conf.pi3 > $PKG/etc/bluetooth/uart.conf.pi3.new
    cat $SRCDIR/uart.conf.pi4 > $PKG/etc/bluetooth/uart.conf.pi4.new
    install -Dm 644 $SRCDIR/100-bluetooth.rules $PKG/etc/udev/rules.d/100-bluetooth.rules
    install -Dm 755 $SRCDIR/btuart $PKG/bin/btuart
  fi

  install -Dm 755 $SRCDIR/rc.bluetooth $PKG/etc/rc.d/rc.bluetooth.new
 
  mkfinalpkg
}

SHA512SUMS="
9f392eaabaef1104d09c8820350cb1c2e7fb09c13cfaa68742459c26d35e00a4aa860963dec345e1b48deba591f1ecebfa5131c7689b30129fd9f28dc4e5405e  bluez-5.40.tar.lz
41ce7ccf78cca97563f0ef31e01dac6eb4484c24fe57be360b5e8de8c5bff5845e9d395766f891bd3f123788344456c88c9fc00cd1bb7c6a1dca89d09f19172b  bluez-5.30-obexd_without_systemd-1.patch
bd6004cf97ade648fcaecc4c363e4d4c6ce2ac8ff66c66d66ec1b1ba445806e2163a765d04b70be97454b1e9cdfee26879bda34a7a197195c11834e1456e98b8  bluez-5.40-rpi-fixes.patch
"
