# Contributor: Ionut Biru <ibiru@archlinux.org>
# Contributor: Jan Alexander Steffens (heftig) <jan.steffens@gmail.com>

pkgbase=modemmanager
pkgname=(modemmanager libmm-glib)
pkgver=1.10.6
pkgrel=1
pkgdesc="Mobile broadband modem management service"
arch=(x86_64)
url="https://www.freedesktop.org/wiki/Software/ModemManager/"
license=(GPL2 LGPL2.1)
depends=(systemd libgudev polkit ppp libqmi libmbim)
makedepends=(gtk-doc gobject-introspection vala autoconf-archive git)
_commit=a0c790e09b93e4d3d2634eb3479c3c80b51585e7  # tags/1.10.6
source=("git+https://gitlab.freedesktop.org/mobile-broadband/ModemManager.git#commit=$_commit"
        strict-filter.diff)
sha256sums=('SKIP'
            '0602cc5088b193426c3af3d6ce14a7fd6502b06f77982dc56291d834280f311e')

pkgver() {
  cd ModemManager
  git describe --tags | sed 's/-rc/rc/;s/-/+/g'
}

prepare() {
  cd ModemManager

  # Recommended by upstream
  patch -Np1 -i ../strict-filter.diff

  NOCONFIGURE=1 ./autogen.sh
}

build() {
  cd ModemManager
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --sbindir=/usr/bin \
    --with-dbus-sys-dir=/usr/share/dbus-1/system.d \
    --with-udev-base-dir=/usr/lib/udev \
    --with-polkit=permissive \
    --with-systemd-journal \
    --with-systemd-suspend-resume \
    --enable-gtk-doc \
    --disable-static

  # https://bugzilla.gnome.org/show_bug.cgi?id=655517
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool

  make
}

check() {
  cd ModemManager
  make -k check
}

package_modemmanager() {
  depends+=(libmm-glib)
  optdepends=('usb_modeswitch: install if your modem shows up as a storage drive')
  options=(!emptydirs)

  cd ModemManager
  make DESTDIR="$pkgdir" install
  make DESTDIR="$pkgdir" -C libmm-glib uninstall
  make DESTDIR="$pkgdir" -C vapi uninstall

  # Some stuff to move is left over
  mv "$pkgdir/usr/include" ..
  mv "$pkgdir/usr/lib/pkgconfig" ..
}

package_libmm-glib() {
  pkgdesc="ModemManager library"
  depends=(glib2)

  install -d "$pkgdir/usr/lib"
  mv include "$pkgdir/usr"
  mv pkgconfig "$pkgdir/usr/lib"

  cd ModemManager
  make DESTDIR="$pkgdir" -C libmm-glib install
  make DESTDIR="$pkgdir" -C vapi install
}
