pkgname="systemd"
pkgver=257.8
pkgdesc="system and service manager"
homepage="https://www.freedesktop.org/wiki/Software/systemd/"
license=("LGPL-2.1-or-laterC" "C0-1.0" "GPL-2.0-or-later" "MIT-0")
dependences=(
  sys-libs/pam
  sys-fs/btrfs-progs
  dev-libs/libgcrypt
  sys-libs/libseccomp
  app-misc/make-ca
  dev-libs/pcre2
  net-misc/rsync
  dev-debug/valgrind
  app-shells/zsh
  dev-python/lxml
)
_systemdhooks=("20-systemd-sysusers.HOOK"
  "21-systemd-tmpfiles.HOOK"
  "25-systemd-binfmt.HOOK"
  "25-systemd-catalog.HOOK"
  "25-systemd-hwdb.HOOK"
  "30-systemd-daemon-reload-system.HOOK"
  "30-systemd-daemon-reload-user.HOOK"
  "35-systemd-udev-reload.HOOK"
  "35-systemd-update.HOOK"
)
_systemdhookmu5sums=("a566b0107a97b603a7ee48e28fb59817"
  "5bb19c1c2964721cf41258990501404e"
  "8f6cabd1b4e96a1c11c2feb0989ddfd6"
  "d49449a2435dc635a39cd46316c271a7"
  "34b38916bf4111fc0b40fdcded1d3a93"
  "49ffd7e5c810f8b3cf14fb9535d6a4c5"
  "e38543ca711f6ff063174b82afe3cbb3"
  "db079083de34b7e3201d8e9b6a3900ab"
  "7b34445517de5da3ef1db422e4b80f8c"
)
sources=("${pkgname}-${pkgver}.tar.gz"
  ${_systemdhooks[@]}
)
urls=("https://github.com/systemd/systemd/archive/v${pkgver}/${sources[0]}"
  ${_systemdhooks[@]}
)
md5sums=("25fe5d328e22641254761f1baa74cee0"
  ${_systemdhookmu5sums[@]}
)

src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
  sed -e 's/GROUP="render"/GROUP="video"/' \
    -e 's/GROUP="sgx", //'               \
    -i rules.d/50-udev-default.rules.in
}

src_build() {
  local _meson_options=(
    --prefix=/usr
    --libdir=/usr/lib
    --buildtype=release
    -Dvcs-tag=false
    -Dsysvinit-path=
    -Dsysvrcnd-path=
    -Dapparmor=disabled
    -Dfirstboot=false
    -Dinstall-tests=false
    -Dldconfig=false
    -Dsysusers=true
    -Drpmmacrosdir=no
    -Dhomed=disabled
    -Duserdb=false
    -Dman=enabled
    -Dmode=release
    -Dpam=enabled
    -Dpamconfdir=/etc/pam.d
    -Ddev-kvm-mode=0660
    -Dnobody-group=nogroup
    -Dsysupdate=disabled
    -Dbashcompletiondir=no

    -Dacl=enabled
    -Ddns-over-tls=openssl
    -Dgcrypt=enabled
    -Dopenssl=enabled
    -Dcryptolib=openssl
    -Dpcre2=enabled
    -Dpolkit=enabled
    -Dkmod=enabled
    -Dlz4=enabled
    -Dzstd=enabled
    -Dseccomp=enabled
    -Daudit=disabled
    -Dbootloader=disabled
    -Dbpf-framework=disabled
    -Dvmlinux-h=disabled
    -Dlibcryptsetup=disabled
    -Dlibcurl=disabled
    -Dlibfido2=disabled
    -Dgnutls=disabled
    -Dmicrohttpd=disabled
    -Didn=false
    -Dlibidn2=disabled
    -Dimportd=disabled
    -Dlibiptc=disabled
    -Dp11kit=disabled
    -Dpwquality=disabled
    -Dqrencode=disabled
    -Dtpm2=disabled
    -Dukify=disabled
    -Dkernel-install=false

    -Ddefault-kill-user-processes=false
    -Ddefault-locale='C.UTF-8'
    -Ddefault-dnssec=no
    -Ddefault-dns-over-tls=no
    -Dcreate-log-dirs=false
    -Dsbat-distro='lfs'
    -Dsbat-distro-summary='Linux from Scratch'
    -Dsbat-distro-pkgname="${pkgname}"
    -Dsbat-distro-version="${pkgver}"
    -Ddocdir=/usr/share/doc/systemd-${pkgver}
  )

  meson setup build "${_meson_options[@]}"
  
  meson compile -C build
}

src_check() {
  # unset these, from Gentoo
  unset DBUS_SESSION_BUS_ADDRESS XDG_RUNTIME_DIR
  chown -R leaf .
  su leaf -c "PATH=$PATH \
    LANG=C.UTF-8 LC_ALL=C.UTF-8 LC_CTYPE=C.UTF-8 \
    meson test -C build --print-errorlogs"
}

src_install() {
  meson install -C build --no-rebuild --destdir "$pkgdir" --quiet
  for _systemdhook in ${_systemdhooks[@]}; do
    leaf_install_hook ${filedir}/${_systemdhook}
  done
  install -d -v -m755 ${pkgdir}/usr/share/man
}

# from Arch
skip_chrooted() {
  if systemd-detect-virt --chroot; then
    echo >&2 "  Skipped: Running in chroot."
    exit 0
  fi
}
# from Arch
systemd_live() {
  skip_chrooted

  if ! systemd-notify --booted; then
    echo >&2 "  Skipped: Current root is not booted."
    exit 0
  fi
}

src_postinstall() {
  systemd_live

  systemd-sysusers

  # follow Arch
  # enable some services by default, but don't track them
  systemctl enable \
    getty@tty1.service \
    systemd-userdbd.socket
}

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