# Maintainer: David P. <megver83@parabola.nu>
# Maintainer (Artix): Christian Hesse <mail@eworm.de>
# Maintainer (Artix): Dave Reisner <dreisner@archlinux.org>
# Maintainer (Artix): Tom Gundersen <teg@jklm.no>

# Remove the 'replaces' variables in package functions in the near future

_pkgbase=systemd

pkgbase=xudev
pkgname=('xudev' 'libxudev' 'etmpfiles' 'esysusers'
        'xudev-docs' 'etmpfiles-docs' 'esysusers-docs')
pkgdesc='Userspace device file manager'
_tag='fd9ad7d3bc35a3633b99edac14ff2a4fb10599b7' # git rev-parse v${pkgver}
_tag_name=248.3
pkgver="${_tag_name/-/}"
pkgrel=2
arch=('x86_64')
arch+=('i686' 'armv7h')
url='https://www.github.com/systemd/systemd'
license=('GPL2' 'LGPL2.1')
makedepends=('acl' 'libacl.so' 'docbook-xsl' 'gperf' 'kbd' 'hwids' 'intltool' 'kmod' 'libkmod.so'
            'libcap' 'libcap.so' 'util-linux' 'libblkid.so' 'git' 'meson' 'rsync')
options=('strip')
validpgpkeys=('63CDA1E5D3FC22B998D20DD6327F26951A015CC4'  # Lennart Poettering <lennart@poettering.net>
              '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'
        'initcpio-hook-udev'
        'initcpio-install-udev'
        'udev-hook'
        '20-sysusers.hook'
        '30-udev-hwdb.hook'
        '30-tmpfiles.hook'
        '30-udev-reload.hook')
sha512sums=('SKIP'
            'SKIP'
            '0db4a6ee10140bcda5021d6be573180410166b9c30f93037641df7fa733af146ed42bf67bdd4a00bf81bd741d4d4f707decfdb487d95603f98d492e2b51ee21d'
            '32606b42856b5f3ea7f485143e532671f58986237e14c58ea5ab17383dc39a375cb6c738c8a2db9e4a8c8be88ea44a876d6bbed129cb2f5c9aa3f8228b04d927'
            'a57b8b34d9ece76342726864dad6d923b461f867c122a069bd521c99c4b951a00a532a8cd30426e678e2c160d00b40a7f6fbb4f1a7a15e47ac6bb5df42fbc4f2'
            '8291ae9f1f4ac0cdf007aa84c6240a2b849ad672d47cc0e3f89c1f83819fe7127d9dd6def139099cd470906a5e7e41a0ff6c5d64d106e478a0916023b177d443'
            '9a02d97585b9ad5b8016b605ddb35027df3c3a107c8619e568dc3a3cd787eedfb059f8ab5acd578a270e299b63516383ee8d620083bcb1c2fc32cf54f8d5c77d'
            'cee9240dac5888d1dde916429ac25c022e30b5d7c53ba9e699e281021d2224bfd6e4cc4ac1c71c15f768b720d524cac0dabaae06d026a68759f6fe84b4c62751'
            '54dd41c1d765923f41e9175bdb4f424b4574215b6c514da474c3d4bdf8cf0c1e7c727e6613456db8bc4698aa9ce1a7a29ad127ad25eb8507b8df2d87546fc5f7'
            '5aa475f37d35752a95e73417c38c4d3bc4d15d5f1052e95d75b4c4144b74c79af94da1d1e954be536339f9829a1ceb3a1bc5c6adceb187df7a8e5f5a83e4a850')

_backports=(
)

_reverts=(
)

prepare() {
  cd "$_pkgbase-stable"

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

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

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

build() {
  if ! [[ "$CARCH" == x86_64 ]]; then
    CFLAGS+=" -Wno-error=implicit-function-declaration"
  fi

  local _meson_options=(
    -Dversion-tag="${pkgver}-${pkgrel}-parabola"
    -Drootprefix=/usr
    -Dgcrypt=false
    -Dlibcryptsetup=false
    -Dlibidn=false
    -Dlibidn2=false
    -Dlibiptc=false
    -Dseccomp=false
    -Dlz4=false
    -Dxz=false
    -Dstandalone-binaries=true
    -Dsysusers=true
    -Dtmpfiles=true
    -Dhwdb=true
    -Dlink-udev-shared=false
    -Dgshadow=true

    -Dtests=false
    -Ddefault-mdns=no
    -Dnss-myhostname=false
    -Dhostnamed=false
    -Dlocaled=false
    -Dtimedated=false
    -Dadm-group=true
    -Dwheel-group=true
    -Dman=true
    -Defi=false
    -Dlibfdisk=false
    -Dp11kit=false
    -Dima=false
    -Dhomed=false
    -Dutmp=false
    -Dresolve=false

    -Dtpm=false
    -Denvironment-d=false
    -Dbinfmt=false
    -Drepart=false
    -Dcoredump=false
    -Dpstore=false
    -Dlogind=false
    -Dmachined=false
    -Dportabled=false
    -Duserdb=false
    -Dnetworkd=false
    -Dtimesyncd=false
    -Dremote=false
    -Dnss-mymachines=false
    -Dnss-resolve=false
    -Dnss-systemd=false
    -Dfirstboot=false
    -Drandomseed=false
    -Dbacklight=false
    -Dvconsole=false
    -Dquotacheck=false
    -Dimportd=false
    -Dxdg-autostart=false
    -Drfkill=false
    -Dfdisk=false
    -Daudit=false
    -Dpwquality=false
    -Dmicrohttpd=false
    -Dlibcurl=false
    -Didn=false
    -Dqrencode=false
    -Dgnutls=false
    -Dopenssl=false
    -Dlibfido2=false
    -Delfutils=false
    -Dxkbcommon=false
    -Dzstd=false
    -Dlz4=false
    -Dxz=false
    -Dbzip2=false
    -Dzlib=false
    -Dpcre2=false
    -Dglib=false
    -Ddbus=false
    -Dgnu-efi=false
    -Dkernel-install=false
    -Dfuzz-tests=false
    -Danalyze=false
    -Dselinux=false
    -Dapparmor=false
    -Dsmack=false
    -Dinitrd=false
    -Dpolkit=false
    -Dldconfig=false
    -Dhibernate=false
    -Ddefault-llmnr=no
    -Drpmmacrosdir=no
    -Ddefault-dnssec=no
    -Doomd=false
    -Dsysext=false
    -Dtpm2=false
    -Dnscd=false
    -Dtranslations=false
    -Dfexecve=false
    -Dvalgrind=false
    -Dlog-trace=false
  )

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

  ninja -C build
}

check() {
  meson test -C build
}

_package() {
    install -d "${srcdir}"/_pkg
    DESTDIR=""${srcdir}"/_pkg" meson install -C build
}

package_xudev() {
    pkgdesc='Userspace device file manager'
    depends=('acl' 'libacl.so' 'kbd' 'kmod' 'libkmod.so' 'hwids' 'libxudev'
            'util-linux' 'libblkid.so')
    provides=("udev=$pkgver" 'eudev')
    conflicts=('udev')

   _package

    install -dm0755 "${pkgdir}"/{etc,usr/{lib/pkgconfig,include,bin,share/pkgconfig}}
    mv -v "${srcdir}"/_pkg/etc/udev "${pkgdir}"/etc/udev

    mv -v "${srcdir}"/_pkg/usr/include/libudev.h "${pkgdir}"/usr/include/libudev.h

    mv -v "${srcdir}"/_pkg/usr/lib/pkgconfig/libudev.pc "${pkgdir}"/usr/lib/pkgconfig

    mv -v "${srcdir}"/_pkg/usr/lib/udev "${pkgdir}"/usr/lib/udev
    mv -v "${srcdir}"/_pkg/usr/bin/udevadm "${pkgdir}"/usr/bin/udevadm
    ln -sfv udevadm "${pkgdir}"/usr/bin/udevd
    #mv -v "${srcdir}"/_pkg/usr/bin/systemd-hwdb "${pkgdir}"/usr/bin/udev-hwdb

    mv -v "${srcdir}"/_pkg/usr/share/pkgconfig/udev.pc "${pkgdir}"/usr/share/pkgconfig

    rm -v "${pkgdir}"/usr/lib/udev/rules.d/99-systemd.rules

    # initcpio
    install -D -m0644 initcpio-install-udev "${pkgdir}"/usr/lib/initcpio/install/udev
    install -D -m0644 initcpio-hook-udev "${pkgdir}"/usr/lib/initcpio/hooks/udev

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

package_libxudev() {
    pkgdesc='udev library for enumerating and introspecting local devices'
    depends=('glibc')
    provides=('libudev.so' 'libudev' 'libeudev')
    conflicts=('libudev')

    install -dm0755 "${pkgdir}"/usr/lib
    mv -v "${srcdir}"/_pkg/usr/lib/libudev*.* "${pkgdir}"/usr/lib
}

package_etmpfiles() {
    pkgdesc='the tmpfiles.d binary'
    groups=('base-devel')
    depends=('libcap' 'libcap.so' 'acl' 'libacl.so')
    conflicts=('opentmpfiles')
    replaces=('opentmpfiles')

    install -dm0755 "${pkgdir}"/{etc,usr/{bin,lib}}
    mv -v "${srcdir}"/_pkg/etc/tmpfiles.d "${pkgdir}"/etc/tmpfiles.d
    mv -v "${srcdir}"/_pkg/usr/lib/tmpfiles.d "${pkgdir}"/usr/lib/tmpfiles.d
    mv -v "${srcdir}"/_pkg/usr/bin/systemd-tmpfiles.standalone "${pkgdir}"/usr/bin/tmpfiles

    rm -v "${pkgdir}"/usr/lib/tmpfiles.d/{*systemd*,journal*,legacy}.conf

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

package_esysusers() {
    pkgdesc='the sysusers.d binary'
    groups=('base-devel')
    depends=('glibc')
    conflicts=('opensysusers')
    replaces=('opensysusers')

    install -dm0755 "${pkgdir}"/usr/{lib,bin}
    mv -v "${srcdir}"/_pkg/usr/lib/sysusers.d "${pkgdir}"/usr/lib/sysusers.d
    mv -v "${srcdir}"/_pkg/usr/bin/systemd-sysusers.standalone "${pkgdir}"/usr/bin/sysusers

    rm -v "${pkgdir}"/usr/lib/sysusers.d/systemd*.conf

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

package_xudev-docs() {
    pkgdesc='Userspace device file manager (docs)'

    install -dm0755 "${pkgdir}"/usr/share/{doc/xudev,man/man{3,5,7,8}}

    mv -v "${srcdir}"/_pkg/usr/share/man/man5/udev*.5 "${pkgdir}"/usr/share/man/man5/
    mv -v "${srcdir}"/_pkg/usr/share/man/man7/{udev*,hwdb}.7 "${pkgdir}"/usr/share/man/man7/
    mv -v "${srcdir}"/_pkg/usr/share/man/man8/udev*.8 "${pkgdir}"/usr/share/man/man8/
    mv -v "${srcdir}"/_pkg/usr/share/man/man3/libudev* "${pkgdir}"/usr/share/man/man3/

    cp -v "${srcdir}"/_pkg/usr/share/doc/systemd/{LICENSE.*,NEWS} "${pkgdir}"/usr/share/doc/xudev
}

package_etmpfiles-docs(){
    pkgdesc='the tmpfiles.d binary (docs)'
    conflicts=('opentmpfiles-docs')
    replaces=('opentmpfiles-docs')

    install -dm0755 "${pkgdir}"/usr/share/{doc/etmpfiles,man/man5}
    mv -v "${srcdir}"/_pkg/usr/share/man/man5/tmpfiles.d.5 "${pkgdir}"/usr/share/man/man5/
    cp -v "${srcdir}"/_pkg/usr/share/doc/systemd/{LICENSE.*,NEWS} "${pkgdir}"/usr/share/doc/etmpfiles
}

package_esysusers-docs(){
    pkgdesc='the sysusers.d binary (docs)'
    conflicts=('opensysusers-docs')
    replaces=('opensysusers-docs')

    install -dm0755 "${pkgdir}"/usr/share/{doc/esysusers,man/man5}
    mv -v "${srcdir}"/_pkg/usr/share/man/man5/sysusers.d.5 "${pkgdir}"/usr/share/man/man5/
    cp -v "${srcdir}"/_pkg/usr/share/doc/systemd/{LICENSE.*,NEWS} "${pkgdir}"/usr/share/doc/esysusers
}
