# PKGBUILD stripped to only provide libsystemd and libsystemd-udev.
# Original Arch maintainers:
# Maintainer: Christian Hesse <mail@eworm.de>
# Maintainer: Dave Reisner <dreisner@archlinux.org>
# Maintainer: Tom Gundersen <teg@jklm.no>

pkgbase=libsystemd-standalone
pkgname=('libsystemd-standalone' 'libsystemd-udev')
# latest commit on stable branch
_commit='743b771c559c6101544f7358a42c8c519fe4b0db'
# Bump this to latest major release for signed tag verification,
# the commit count is handled by pkgver() function.
pkgver=241.0
pkgrel=3
arch=('x86_64')
url="https://www.github.com/systemd/systemd"
makedepends=('acl' 'cryptsetup' 'docbook-xsl' 'gperf' 'lz4' 'xz' 'pam' 'libelf'
             'intltool' 'iptables' 'kmod' 'libcap' 'libidn' 'libgcrypt'
             'libmicrohttpd' 'libxslt' 'util-linux' 'linux-api-headers'
             'python-lxml' 'quota-tools' 'shadow' 'gnu-efi-libs' 'git'
             'meson' 'libseccomp')
options=('strip')
validpgpkeys=('63CDA1E5D3FC22B998D20DD6327F26951A015CC4')  # Lennart Poettering <lennart@poettering.net>
source=('git://github.com/systemd/systemd-stable.git'
        'git://github.com/systemd/systemd.git') # pull in for tags, backports & reverts

_backports=(
	# Fix typo in statx macro (#7180) (FS#56289)
	'8e6a7a8b2be409d356bcaface00f6d44390c07ff'
)

_reverts=(
)

_validate_tag() {
  local success fingerprint trusted status tag=v${pkgver%.*}

  parse_gpg_statusfile /dev/stdin < <(git verify-tag --raw "$tag" 2>&1)

  if (( ! success )); then
    error 'failed to validate tag %s\n' "$tag"
    return 1
  fi

  if ! in_array "$fingerprint" "${validpgpkeys[@]}" && (( ! trusted )); then
    error 'unknown or untrusted public key: %s\n' "$fingerprint"
    return 1
  fi

  case $status in
    'expired')
      warning 'the signature has expired'
      ;;
    'expiredkey')
      warning 'the key has expired'
      ;;
  esac

  return 0
}

pkgver() {
  local version count

  cd "systemd-stable"

  version="$(git describe --abbrev=0 --tags)"
  count="$(git rev-list --count ${version}..)"
  printf '%s.%s' "${version#v}" "${count}"
}

prepare() {
  cd "systemd-stable"

  git remote add upstream ../systemd/
  git fetch --all

  git checkout "${_commit}"

  _validate_tag || return

  local _commit
  for _commit in "${_backports[@]}"; do
    git cherry-pick -n "$_commit"
  done
  for _commit in "${_reverts[@]}"; do
    git revert -n "$_commit"
  done
}

build() {
  local timeservers=({0..3}.arch.pool.ntp.org)

  local meson_options=(
    -Daudit=false
    -Dgnuefi=true
    -Dima=false
    -Dlz4=true

    -Ddbuspolicydir=/usr/share/dbus-1/system.d
    -Ddefault-dnssec=no
    # TODO(dreisner): consider changing this to unified
    -Ddefault-hierarchy=hybrid
    -Ddefault-kill-user-processes=false
    -Dfallback-hostname='archlinux'
    -Dntp-servers="${timeservers[*]}"
    -Drpmmacrosdir=no
    -Dsysvinit-path=
    -Dsysvrcnd-path=
  )

  arch-meson "systemd-stable" build "${meson_options[@]}"

  ninja -C build

  DESTDIR="$srcdir"/full-install ninja -C build install
}

package_libsystemd-standalone() {
  pkgdesc="systemd client libraries"
  depends=('glibc' 'libcap' 'libgcrypt' 'lz4' 'xz')
  license=('GPL2')
  provides=('libsystemd' 'libsystemd.so' 'systemd-libs')

  install -dm755 "$pkgdir"/usr/lib/
  cp --archive "$srcdir"/full-install/usr/lib/lib{nss_*,systemd}.so* "$pkgdir"/usr/lib/

  install -dm755 "$pkgdir"/usr/lib/pkgconfig/
  cp --archive "$srcdir"/full-install/usr/lib/pkgconfig/libsystemd.pc "$pkgdir"/usr/lib/pkgconfig/

  install -dm755 "$pkgdir"/usr/lib/include/systemd/
  cp --archive "$srcdir"/full-install/usr/include/systemd/*.h "$pkgdir"/usr/lib/include/systemd/
}

package_libsystemd-udev() {
  pkgdesc="udev client libraries"
  depends=('glibc' 'libcap' 'libgcrypt' 'lz4' 'xz')
  license=('GPL2')
  provides=('libudev' 'libudev.so')

  install -dm755 "$pkgdir"/usr/lib/
  cp --archive "$srcdir"/full-install/usr/lib/libudev.so* "$pkgdir"/usr/lib/

  install -dm755 "$pkgdir"/usr/lib/pkgconfig/
  cp --archive "$srcdir"/full-install/usr/lib/pkgconfig/libudev.pc "$pkgdir"/usr/lib/pkgconfig/

  install -dm755 "$pkgdir"/usr/lib/include/
  cp --archive "$srcdir"/full-install/usr/include/libudev.h "$pkgdir"/usr/lib/include/
}

# vim: ft=sh syn=sh et
sha1sums=('SKIP'
          'SKIP')
