# Maintainer: David P. <megver83@parabola.nu>
# Maintainer (Arch): Jan Alexander Steffens (heftig) <jan.steffens@gmail.com>
# Maintainer (Arch): Jan de Groot <jgc@archlinxu.org>
# Contributor (Arch): Wael Nasreddine <gandalf@siemens-mobiles.org>
# Contributor (Arch): Tor Krill <tor@krill.nu>
# Contributor (Arch): Will Rea <sillywilly@gmail.com>
# Contributor (Arch): Valentine Sinitsyn <e_val@inbox.ru>

pkgbase=networkmanager
pkgname=(networkmanager libnm)
pkgver=1.20.6
pkgrel=1
pkgrel+=.nonsystemd1
pkgdesc="Network connection manager and user applications"
url="https://wiki.gnome.org/Projects/NetworkManager"
arch=(x86_64 i686 armv7h)
license=(GPL2 LGPL2.1)
_pppver=2.4.7
makedepends=(intltool dhclient iptables gobject-introspection gtk-doc "ppp=$_pppver" modemmanager
             iproute2 nss polkit wpa_supplicant curl elogind libmm-glib
             libnewt libndp libteam vala perl-yaml python-gobject git vala jansson bluez-libs
             glib2-docs dhcpcd iwd dnsmasq openresolv libpsl audit meson)
checkdepends=(libx11 python-dbus)
_commit=7910cdfe5a9ffe02cd0780d56fcc66500e292d09  # tags/1.20.6^0
source=("git+https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git#commit=$_commit"
        NetworkManager.{confd,initd} 10-openrc-status)
sha256sums=('SKIP'
            '4594573f01fe5e04b6dde4525796acf909158591bdcefd662ec23fe0d1c3e1bd'
            '861445d46e8863dca26a91018da3dd9fee6a64467a93e7512774dfd88914539d'
            '1535237db113a76261d6f1ce0b24a55e956934b0c1c59e9fd624f621961327b6')

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

prepare() {
  cd NetworkManager
}

build() {
  local meson_args=(
    -D dbus_conf_dir=/usr/share/dbus-1/system.d
    -D dist_version="$pkgver-$pkgrel"
    -D session_tracking_consolekit=false
    -D suspend_resume=elogind
    -D modify_system=true
    -D polkit_agent=true
    -D selinux=false
    -D iwd=true
    -D pppd_plugin_dir=/usr/lib/pppd/$_pppver
    -D teamdctl=true
    -D bluez5_dun=true
    -D ebpf=true
    -D config_plugins_default=keyfile
    -D vapi=true
    -D docs=true
    -D more_asserts=no
    -D more_logging=false
    -D qt=false
    -D systemdsystemunitdir=no
    -D session_tracking=elogind
    -D systemd_journal=false
  )

  arch-meson NetworkManager build "${meson_args[@]}"
  ninja -C build
}

check() {
  # iproute2 bug
  # https://gitlab.freedesktop.org/NetworkManager/NetworkManager/commit/be76d8b624fab99cbd76092ff511e6adc305279c
  meson test -C build --print-errorlogs || :
}

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

package_networkmanager() {
  depends=(libnm iproute2 polkit wpa_supplicant libmm-glib libnewt libndp libteam curl
           bluez-libs libpsl audit elogind)
  optdepends=('dnsmasq: connection sharing'
              'bluez: Bluetooth support'
              'ppp: dialup connection support'
              'modemmanager: cellular network support'
              'iwd: wpa_supplicant alternative')
  provides=($pkgname-{elogind,openrc})
  replaces=($pkgname-{elogind,openrc})
  backup=(etc/NetworkManager/NetworkManager.conf)
  groups=(gnome)

  DESTDIR="$pkgdir" meson install -C build

  # /etc/NetworkManager
  install -d "$pkgdir"/etc/NetworkManager/{conf,dnsmasq}.d
  install -dm700 "$pkgdir/etc/NetworkManager/system-connections"
  install -m644 /dev/stdin "$pkgdir/etc/NetworkManager/NetworkManager.conf" <<END
# Configuration file for NetworkManager.
# See "man 5 NetworkManager.conf" for details.
END

  # packaged configuration
  install -Dm644 /dev/stdin "$pkgdir/usr/lib/NetworkManager/conf.d/20-connectivity.conf" <<END
[connectivity]
uri=http://www.archlinux.org/check_network_status.txt
END

### Split libnm
  _pick libnm "$pkgdir"/usr/include/libnm
  _pick libnm "$pkgdir"/usr/lib/girepository-1.0/NM-*
  _pick libnm "$pkgdir"/usr/lib/libnm.*
  _pick libnm "$pkgdir"/usr/lib/pkgconfig/libnm.pc
  _pick libnm "$pkgdir"/usr/share/gir-1.0/NM-*
  _pick libnm "$pkgdir"/usr/share/gtk-doc/html/libnm
  _pick libnm "$pkgdir"/usr/share/vala/vapi/libnm.*

  # Init script
  install -Dm755 ${srcdir}/NetworkManager.confd ${pkgdir}/etc/conf.d/NetworkManager
  install -Dm755 ${srcdir}/NetworkManager.initd ${pkgdir}/etc/init.d/NetworkManager
  install -Dm755 "${srcdir}/10-openrc-status" "${pkgdir}/etc/NetworkManager/dispatcher.d/10-openrc-status"

  sed -e 's|#!/sbin/openrc-run|#!/usr/bin/openrc-run|g' \
      -e 's|/var/run|/run|g' \
      -e 's|/usr/sbin|/usr/bin|g' \
      -e 's|use consolekit|use consolekit elogind|' \
      -i ${pkgdir}/etc/init.d/NetworkManager
}

package_libnm() {
  pkgdesc="NetworkManager client library"
  depends=(glib2 nss libutil-linux jansson libelogind)
  mv libnm/* "$pkgdir"
}

# vim:set sw=2 et:
