# 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 nm-cloud-setup)
pkgver=1.32.2
pkgrel=1
pkgrel+=.nonsystemd1
pkgdesc="Network connection manager and user applications"
url="https://wiki.gnome.org/Projects/NetworkManager"
arch=(x86_64)
arch+=(i686 armv7h)
license=(GPL2)
_pppver=2.4.9
makedepends=(intltool dhclient dhcpcd iptables-nft gobject-introspection gtk-doc
             "ppp=$_pppver" modemmanager iproute2 nss polkit wpa_supplicant curl
             logind libmm-glib libnewt libndp libteam nftables vala perl-yaml
             python-gobject git vala jansson bluez-libs glib2-docs iwd dnsmasq
             openresolv libpsl audit meson)
checkdepends=(libx11 python-dbus)
_commit=bf1b09702ed442a3efe426c8ceba4ef1e6bb1902  # 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'
            'eaf7d210540ed9f4c6b4ba81ddbb30a2aa5ce8b528057f7d64ce3478981f88e4'
            'd32a21c0683cf7a09370b35b7e3d3b3f28f5d4d242ecde2c866cfb400b94bcbe')

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

prepare() {
  cd NetworkManager
}

build() {
  local meson_args=(
    # system paths
    -D dbus_conf_dir=/usr/share/dbus-1/system.d

    # platform
    -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 systemdsystemunitdir=no
    -D session_tracking=elogind
    -D systemd_journal=false

    # features
    -D iwd=true
    -D pppd_plugin_dir=/usr/lib/pppd/$_pppver
    -D teamdctl=true
    -D nm_cloud_setup=true
    -D bluez5_dun=true
    -D ebpf=true

    # configuration plugins
    -D config_plugins_default=keyfile

    # handlers for resolv.conf
    -D netconfig=no
    -D config_dns_rc_manager_default=symlink

    # miscellaneous
    -D vapi=true
    -D docs=true
    -D more_asserts=no
    -D more_logging=false
    -D qt=false
  )

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

check() {
  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 mobile-broadband-provider-info elogind)
  optdepends=('dnsmasq: connection sharing'
              'nftables: connection sharing'
              'iptables: connection sharing'
              'bluez: Bluetooth support'
              'ppp: dialup connection support'
              'modemmanager: cellular network support'
              'iwd: wpa_supplicant alternative'
              'dhclient: alternative DHCP client'
              'dhcpcd: alternative DHCP client'
              'openresolv: alternative resolv.conf manager'
              'firewalld: firewall support')
  provides=($pkgname-{elogind,openrc})
  replaces=($pkgname-{elogind,openrc})
  backup=(etc/NetworkManager/NetworkManager.conf)

  meson install -C build --destdir "$pkgdir"

  cd "$pkgdir"

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

  # packaged configuration
  install -Dm644 /dev/stdin usr/lib/NetworkManager/conf.d/20-connectivity.conf <<END
[connectivity]
uri=http://ping.archlinux.org/nm-check.txt
END

  shopt -s globstar

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

  _pick nm-cloud-setup "$pkgdir"/usr/lib/**/*nm-cloud-setup*
#  _pick nm-cloud-setup "$pkgdir"/usr/share/man/*/nm-cloud-setup* (Parabola)

  # Restore empty dir
# mkdir "$pkgdir/usr/lib/NetworkManager/dispatcher.d/no-wait.d" (Parabola)

  # 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"

}

package_libnm() {
  pkgdesc="NetworkManager client library"
  depends=(glib2 nss util-linux-libs jansson libelogind)
  license=(LGPL)
  provides=(libnm.so)

  mv libnm/* "$pkgdir"
}

package_nm-cloud-setup() {
  pkgdesc="Automatically configure NetworkManager in cloud"
  depends=(networkmanager)

  mv nm-cloud-setup/* "$pkgdir"
}

# vim:set sw=2 et:
