# Maintainer: Andreas Radke <andyrtr@archlinux.org>
# Maintainer: Robin Candau <antiz@archlinux.org>
# Contributor: Tom Gundersen <teg@jklm.no>
# Contributor: Andrea Scarpino <andrea@archlinux.org>
# Contributor: Geoffroy Carrier <geoffroy@archlinux.org>

pkgbase=bluez
pkgname=('bluez' 'bluez-utils' 'bluez-libs' 'bluez-cups' 'bluez-deprecated-tools' 'bluez-hid2hci' 'bluez-mesh' 'bluez-obex')
pkgver=5.87
pkgrel=2.2
url="http://www.bluez.org/"
arch=('x86_64')
license=('GPL-2.0-only')
# Holo change: The python explicit version dependency is used for all our
# packages. It helps us avoid retaining old packages when we bump Python.
# By explicitly depending on the right version, packages are forced to rebuild.
makedepends=('dbus' 'libical' 'systemd' 'alsa-lib' 'json-c' 'ell' 'python-docutils' 'python-pygments' 'cups' 'git' 'python>=3.14' 'python<3.15')
source=(#https://www.kernel.org/pub/linux/bluetooth/${pkgname}-${pkgver}.tar.{xz,sign}
        # "git+https://github.com/bluez/bluez.git?signed#tag=$pkgver"
        "git+https://github.com/bluez/bluez.git#tag=$pkgver"
        bluetooth.modprobe

        0001-valve-bluetooth-config.patch  # Holo: Enable compatibility with devices like AirPods Pro
        0014-shared-gatt-Add-env-variable-to-prefer-indication-ov.patch # Holo: For Bluetooth qualification tests GAP/SEC/SEM/BV-56-C, GAP/SEC/SEM/BV-57-C and GAP/SEC/SEM/BV-58-C # not upstreamable
        0018-disable-unreliable-vcp-tests.patch  # Holo: TODO
        0019-plugins-Add-new-plugin-to-manage-wake-policy.patch  # Holo: TODO
        0020-plugins-wake-policy-Only-allow-Peripherals-to-wake-u.patch  # Holo: TBD
        0021-valve-bluetooth-ll-privacy.patch  # Holo: TBD

        # Holo: Fix for the Steam Controller that could block a suspend request
        # Part of https://gitlab.steamos.cloud/holo-team/tasks/-/issues/1267
        # At the moment of writing, the upstream patch has still not being merged
        # https://lore.kernel.org/all/CABBYNZKFEBuW2OeU4uOSfku=-jCnn3oXJENDMBGmkqP-4rybDA@mail.gmail.com/t/#u
        0001-BlueZ-adapter-Fix-execute-LE-Add-Device-To-Resolving.patch
        # Holo: Fix unstable BT connection with Nintendo Switch Pro v1
        0024-Modify-Nintendo-gamepad-abnormal-disconnect-during-use.patch)
b2sums=('9abcc5de17102c1307396627c87cffb5585e1c9b099cfd33d2a682d93f10e48a178fe8651c16ab9bb788d80f603f91db75e9fcadf7b627017202402d4ec396e5'
        '0ce33d13b796d4ae2fd688b17742b3a3055663b68a352b02720dac82bcfdaa47bf2ee2a034dfb8ef4ddf3f827bc58fe80548730939c2915f64864cea39c10170'
        '355c0dfce71d3e832fb5b091a26afcfc1f7f7b890befee3eb525505c4cacaf5c8121d097a1724ba0ad70b74c3ddffef17046747e93c241f948304663643d8316'
        'ec40df76b5e20fbb41a70bd9930f587ee172e16a39218666607708d3b6873f29959e89945e06c8b1212d5ad8eb4aec98c5f39a7c844cbdd9d1b096dd02f728d2'
        '2e5087a348599131aeb2343a0e68846c04e3ea3ff45488c2f82f6c82087a4403012ce6bacdad75d39f13770368661b07af83174b3e23c64a5838863a2752a0dc'
        'e8df6143b7ce3541b94fff37a2488c574753467e1ccc7d852652c7a6e889350bceb95ef3ad5809c001fd6982d066a7e6796a3b7f358f658ff2fb33e1ef859abb'
        '78e0d61ae87c8e12805cd09872abe3e1be2c5a68ea7fe814e5e938f16ebb519d1f758bd776d26ed970fecd2a905bae33ae2d5afd2c5973106ed0faf9a743e4a5'
        'f8e7344443cbd28700a087dc76331f38503a1a18b9813009f1989733ff2e6ffac75a6ae83a56d3cc534c50f8de57c7d915b05b3afdad8bc66550198d5d3a8e2c'
        '041d344b81ea91bce7702019436c28126bb6f7afb3870d642f667c5e0f1ba42625a825e1db02b94556cd8b91b357e6ffde89bfe7f5e13f048ea719bebaed88a8'
        '320ad8f3cede12214c2ad399b490ff69a22efee52e0b51fa84f4420d67692463300830266588ea02d50f738d602c682620a0051d832ade1269e9ce437e72d22a')
# validpgpkeys=('E932D120BC2AEC444E558F0106CA9F5D1DCF2659') # Marcel Holtmann <marcel@holtmann.org>

prepare() {
  cd "${pkgname}"

  for patch in "${source[@]}"
  do
    case $patch in
      *.patch)
        echo "# Applying ${patch}"
        patch -p1 -i "${srcdir}"/"${patch}"
        ;;
    esac
  done

  autoreconf -vfi
}

build() {
  cd "${pkgname}"
  ICAL_LIBS="-lical -licalvcal" \
  ./configure \
          --prefix=/usr \
          --mandir=/usr/share/man \
          --sysconfdir=/etc \
          --localstatedir=/var \
          --libexecdir=/usr/lib \
          --with-dbusconfdir=/usr/share \
          --enable-btpclient \
          --enable-midi \
          --enable-sixaxis \
          --enable-mesh \
          --enable-hid2hci \
          --enable-experimental \
          --enable-datafiles \
          --enable-external-ell \
          --enable-library --enable-deprecated # libraries and these tools are deprecated
  make

  # fake installation to be seperated into packages
  make DESTDIR="${srcdir}/fakeinstall" install

  # add missing tools FS#41132, FS#41687, FS#42716
  for files in `find tools/ -type f -perm -755`; do
    filename=$(basename $files)
    install -Dm755 "${srcdir}"/"${pkgbase}"/tools/$filename "${srcdir}/fakeinstall"/usr/bin/$filename
  done

  for files in `find client/btpclient/ -type f -perm -755`; do
    filename=$(basename $files)
    install -Dm755 "${srcdir}"/"${pkgbase}"/client/btpclient/$filename "${srcdir}/fakeinstall"/usr/bin/$filename
  done
  

  # add man pages for the above tools
  # https://github.com/bluez/bluez/commit/44e3dd321e4be052bcde40939552b93b734538d3
  for manfile in `find doc/ -type f -regex '.*\.[1-8]'`; do
    section=$(echo $manfile | sed -E 's/.*\.([1-8])$/\1/')
    filename=$(basename $manfile)
    install -Dm644 "${srcdir}"/"${pkgbase}"/doc/$filename "${srcdir}/fakeinstall"/usr/share/man/man${section}/$filename
  done
}

_install() {
  local src f dir
  for src; do
    f="${src#fakeinstall/}"
    dir="${pkgdir}/${f%/*}"
    install -m755 -d "${dir}"
    # use copy so a new file is created and fakeroot can track properties such as setuid
    cp -av "${src}" "${dir}/"
    rm -rf "${src}"
  done
}

check() {
  cd "$pkgname"
  make check
}


package_bluez() {
  pkgdesc="Daemons for the bluetooth protocol stack"
  depends=('systemd-libs' 'dbus' 'glib2' 'alsa-lib' 'glibc')
  backup=(etc/bluetooth/{main,input,network}.conf)

  _install fakeinstall/etc/bluetooth/main.conf
  _install fakeinstall/etc/bluetooth/input.conf
  _install fakeinstall/etc/bluetooth/network.conf
  _install fakeinstall/usr/lib/bluetooth/bluetoothd
  _install fakeinstall/usr/lib/systemd/system/bluetooth.service
  _install fakeinstall/usr/share/dbus-1/system-services/org.bluez.service
  _install fakeinstall/usr/share/dbus-1/system.d/bluetooth.conf
  _install fakeinstall/usr/share/man/man8/bluetoothd.8

  # bluetooth.service wants ConfigurationDirectoryMode=0555
  chmod -v 555 "${pkgdir}"/etc/bluetooth

  # add basic documention
  install -dm755 "${pkgdir}"/usr/share/doc/"${pkgbase}"/dbus-apis
  cp -a "${pkgbase}"/doc/*.txt "${pkgdir}"/usr/share/doc/"${pkgbase}"/dbus-apis/
  # fix module loading errors
  install -dm755 "${pkgdir}"/usr/lib/modprobe.d
  install -Dm644 "${srcdir}"/bluetooth.modprobe "${pkgdir}"/usr/lib/modprobe.d/bluetooth-usb.conf
  # load module at system start required by some functions
  # https://bugzilla.kernel.org/show_bug.cgi?id=196621
  install -dm755 "$pkgdir"/usr/lib/modules-load.d
  echo "crypto_user" > "$pkgdir"/usr/lib/modules-load.d/bluez.conf
}

package_bluez-utils() {
  pkgdesc="Development and debugging utilities for the bluetooth protocol stack"
  depends=('dbus' 'systemd-libs' 'glib2' 'glibc' 'readline')
  optdepends=('ell: for btpclient'
              'perl: for parse_companies.pl')
  
  provides=('bluez-plugins')
  replaces=('bluez-plugins')

  _install fakeinstall/usr/bin/{advtest,avinfo,avtest,bcmfw,bdaddr,bluemoon,bluetoothctl,bluetooth-player,bneptest,btattach,btconfig,btgatt-client,btgatt-server,btinfo,btiotest,btmgmt,btmon,btpclient,btpclientctl,btproxy,btsnoop,check-selftest,cltest,create-image,eddystone,gatt-service,hcieventmask,hcisecfilter,hex2hcd,hid2hci,hwdb,ibeacon,isotest,l2ping,l2test,mpris-proxy,nokfw,oobtest,parse_companies.pl,rctest,rtlfw,scotest,seq2bseq,test-runner,update_compids.sh}
  _install fakeinstall/usr/lib/systemd/user/mpris-proxy.service
  _install fakeinstall/usr/share/man/man1/bluetoothctl*.1
  # _install fakeinstall/usr/share/man/man1/{bdaddr,btattach,btmgmt,btmon,isotest,l2ping,rctest}.1
  _install fakeinstall/usr/share/man/man1/{btattach,btmon,isotest,l2ping,rctest}.1
  _install fakeinstall/usr/share/man/man5/org.bluez.{A,B,C,D,G,I,L,M,N,P,T}*.5
  _install fakeinstall/usr/share/man/man7/{btsnoop,hci,l2cap,mgmt,sco,iso}.7
  _install fakeinstall/usr/share/zsh/site-functions/_bluetoothctl
}

package_bluez-deprecated-tools() {
  pkgdesc="Deprecated tools that are no longer maintained"
  depends=('json-c' 'systemd-libs' 'glib2' 'dbus' 'readline' 'glibc')

  _install fakeinstall/usr/bin/{ciptool,hciattach,hciconfig,hcitool,meshctl,rfcomm,sdptool}
  _install fakeinstall/usr/share/man/man1/{ciptool,hciattach,hciconfig,hcitool,rfcomm,sdptool}.1
  _install fakeinstall/usr/share/man/man7/rfcomm.7
}

package_bluez-libs() {
  pkgdesc="Deprecated libraries for the bluetooth protocol stack"
  depends=('glibc')
  provides=('libbluetooth.so')
  license=('LGPL-2.1-only')

  _install fakeinstall/usr/include/bluetooth/*
  _install fakeinstall/usr/lib/libbluetooth.so*
  _install fakeinstall/usr/lib/pkgconfig/*
}

package_bluez-cups() {
  pkgdesc="CUPS printer backend for Bluetooth printers"
  depends=('cups' 'glib2' 'glibc' 'dbus')

  _install fakeinstall/usr/lib/cups/backend/bluetooth
}

package_bluez-hid2hci() {
  pkgdesc="Put HID proxying bluetooth HCI's into HCI mode"
  depends=('systemd-libs' 'glibc')

  _install fakeinstall/usr/lib/udev/*
  _install fakeinstall/usr/share/man/man1/hid2hci.1
}

package_bluez-mesh() {
  pkgdesc="Services for bluetooth mesh"
  depends=('ell' 'json-c' 'readline' 'glibc')
  backup=('etc/bluetooth/mesh-main.conf')

  _install fakeinstall/etc/bluetooth/mesh-main.conf
  _install fakeinstall/usr/bin/{mesh-cfgclient,mesh-cfgtest}
  _install fakeinstall/usr/lib/bluetooth/bluetooth-meshd
  _install fakeinstall/usr/lib/systemd/system/bluetooth-mesh.service
  _install fakeinstall/usr/share/dbus-1/system-services/org.bluez.mesh.service
  _install fakeinstall/usr/share/dbus-1/system.d/bluetooth-mesh.conf
  _install fakeinstall/usr/share/man/man8/bluetooth-meshd.8

  # bluetooth.service wants ConfigurationDirectoryMode=0555
  chmod -v 555 "${pkgdir}"/etc/bluetooth
}

package_bluez-obex() {
  pkgdesc="Object Exchange daemon for sharing content"
  depends=('glib2' 'libical' 'dbus' 'readline' 'systemd-libs' 'glibc')

  _install fakeinstall/usr/bin/{obexctl,obex-client-tool,obex-server-tool}
  _install fakeinstall/usr/lib/bluetooth/obexd
  _install fakeinstall/usr/lib/systemd/user/obex.service
  _install fakeinstall/usr/share/dbus-1/services/org.bluez.obex.service
  _install fakeinstall/usr/share/dbus-1/system.d/obex.conf
  _install fakeinstall/usr/lib/systemd/user/dbus-org.bluez.obex.service
  _install fakeinstall/usr/share/man/man5/org.bluez.obex*.5  

  # make sure there are no files left to install
  rm fakeinstall/usr/lib/libbluetooth.la
  find fakeinstall -depth -print0 | xargs -0 rmdir
}
