# Maintainer: PktSurf <smlinux@pktsurf.in>
app=bluez
version=5.40
build=1sml
homepage="https://www.bluez.org"
download=""
desc="Bluetooth libraries and utilities"
requires="libical alsa-lib json-c glib eudev dbus icu"

prepbuilddir() {
  mkandenterbuilddir
  rm -rf $app-$version

  tar xf $srcdir/$app-$version.tar.?z*
  cd $app-$version
  fixbuilddirpermissions

  applypatch $srcdir/bluez-5.30-obexd_without_systemd-1.patch
  [ "$arch" = "aarch64" ] && applypatch $srcdir/bluez-5.40-rpi-fixes.patch

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

build() {
  ./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

  [[ -d $pkg/usr/lib/systemd ]] && rm -r $pkg/usr
 
  mkfinalpkg
}

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