# Maintainer (arch): Christian Hesse <mail@eworm.de>
# Maintainer: Luke Shumaker <lukeshu@parabola.nu>
# Contributor: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contributor: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
# Contributor: bill-auger <bill-auger@programmer.net>

# Tips for the maintainer (i.e.: future me?):
#
#  - I check in the .SRCINFO file because it makes it easier to verify
#    that the metadata changes are what you expect.
#  - I use the `./srcinfo.sh` script to update .SRCINFO after
#    `libremakepkg`ing the package.

# There are a lot of pkgnames in this file, whether in the pkgname=()
# array, or in provides=(), conflicts=(), or replaces=().  Let's go
# over where each comes from and what it means, for clarity.
#
# Inherited from Arch:
#  * systemd's current pkgname=():
#    - `systemd`: Duh
#    - `systemd-libs`: `libsystemd.so`, `libudev.so`, and the NSS modules
#    - `systemd-resolvconf`: resolvconf(1)
#    - `systemd-sysvcompat`: init(1), halt(8), poweroff(8), reboot(8), shutdown(8)
#    - `systemd-ukify`: ukify(1)
#  * historical systemd pkgname=() values:
#    - `udev`: existed as a split package 2012-02 to 2012-05, when it merged into
#      `systemd-tools`
#    - `systemd-tools`: existed as a split package 2012-02 to 2012-08, when it
#      merged into `systemd`
#    - `libsystemd`: the name of `systemd-libs` prior to 2019-02
#  * separate packages:
#    - `systemd-sysvcompat`: existed as a separate package (not a split package)
#      2012-02 to 2012-06
#    - `nss-myhostname`: a separate that existed first in `extra` then in `core`,
#      2012-02 to 2013-01, when it became part of systemd
#    - `sysvinit`: a separate package that existed in `core` until 2013-05
#    - `openresolv`: prior to 2018-06 this was Arch's only `resolvconf`
#      implementation, so most packages depended on it instead of the `resolvconf`
#      virtual package
#  * virtual packages:
#    - `resolvconf`: since 2018-06, resolvconf(1)
#
# From Parabola (mostly by the decision-making of lukeshu):
#  * systemd's current pkgname=():
#    - We further split `systemd` into:
#       + `systemd`: systemd itself
#       + `systemd-udev`: just udev
#       + `systemd-common`: files shared between `systemd` and `systemd-udev`
#    - We further split `systemd-libs` into a separate package for each
#      library and NSS module:
#       + `systemd-libsystemd`
#       + `systemd-libudev`
#       + `systemd-nss-systemd`
#       + `systemd-nss-myhostname`
#       + `systemd-nss-mymachines`
#       + `systemd-nss-resolve`
#      Separating libsystemd and libudev are obvious.  I can make a good case for
#      separating nss-myhostname, because it's useful on nonsystemd boxes.  But I
#      guess I (lukeshu) separated out the other NSS modules too just for
#      consistency with nss-myhostname?  I'm sure I felt strongly about it at the
#      time.
#  * separate packages:
#    - `notsystemd-resolvconf`: existed 2018-07 to 2018-08, was identical to
#      `systemd-resolvconf`
#  * virtual packages:
#    - `init`: `/usr/bin/init`; invented by lukeshu in order to have mutual
#      provides/conflicts between `systemd-sysvcompat`, `sysvinit`, and
#      `openrc-init`.
#    - `libsystemd-standalone`: `libsystemd.so`; the pkgname has `-standalone`
#      appended because we inherited from Arch a different (historical) meaning of
#      what the `libsystemd` pkgname is.
#    - `libudev`: `libudev.so`
#    - `nss-systemd`: `libnss_systemd.so`
#    - `nss-myhostname`: `libnss_myhostname.so`
#    - `nss-mymachines`: `libnss_mymachines.so`
#    - `nss-resolve`: `libnss_resolve.so`

pkgbase=systemd
pkgname=('systemd'
         'systemd-libs'
         'systemd-resolvconf'
         'systemd-sysvcompat'
         'systemd-ukify')
pkgname+=('systemd-common' 'systemd-udev')
# We split Arch's systemd-libs into systemd-$X, for the following $X:
_systemd_libs=('libsystemd' 'libudev' 'nss-systemd' 'nss-myhostname' 'nss-mymachines' 'nss-resolve')
pkgname+=("${_systemd_libs[@]/#/systemd-}")
_tag='4003dd6754e3446691402d3cc389fbfd4faccc90' # git rev-parse v${_tag_name}
_tag_name=255.4
# Upstream versioning is incompatible with pacman's version comparisons so we
# replace tildes with the empty string to make sure pacman's version comparing
# does the right thing for rc versions:
# ➜ vercmp 255~rc1 255
# 1
# ➜ vercmp 255rc1 255
# -1
pkgver="${_tag_name/~/}"
pkgrel=2
pkgrel+=.parabola1
arch=('x86_64')
arch+=('i686' 'armv7h' 'ppc64le')
license=('LGPL-2.1-or-later')
url='https://www.github.com/systemd/systemd'
makedepends=('acl' 'cryptsetup' 'docbook-xsl' 'gperf' 'lz4' 'xz' 'pam' 'libelf'
             'intltool' 'iptables' 'kmod' 'libcap' 'libidn2' 'libgcrypt'
             'libmicrohttpd' 'libxcrypt' 'libxslt' 'util-linux' 'linux-api-headers'
             'python-jinja' 'python-lxml' 'quota-tools' 'shadow' 'git'
             'meson' 'libseccomp' 'pcre2' 'audit' 'kexec-tools' 'libxkbcommon'
             'bash-completion' 'p11-kit' 'systemd' 'libfido2' 'tpm2-tss' 'rsync'
             'bpf' 'libbpf' 'clang' 'llvm' 'curl' 'gnutls' 'python-pyelftools'
             'libpwquality' 'qrencode' 'lib32-gcc-libs' 'python-pefile')
makedepends=(${makedepends[*]/lib32-gcc-libs}) # only include lib32-gcc-libs on x86_64
makedepends_x86_64=('lib32-gcc-libs')
makedepends_i686=('pkgconf') # FIXME(lukeshu): Why do we need to request `pkgconf` on i686?
conflicts=("mkinitcpio<38-1")
validpgpkeys=('63CDA1E5D3FC22B998D20DD6327F26951A015CC4'  # Lennart Poettering <lennart@poettering.net>
              'A9EA9081724FFAE0484C35A1A81CEA22BC8C7E2E'  # Luca Boccassi <luca.boccassi@gmail.com>
              '9A774DB5DB996C154EBBFBFDA0099A18E29326E1'  # Yu Watanabe <watanabe.yu+github@gmail.com>
              '5C251B5FC54EB2F80F407AAAC54CA336CFEB557E') # Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
source=("git+https://github.com/systemd/systemd-stable#tag=${_tag}?signed"
        "git+https://github.com/systemd/systemd#tag=v${_tag_name%.*}?signed"
        '0001-Use-Arch-Linux-device-access-groups.patch'
        '0001-FSDG-bootctl-Say-Systemd-Boot-Manager-instead-of-Lin.patch'
        '0002-FSDG-os-release-Default-PRETTY_NAME-to-GNU-Linux-ins.patch'
        '0003-FSDG-os-release-Default-NAME-to-GNU-Linux-instead-of.patch'
        '0004-FSDG-os-release-Default-ID-to-gnu-linux-instead-of-l.patch'
        '0005-FSDG-systemd-resolved-Fallback-hostname-to-gnu-linux.patch'
        '0006-FSDG-man-Mention-Parabola-instead-of-Arch.patch'
        '0007-FSDG-man-Refer-to-the-operating-system-as-GNU-Linux.patch'
        # bootloader files
        'parabola.conf'
        'loader.conf'
        'splash-parabola.bmp'
        # pam configuration
        'systemd-user.pam'
        # pacman / libalpm hooks
        'systemd-hook'
        '20-systemd-sysusers.hook'
        '30-systemd-binfmt.hook'
        '30-systemd-catalog.hook'
        '30-systemd-daemon-reload-system.hook'
        '30-systemd-daemon-reload-user.hook'
        '30-systemd-hwdb.hook'
        '30-systemd-sysctl.hook'
        '30-systemd-tmpfiles.hook'
        '30-systemd-udev-reload.hook'
        '30-systemd-update.hook')
sha512sums=('SKIP'
            'SKIP'
            '3ccf783c28f7a1c857120abac4002ca91ae1f92205dcd5a84aff515d57e706a3f9240d75a0a67cff5085716885e06e62597baa86897f298662ec36a940cf410e'
            'b01d28646bca80c33192a10728218427b95246f904d921d00451bd32673f6352aefe6a8168942e796f4b7d1682921cc5c6adb895c9d9cdc0365606bfd4eca391'
            'fb1f75ded29d635c9e746ce76a99ace4e79694eea6d6ec7e8a84d7700e92ee5698576b979e16d71316c3e9276f285e3db248f74e8e55a8cf719456f6abc362b1'
            '03df948013f353e3fb6db745a01520c2dc8278b823e57178aca1c59786b81dd8a2e10e7f603e33028e1e9084a753cb7a09ebd4b4291dbdda30a16eba39628866'
            '2e89361d6af47464b91a04dc4be7a941ff12894b56831702deb6063d8d386141ff04989d1ecb39dbc452d83caacd5830c8eb6b06350c4bee6ccd3383aa2b1c8f'
            '0b634f674468e6c859e7faff9bbf9d310a2c1feac2ceb4bfd5cb132e9b706f512908c1c905c72181395a5f0d0af40600e62ef46ce1ab534beb0b8d16aba0a2be'
            '819331360737c59bc6fa2ca681da16bdae599d83289d619da802c4ffff2fe3b72852b11d50caecc058c80022b6b5158c5309560670fa2c30c08e9bb14b7e0454'
            'eb4ca9b4460f19ac93885d8e22ae4e844d0798918d692d84333e4f1b5ae6fa28a2443d156e31202c1c05f97c3f61531a8822f1b0e66ccc00e20743b871322aa8'
            '70b3f1d6aaa9cd4b6b34055a587554770c34194100b17b2ef3aaf4f16f68da0865f6b3ae443b3252d395e80efabd412b763259ffb76c902b60e23b6b522e3cc8'
            '6c6f579644ea2ebb6b46ee274ab15110718b0de40def8c30173ba8480b045d403f2aedd15b50ad9b96453f4ad56920d1350ff76563755bb9a80b10fa7f64f1d9'
            'a50bc85061a9a16d776235099867bc1a17c17dddb74c1ecf5614c849735a9779c5e34e5ddca8ca6af4b59a40f57c08ecf219b98cab09476ddb0f110e6a35e45c'
            'b90c99d768dc2a4f020ba854edf45ccf1b86a09d2f66e475de21fe589ff7e32c33ef4aa0876d7f1864491488fd7edb2682fc0d68e83a6d4890a0778dc2d6fe19'
            '3cb8f88c1bffc753d0c540be5d25a0fdb9224478cca64743b5663340f2f26b197775286e6e680228db54c614dcd11da1135e625674a622127681662bec4fa886'
            '299dcc7094ce53474521356647bdd2fb069731c08d14a872a425412fcd72da840727a23664b12d95465bf313e8e8297da31259508d1c62cc2dcea596160e21c5'
            '0d6bc3d928cfafe4e4e0bc04dbb95c5d2b078573e4f9e0576e7f53a8fab08a7077202f575d74a3960248c4904b5f7f0661bf17dbe163c524ab51dd30e3cb80f7'
            '2b50b25e8680878f7974fa9d519df7e141ca11c4bfe84a92a5d01bb193f034b1726ea05b3c0030bad1fbda8dbb78bf1dc7b73859053581b55ba813c39b27d9dc'
            'a436d3f5126c6c0d6b58c6865e7bd38dbfbfb7babe017eeecb5e9d162c21902cbf4e0a68cf3ac2f99815106f9fa003b075bd2b4eb5d16333fa913df6e2f3e32a'
            '190112e38d5a5c0ca91b89cd58f95595262a551530a16546e1d84700fc9644aa2ca677953ffff655261e8a7bff6e6af4e431424df5f13c00bc90b77c421bc32d'
            'a1661ab946c6cd7d3c6251a2a9fd68afe231db58ce33c92c42594aedb5629be8f299ba08a34713327b373a3badd1554a150343d8d3e5dfb102999c281bd49154'
            '9426829605bbb9e65002437e02ed54e35c20fdf94706770a3dc1049da634147906d6b98bf7f5e7516c84068396a12c6feaf72f92b51bdf19715e0f64620319de'
            'da7a97d5d3701c70dd5388b0440da39006ee4991ce174777931fea2aa8c90846a622b2b911f02ae4d5fffb92680d9a7e211c308f0f99c04896278e2ee0d9a4dc'
            'a50d202a9c2e91a4450b45c227b295e1840cc99a5e545715d69c8af789ea3dd95a03a30f050d52855cabdc9183d4688c1b534eaa755ebe93616f9d192a855ee3'
            '825b9dd0167c072ba62cabe0677e7cd20f2b4b850328022540f122689d8b25315005fa98ce867cf6e7460b2b26df16b88bb3b5c9ebf721746dce4e2271af7b97')

_backports=(
)

_reverts=(
)

prepare() {
  cd "$pkgbase-stable"

  # add upstream repository for cherry-picking
  git remote add -f upstream ../systemd

  local _c _l
  for _c in "${_backports[@]}"; do
    if [[ "${_c}" == *..* ]]; then _l='--reverse'; else _l='--max-count=1'; fi
    git log --oneline "${_l}" "${_c}"
    git cherry-pick --mainline 1 --no-commit "${_c}"
  done
  for _c in "${_reverts[@]}"; do
    if [[ "${_c}" == *..* ]]; then _l='--reverse'; else _l='--max-count=1'; fi
    git log --oneline "${_l}" "${_c}"
    git revert --mainline 1 --no-commit "${_c}"
  done

  # Replace cdrom/dialout/tape groups with optical/uucp/storage
  patch -Np1 -i ../0001-Use-Arch-Linux-device-access-groups.patch

  # apply FSDG patches
  local _patch
  for _patch in "$srcdir"/????-FSDG-*.patch; do
    echo "applying $_patch"
    patch -Np1 -i "$_patch"
  done
}

build() {
  local _timeservers=({0..3}.arch.pool.ntp.org)
  local _nameservers=(
    # We use these public name services, ordered by their privacy policy (hopefully):
    #  * Cloudflare (https://1.1.1.1/)
    #  * Quad9 (https://www.quad9.net/)
    #  * Google (https://developers.google.com/speed/public-dns/)
    '1.1.1.1#cloudflare-dns.com'
    '9.9.9.9#dns.quad9.net'
    '8.8.8.8#dns.google'
    '2606:4700:4700::1111#cloudflare-dns.com'
    '2620:fe::9#dns.quad9.net'
    '2001:4860:4860::8888#dns.google'
  )

  if [ "$CARCH" = "armv7h" ]; then
    LDFLAGS+=" -Wl,-fuse-ld=bfd"
    CFLAGS+=" -fno-lto"
    CXXFLAGS+=" -fno-lto"
  fi

  local _meson_options=(
    -Dversion-tag="${_tag_name}-${pkgrel}-parabola"
    # We use the version without tildes as the shared library tag because
    # pacman looks at the shared library version.
    -Dshared-lib-tag="${pkgver}-${pkgrel}"
    -Dmode=release

    -Dapparmor=false
    -Dbootloader=true
    -Dxenctrl=false
    -Dbpf-framework=true
    -Dima=false
    -Dlibidn2=true
    -Dlz4=true
    -Dman=true
    -Dnscd=false
    -Dselinux=false

    # We disable DNSSEC by default, it still causes trouble:
    # https://github.com/systemd/systemd/issues/10579

    -Ddbuspolicydir=/usr/share/dbus-1/system.d
    -Ddefault-dnssec=no
    -Ddefault-hierarchy=unified
    -Ddefault-kill-user-processes=false
    -Ddefault-locale='C.UTF-8'
    -Dlocalegen-path=/usr/bin/locale-gen
    -Ddns-over-tls=openssl
    -Dfallback-hostname='parabola'
    -Dnologin-path=/usr/bin/nologin
    -Dntp-servers="${_timeservers[*]}"
    -Ddns-servers="${_nameservers[*]}"
    -Drpmmacrosdir=no
    -Dsysvinit-path=
    -Dsysvrcnd-path=

    -Dsbat-distro='parabola'
    -Dsbat-distro-summary='Parabola GNU/Linux-libre'
    -Dsbat-distro-pkgname="${pkgname}"
    -Dsbat-distro-version="${pkgver}"
    -Dsbat-distro-url="https://www.parabola.nu/packages/libre/${CARCH}/${pkgname}/"
  )

  arch-meson "$pkgbase-stable" build "${_meson_options[@]}"

  meson compile -C build

  # Go ahead and split the package now.  It's easier this way, because
  # we can use `mv` instead of awkward+error-prone pairs of `rm`ing
  # the files in package_systemd() and then re-creating them in the
  # other package_ functions.

  # Put things in the main `systemd` package by default.
  meson install -C build --destdir "$srcdir/dest/systemd"

  # Parabola: Pull out `systemd-common`.
  install -dm755 "$srcdir/dest/systemd-common"/usr/lib/systemd
  mv -T "$srcdir/dest"/systemd{,-common}/usr/lib/systemd/"libsystemd-shared-${pkgver}-${pkgrel}.so"

  # Pull out `systemd-libs`.
  install -d -m0755 "$srcdir/dest/systemd-libs"/usr/{lib,share/man/man8}
  mv "$srcdir/dest/systemd"/usr/lib/lib{nss,systemd,udev}*.so* -t "$srcdir/dest/systemd-libs"/usr/lib/
  mv "$srcdir/dest/systemd"/usr/lib/pkgconfig                  -t "$srcdir/dest/systemd-libs"/usr/lib/
  mv "$srcdir/dest/systemd"/usr/include                        -t "$srcdir/dest/systemd-libs"/usr/
  mv "$srcdir/dest/systemd"/usr/share/man/man3                 -t "$srcdir/dest/systemd-libs"/usr/share/man/
  mv "$srcdir/dest/systemd"/usr/share/man/man8/{nss-,libnss_}* -t "$srcdir/dest/systemd-libs"/usr/share/man/man8/

  # Parabola: Further split `systemd-libs`
  {
    # Pull out `systemd-libsystemd`.
    install -dm755 "$srcdir/dest/systemd-libsystemd"/usr/{lib/pkgconfig,share/man/man3,include}
    mv -T "$srcdir/dest"/systemd-{libs,libsystemd}/usr/include/systemd
    mv -T "$srcdir/dest"/systemd-{libs,libsystemd}/usr/lib/pkgconfig/libsystemd.pc
    mv "$srcdir/dest/systemd-libs"/usr/lib/libsystemd.so*                 -t "$srcdir/dest/systemd-libsystemd"/usr/lib/
    mv "$srcdir/dest/systemd-libs"/usr/share/man/man3/{SD,sd,libsystemd}* -t "$srcdir/dest/systemd-libsystemd"/usr/share/man/man3/

    # Pull out `systemd-libudev`.
    install -dm755 "$srcdir/dest/systemd-libudev"/usr/{lib/pkgconfig,share/man/man3,include}
    mv -T "$srcdir/dest"/systemd-{libs,libudev}/usr/include/libudev.h
    mv -T "$srcdir/dest"/systemd-{libs,libudev}/usr/lib/pkgconfig/libudev.pc
    mv "$srcdir/dest/systemd-libs"/usr/lib/libudev.so*       -t "$srcdir/dest/systemd-libudev"/usr/lib/
    mv "$srcdir/dest/systemd-libs"/usr/share/man/man3/*udev* -t "$srcdir/dest/systemd-libudev"/usr/share/man/man3/

    # Pull out `systemd-nss-*`.
    local nssmodule
    for nssmodule in systemd myhostname mymachines resolve; do
      install -dm755 "$srcdir/dest/systemd-nss-$nssmodule"/usr/{lib,share/man/man8}
      mv -t "$srcdir/dest/systemd-nss-$nssmodule"/usr/share/man/man8/ -- \
         "$srcdir/dest/systemd-libs"/usr/share/man/man8/nss-$nssmodule.8 \
         "$srcdir/dest/systemd-libs"/usr/share/man/man8/libnss_$nssmodule.*.8
      mv -t "$srcdir/dest/systemd-nss-$nssmodule"/usr/lib/ -- \
         "$srcdir/dest/systemd-libs"/usr/lib/libnss_$nssmodule.so*
    done

    # Verify that there's nothing left in `systemd-libs`.
    diff -u /dev/null <(find "$srcdir/dest/systemd-libs" -not -type d)
  }

  # Parabola: Pull out `systemd-udev`.
  # NB: Must do this *after* pulling out `systemd-libudev`.
  install -dm755 "$srcdir/dest/systemd-udev"/{etc,usr/lib}
  mv -T "$srcdir/dest"/systemd{,-udev}/etc/udev/
  mv -T "$srcdir/dest"/systemd{,-udev}/usr/lib/udev/
  while read -d '' -r file; do
    install -dm755 "$srcdir/dest/systemd-udev/${file%/*}"
    mv -T "$srcdir/dest/systemd/$file" "$srcdir/dest/systemd-udev/$file"
  done < <(find "$srcdir/dest/systemd" \( -name '*udev*' -o -name '*hwdb*' \) -printf '%P\0')

  # Pull out `systemd-ukify`.
  install -d -m0755 "$srcdir/dest/systemd-ukify"/usr/{bin,lib/{systemd,kernel/install.d},share/man/man1}
  mv "$srcdir/dest/systemd"/usr/bin/ukify                             -t "$srcdir/dest/systemd-ukify"/usr/bin/
  mv "$srcdir/dest/systemd"/usr/lib/systemd/ukify                     -t "$srcdir/dest/systemd-ukify"/usr/lib/systemd/
  mv "$srcdir/dest/systemd"/usr/share/man/man1/ukify.1                -t "$srcdir/dest/systemd-ukify"/usr/share/man/man1/
  # we move the ukify hook itself, but keep 90-uki-copy.install in place,
  # because there are other ways to generate UKIs w/o ukify, e.g. w/ mkinitcpio
  mv "$srcdir/dest/systemd"/usr/lib/kernel/install.d/60-ukify.install -t "$srcdir/dest/systemd-ukify"/usr/lib/kernel/install.d

  # Pull out `systemd-sysvcompat`.
  install -d -m0755 "$srcdir/dest/systemd-sysvcompat"/usr/{bin,share/man/man{1,8}}
  mv "$srcdir/dest/systemd"/usr/share/man/man1/init.1                            -t "$srcdir/dest/systemd-sysvcompat"/usr/share/man/man1/
  mv "$srcdir/dest/systemd"/usr/share/man/man8/{halt,poweroff,reboot,shutdown}.8 -t "$srcdir/dest/systemd-sysvcompat"/usr/share/man/man8/
  mv "$srcdir/dest/systemd"/usr/bin/{halt,init,poweroff,reboot,shutdown}         -t "$srcdir/dest/systemd-sysvcompat"/usr/bin/

  # Pull out `systemd-resolvconf`.
  install -d -m0755 "$srcdir/dest/systemd-resolvconf"/usr/{bin,share/man/man1}
  mv "$srcdir/dest/systemd"/usr/share/man/man1/resolvconf.1 -t "$srcdir/dest/systemd-resolvconf"/usr/share/man/man1/
  mv "$srcdir/dest/systemd"/usr/bin/resolvconf              -t "$srcdir/dest/systemd-resolvconf"/usr/bin/
}

check() {
  # FIXME: tests fail on armv7h
  # $ grep -E 'TIMEOUT|FAIL' systemd-253-1.parabola1-armv7h-check.log
  #  377/1081 systemd:netlink / test-netlink          FAIL      0.72s  killed by signal 6 SIGABRT
  #  414/1081 systemd:network / test-network          FAIL      0.88s  killed by signal 6 SIGABRT
  #  451/1081 systemd:resolve / test-resolved-stream  FAIL      6.38s  killed by signal 6 SIGABRT
  #  452/1081 systemd:test / test-blockdev-util       FAIL      2.24s  killed by signal 6 SIGABRT
  #  467/1081 systemd:test / test-raw-clone           FAIL      0.56s  killed by signal 6 SIGABRT
  #  480/1081 systemd:test / test-mountpoint-util     FAIL      0.61s  killed by signal 6 SIGABRT
  #  495/1081 systemd:test / test-stat-util           FAIL      0.60s  killed by signal 6 SIGABRT
  #  533/1081 systemd / test-sysusers                 TIMEOUT  30.32s  killed by signal 15 SIGTERM
  #  534/1081 systemd / test-systemctl-enable         TIMEOUT  30.43s  killed by signal 15 SIGTERM
  #  537/1081 systemd:test / test-sigbus              TIMEOUT  30.36s  killed by signal 15 SIGTERM
  #  559/1081 systemd:test / test-fileio              FAIL      1.93s  killed by signal 6 SIGABRT
  [[ "${CARCH}" == armv7h ]] ||
  meson test -C build --print-errorlogs
}

# How we use this function might seem like "overthinking it", and it
# sorta is, but it's doing the (tedious, error-prone) comparisons that
# I'd otherwise have to do manually when merging from Arch.
_lib_deps() {
  # Resolve short names, to make calling code readable.
  # Where, relative to $srcdir, each .so file lives.
  declare -A paths=(
    [libsystemd-shared]="dest/systemd-common/usr/lib/systemd/libsystemd-shared-${pkgver}-${pkgrel}.so"
    [libsystemd]='dest/systemd-libsystemd/usr/lib/libsystemd.so'
    [libudev]='dest/systemd-libudev/usr/lib/libudev.so'
    [libnss_systemd]='dest/systemd-nss-systemd/usr/lib/libnss_systemd.so.2'
    [libnss_myhostname]='dest/systemd-nss-myhostname/usr/lib/libnss_myhostname.so.2'
    [libnss_mymachines]='dest/systemd-nss-mymachines/usr/lib/libnss_mymachines.so.2'
    [libnss_resolve]='dest/systemd-nss-resolve/usr/lib/libnss_resolve.so.2'
  )

  local libname libfile txtfile mode
  for libname in "$@"; do
    # Save the raw list to a textfile so that `makepkg --printsrcinfo`
    # works.
    libfile="${_existing_srcdir:-${srcdir:-.}}/${paths[$libname]}"
    txtfile="srcinfo-${libname}.txt"

    # mode is: 'gen', 'lib', 'txt', 'nop', or 'err:*'
    mode="err:TXTFILE:'${txtfile}' and LIBFILE:'${libfile}' are both missing"
    if [[ -n "${_existing_srcdir:-}" ]]; then
      if [[ -f "$libfile" ]]; then
	mode=gen
      else
	mode="err:LIBFILE:'${libfile}' is missing"
      fi
    elif [[ -f "$libfile" ]]; then
      mode=lib
    elif [[ -f "$txtfile" ]]; then
      mode=txt
    elif [[ "${FUNCNAME[*]}" == *' lint_depends '* ]]; then
      mode=nop
    fi
    case "$mode" in
      gen) LC_ALL=C readelf -d "$libfile" | sed -nE 's/.* Shared library: \[(lib.*\.so)(\.[0-9.]+)?\]/\1/p' | tee "$txtfile";;
      lib) LC_ALL=C readelf -d "$libfile" | sed -nE 's/.* Shared library: \[(lib.*\.so)(\.[0-9.]+)?\]/\1/p';;
      txt) cat "$txtfile";;
      nop) true;;
      err:*) error '%s' "${mode#err:}";;
    esac
  done |
    # Add some redundant transitive dependecies, idk why, it's what
    # Arch does.
    sed -e 's/^libpam\.so$/libaudit.so\n&/' |
    # Refer to these by their just pkgname instead of .so, idk why,
    # it's what Arch does.
    sed -e 's/^lib[cm]\.so$/glibc/' \
	-e 's/^libgcc_s\.so$/gcc-libs/' \
	-e 's/^libgcrypt\.so$/libgcrypt/' \
	-e 's/^libip4tc\.so$/iptables/' \
	-e 's/^liblz4\.so$/lz4/' \
	-e 's/^liblzma\.so$/xz/' \
	-e 's/^libpam\.so$/pam/' \
	-e 's/^libzstd\.so$/zstd/' |
    # Refer to these by both their pkgname and .so, idk why, it's what
    # Arch does.
    sed -E \
	-e 's/^libcrypt\.so$/libxcrypt\n&/' \
	-e 's/^libcrypto\.so$/openssl\n&/' \
	-e 's/^lib(mount|libblkid)\.so$/util-linux\n&/' \
	-e 's/^lib(acl|audit|kmod)\.so$/\1\n&/' \
	-e 's/^(lib(cap|seccomp))\.so$/\1\n&/' |
    # Sort and de-duplicate.
    sort -u
}

package_systemd() {
  pkgdesc='system and service manager'
  license+=(
    'CC0-1.0' # siphash # Parabola: ends up in systemd-common
    'GPL-2.0-or-later' # udev # Parabola: ends up in systemd-udev
    'MIT-0' # documentation and config files
  )
  depends=('acl' 'libacl.so' 'bash' 'cryptsetup' 'libcryptsetup.so' 'dbus'
           'dbus-units' 'kbd' 'kmod' 'libkmod.so' 'hwdata' 'libcap' 'libcap.so'
           'libgcrypt' 'libxcrypt' 'libcrypt.so' 'systemd-libs' 'libidn2' 'lz4' 'pam'
           'libelf' 'libseccomp' 'libseccomp.so' 'util-linux' 'libblkid.so'
           'libmount.so' 'xz' 'pcre2' 'audit' 'libaudit.so'
           'openssl' 'libcrypto.so' 'libssl.so')
  provides=('nss-myhostname' "systemd-tools=$pkgver" "udev=$pkgver")
  replaces=('nss-myhostname' 'systemd-tools' 'udev')
  conflicts=('nss-myhostname' 'systemd-tools' 'udev')
  optdepends=('libmicrohttpd: systemd-journal-gatewayd and systemd-journal-remote'
              'quota-tools: kernel-level quota management'
              'systemd-sysvcompat: symlink package to provide sysvinit binaries'
              'systemd-ukify: combine kernel and initrd into a signed Unified Kernel Image'
              'polkit: allow administration as unprivileged user'
              'curl: systemd-journal-upload, machinectl pull-tar and pull-raw'
              'gnutls: systemd-journal-gatewayd and systemd-journal-remote'
              'qrencode: show QR codes'
              'iptables: firewall features'
              'libbpf: support BPF programs'
              'libpwquality: check password quality'
              'libfido2: unlocking LUKS2 volumes with FIDO2 token'
              'libp11-kit: support PKCS#11'
              'tpm2-tss: unlocking LUKS2 volumes with TPM2')
  backup=(etc/pam.d/systemd-user
          etc/systemd/coredump.conf
          etc/systemd/homed.conf
          etc/systemd/journald.conf
          etc/systemd/journal-remote.conf
          etc/systemd/journal-upload.conf
          etc/systemd/logind.conf
          etc/systemd/networkd.conf
          etc/systemd/oomd.conf
          etc/systemd/pstore.conf
          etc/systemd/resolved.conf
          etc/systemd/sleep.conf
          etc/systemd/system.conf
          etc/systemd/timesyncd.conf
          etc/systemd/user.conf
          etc/udev/iocost.conf
          etc/udev/udev.conf)
  install=systemd.install

  # Parabola: Split the package.
  # (the "eval"/"outputvar" parts are so that `makepkg --printsrcinfo` works)
  license=($(eval "printf '%s\n' \"\${${outputvar:-license}[@]}\"|grep -vFx -e CC0-1.0 -e GPL-2.0-or-later")) # the CC0 parts end up in systemd-common, the GPL2 parts end up in systemd-udev
  depends=($(eval "printf '%s\n' \"\${${outputvar:-depends}[@]}\"|grep -vFx -f <(_lib_deps libsystemd-shared)"))
  depends+=("systemd-common=$pkgver-$pkgrel" 'udev')
  provides=($(eval "printf '%s\n' \"\${${outputvar:-provides}[@]}\"|grep -v -e nss-myhostname -e ^udev="))
  replaces=($(eval "printf '%s\n' \"\${${outputvar:-replaces}[@]}\"|grep -v -e nss-myhostname -e ^udev"))
  conflicts=($(eval "printf '%s\n' \"\${${outputvar:-conflicts}[@]}\"|grep -v -e nss-myhostname -e ^udev"))
  backup=($(eval "printf '%s\n' \"\${${outputvar:-backup}[@]}\"|grep -v -e etc/udev"))

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"

  # we'll create this on installation
  rmdir "$pkgdir"/var/log/journal/remote

  # avoid a potential conflict with [core]/filesystem
  rm "$pkgdir"/usr/share/factory/etc/{issue,nsswitch.conf}
  sed -i -e '/^C \/etc\/nsswitch\.conf/d' \
    -e '/^C \/etc\/issue/d' "$pkgdir"/usr/lib/tmpfiles.d/etc.conf

  # ship default policy to leave services disabled
  echo 'disable *' >"$pkgdir"/usr/lib/systemd/system-preset/99-default.preset

  # The group 'systemd-journal' is allocated dynamically and may have varying
  # gid on different systems. Let's install with gid 0 (root), systemd-tmpfiles
  # will fix the permissions for us. (see /usr/lib/tmpfiles.d/systemd.conf)
  install -d -o root -g root -m 2755 "$pkgdir"/var/log/journal

  # add example bootctl configuration
  install -D -m0644 parabola.conf "$pkgdir"/usr/share/systemd/bootctl/parabola.conf
  install -D -m0644 loader.conf "$pkgdir"/usr/share/systemd/bootctl/loader.conf
  install -D -m0644 splash-parabola.bmp "$pkgdir"/usr/share/systemd/bootctl/splash-parabola.bmp

  # pacman hooks
  #install -D -m0755 systemd-hook "$pkgdir"/usr/share/libalpm/scripts/systemd-hook Parabola: moved to package_systemd-common()
  install -D -m0644 -t "$pkgdir"/usr/share/libalpm/hooks *.hook
  rm -- "$pkgdir"/usr/share/libalpm/hooks/*{udev,hwdb}* # Parabola: we put these in our `systmed-udev` package

  # overwrite the systemd-user PAM configuration with our own
  install -D -m0644 systemd-user.pam "$pkgdir"/etc/pam.d/systemd-user
}

package_systemd-libs() {
  pkgdesc='systemd client libraries'
  depends=('glibc' 'gcc-libs' 'libcap' 'libgcrypt' 'lz4' 'xz' 'zstd')
  license+=(
    'CC0-1.0' # siphash
    'GPL-2.0-or-later WITH Linux-syscall-note' # src/basic/linux/*
    'MIT-0' # documentation
  )
  provides=('libsystemd' 'libsystemd.so' 'libudev.so')
  conflicts=('libsystemd')
  replaces=('libsystemd')

  # Parabola: First, sanity check that _lib_deps finds all of the
  # packages that are in Arch's depends=() list.
  diff -u /dev/null <(printf '%s\n' "${depends[@]}" | grep -vFx -f <(
    _lib_deps libsystemd libudev libnss_{systemd,myhostname,mymachines,resolve}))

  # Parabola: Split the package.
  pkgdesc+=' metapackage'
  depends=("${_systemd_libs[@]}")
  # (the "eval"/"outputvar" parts are so that `makepkg --printsrcinfo` works)
  provides=($(eval "printf '%s\n' \"\${${outputvar:-provides}[@]}\"|grep -v -e '\.so$'"))
  return

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
}

package_systemd-resolvconf() {
  pkgdesc='systemd resolvconf replacement (for use with systemd-resolved)'
  depends=('systemd')
  provides=('openresolv' 'resolvconf')
  conflicts=('resolvconf')

  replaces+=('notsystemd-resolvconf') # notsystemd-resolvconf should have never existed

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
}

package_systemd-sysvcompat() {
  pkgdesc='sysvinit compat for systemd'
  conflicts=('sysvinit')
  depends=('systemd')

  provides=('init')
  conflicts+=('init')

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
}

package_systemd-ukify() {
  pkgdesc='Combine kernel and initrd into a signed Unified Kernel Image'
  provides=('ukify')
  depends=('binutils' 'python-cryptography' 'python-pefile' 'systemd')
  optdepends=('python-pillow: Show the size of splash image'
              'sbsigntools: Sign the embedded kernel')

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
}

# vim:ft=sh syn=sh et sw=2:

# Parabola: The remainder of the file is our additional split packages.

package_systemd-common() {
  pkgdesc='systemd files shared between split packages'
  depends=($(_lib_deps libsystemd-shared))
  license+=(
	  'CC0-1.0' # siphash
  )

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
  install -D -m0755 systemd-hook "$pkgdir"/usr/share/libalpm/scripts/systemd-hook
}

package_systemd-udev() {
  pkgdesc='Userspace device file manager'
  license+=('GPL-2.0-or-later')
  depends=("systemd-common=$pkgver-$pkgrel" 'systemd-libudev'
           'hwdata' 'kmod' 'libkmod.so' 'util-linux' 'zlib')
  backup=(etc/udev/iocost.conf
          etc/udev/udev.conf)

  provides+=("${pkgname#systemd-}=$pkgver")
  conflicts+=("${pkgname#systemd-}")
  replaces+=("${pkgname#systemd-}")

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"

  # pacman hooks
  install -D -m0644 -t "$pkgdir"/usr/share/libalpm/hooks *{udev,hwdb}*.hook
}

# TODO(lukeshu): I might have been over-zealous adding the licenses
# for `siphash` and `src/basic/linux/*` to the following packages;
# audit whether those packages actually inclode that code.

package_systemd-libsystemd() {
  pkgdesc='systemd client library'
  depends=($(_lib_deps libsystemd))
  license+=(
    'CC0-1.0' # siphash
    'GPL-2.0-or-later WITH Linux-syscall-note' # src/basic/linux/*
    'MIT-0' # documentation
  )
  provides=('libsystemd.so')

  provides+=("libsystemd-standalone=$pkgver")
  conflicts+=('libsystemd-standalone')
  replaces+=('libsystemd-standalone')

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
}

package_systemd-libudev() {
  pkgdesc='systemd library for enumerating and introspecting local devices'
  depends=($(_lib_deps libudev))
  license+=(
    'CC0-1.0' # siphash
    'GPL-2.0-or-later WITH Linux-syscall-note' # src/basic/linux/*
  )
  provides=('libudev.so')

  provides+=("${pkgname#systemd-}=$pkgver")
  conflicts+=("${pkgname#systemd-}")
  replaces+=("${pkgname#systemd-}")

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
}

package_systemd-nss-systemd() {
  pkgdesc='NSS module providing user and group resolution for dynamic users and groups'
  depends=($(_lib_deps libnss_systemd))
  license+=(
    'CC0-1.0' # siphash
    'GPL-2.0-or-later WITH Linux-syscall-note' # src/basic/linux/*
  )

  provides+=("${pkgname#systemd-}=$pkgver")
  conflicts+=("${pkgname#systemd-}")
  replaces+=("${pkgname#systemd-}")

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
}

package_systemd-nss-myhostname() {
  pkgdesc='NSS module providing hostname resolution for the locally configured system hostname'
  depends=($(_lib_deps libnss_myhostname))
  license+=(
    'CC0-1.0' # siphash
    'GPL-2.0-or-later WITH Linux-syscall-note' # src/basic/linux/*
  )

  provides+=("${pkgname#systemd-}=$pkgver")
  conflicts+=("${pkgname#systemd-}")
  replaces+=("${pkgname#systemd-}")

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
}

package_systemd-nss-mymachines() {
  pkgdesc='NSS module providing hostname resolution for local systemd-machined container instances'
  depends=($(_lib_deps libnss_mymachines))
  license+=(
    'CC0-1.0' # siphash
    'GPL-2.0-or-later WITH Linux-syscall-note' # src/basic/linux/*
  )

  provides+=("${pkgname#systemd-}=$pkgver")
  conflicts+=("${pkgname#systemd-}")
  replaces+=("${pkgname#systemd-}")

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
}

package_systemd-nss-resolve() {
  pkgdesc='NSS module providing hostname resolution via systemd-resolved'
  depends=($(_lib_deps libnss_resolve))
  license+=(
    'CC0-1.0' # siphash
    'GPL-2.0-or-later WITH Linux-syscall-note' # src/basic/linux/*
  )

  provides+=("${pkgname#systemd-}=$pkgver")
  conflicts+=("${pkgname#systemd-}")
  replaces+=("${pkgname#systemd-}")

  cp -rT -d --no-preserve=ownership,timestamp "$srcdir/dest/$pkgname" "$pkgdir"
}
