# Maintainer: Jack L. Frost <fbt@fleshless.org>
# Modified from the upstream Arch system PKGBUILD

pkgbase=systemd-libs
pkgname=( 'systemd-libs-systemd' 'systemd-libs-udev' )
# Upstream versioning is incompatible with pacman's version comparisons, one
# way or another. We use proper version for pacman here (no dash for rc
# release!), and change in source array below.
pkgver=261
pkgrel=1
arch=('x86_64')
license=('LGPL-2.1-or-later')
url='https://www.github.com/systemd/systemd'
makedepends=('acl' 'apparmor' 'cryptsetup' 'docbook-xsl' 'gperf' 'lz4' 'xz' 'pam' 'libelf'
             'intltool' 'iptables' 'kmod' 'libarchive' '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' 'linux-headers')
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>
# For pacman's version comparisons $pkgver is missing the dash that would be
# in an upstream rc version so add it back when fetching the tag from github.
source=("git+https://github.com/systemd/systemd#tag=v${pkgver/rc/-rc}?signed"
        '0001-Use-Arch-Linux-device-access-groups.patch')

sha256sums=('3647acaee7daaf2e58432c2c224b61cb477e1580b2278717dbbdeaa55249253f'
            '3ed6376fa18f9f79413c49ca0a04543476649217365c27cd396c83e867a3dffc')

_meson_version="${pkgver}-${pkgrel}"
_systemd_src_dir=systemd

if ((_systemd_UPSTREAM)); then
  _meson_version="${pkgver}"
fi

# Some heuristics to detect that we are building on OBS, with no network access. Skip
# git verification, and use the OBS-provided tarball instead. The sources will be
# unpacked by OBS in $package-$version/
if [ -f /.build/build.dist ] && [ -d /usr/src/packages/SOURCES ] &&  [ -d /usr/src/packages/BUILD ] &&  [ -d /usr/src/packages/OTHER ]; then
  source[0]="$(find . -name "${pkgbase}-${pkgver}.tar.*" -print -quit)"
  sha512sums[0]='SKIP'
  _systemd_src_dir="${pkgbase}-${pkgver}"
fi

_backports=(
)

_reverts=(
)

prepare() {
  cd "${_systemd_src_dir}"

  # return if not a git repository
  if ! git status >/dev/null 2>&1; then
    return
  fi

  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
}

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

  local _meson_options=(
    -Dversion-tag="${_meson_version}-arch"
    -Dvcs-tag=false
    -Dshared-lib-tag="${_meson_version}"
    -Dmode=release

    -Dapparmor=enabled
    -Dbootloader=enabled
    -Dxenctrl=disabled
    -Dbpf-framework=enabled
    -Dima=false
    -Dinstall-tests=true
    -Dlibidn2=enabled
    -Dlz4=enabled
    -Dman=enabled
    -Dselinux=disabled
    -Dsshdprivsepdir=/usr/share/empty.sshd
    -Dsysupdated=enabled
    -Dvmlinux-h=provided
    -Dvmlinux-h-path=/usr/src/linux/vmlinux.h

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

    -Dsbat-distro='arch'
    -Dsbat-distro-summary='Arch Linux'
    -Dsbat-distro-pkgname="${pkgname}"
    -Dsbat-distro-version="${pkgver}"
    -Dsbat-distro-url="https://archlinux.org/packages/core/x86_64/${pkgname}/"
  )

  arch-meson "${_systemd_src_dir}" build "${_meson_options[@]}" $MESON_EXTRA_CONFIGURE_OPTIONS

  meson compile -C build
}

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

  meson install -C build --no-rebuild --destdir "${srcdir}/pkg" --quiet

  # We don't need the whole thing
  install -d -m0755 systemd-libs/lib/
  mv "${srcdir}/pkg"/usr/lib/lib{nss,systemd,udev}*.so* systemd-libs/lib/
  mv "${srcdir}/pkg"/usr/lib/pkgconfig systemd-libs/lib/pkgconfig
  mv "${srcdir}/pkg"/usr/include systemd-libs/include
  mv "${srcdir}/pkg"/usr/share/man/man3 systemd-libs/man3 

  install -d -m0755 "$pkgdir/usr/share/man"
  install -d -m0755 "$pkgdir/usr/lib"
  install -d -m0755 "$pkgdir/usr/include"

  mv systemd-libs/lib/lib{nss,systemd}*.so* "$pkgdir/usr/lib"
  mv systemd-libs/include/systemd "$pkgdir/usr/include/systemd"
  mv systemd-libs/man3 "$pkgdir/usr/share/man/man3"
}

package_systemd-libs-udev() {
  pkgdesc='systemd client libraries, udev'
  depends=('glibc' 'libgcc' 'libgcc_s.so' 'libgcrypt' 'lz4' 'xz'  'zstd')
  license+=(
    'CC0-1.0' # siphash
    'GPL-2.0-or-later WITH Linux-syscall-note' # src/basic/linux/*
  )
  provides=('libudev.so')

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

  mv systemd-libs/lib/libudev*.so* "$pkgdir/usr/lib"
  mv systemd-libs/lib/pkgconfig/libudev.pc "$pkgdir/usr/lib/pkgconfig/libudev.pc"
  mv systemd-libs/include/libudev.h "$pkgdir/usr/include/libudev.h"
}

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