# Maintainer: David P. <megver83@parabola.nu>
# Contributor: Luke Shumaker <lukeshu@parabola.nu>
# Contributor: artoo <artoo@artixlinux.org>


# NOTE: this was originally based on the artix PKGBUILD (<= v0.2)
#       artix as since dropped this software in favor of 'etmpfiles' from eudev
#       https://gitea.artixlinux.org/artixlinux/packages/commit/e8314e156545bfaab6455f8200a253932dc0049c


pkgname=opentmpfiles
pkgver=0.3.1
pkgrel=1
pkgdesc="standalone utility for handling systemd-style tmpfiles.d settings"
arch=('any')
url="https://github.com/OpenRC/opentmpfiles"
license=('BSD2')
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/OpenRC/${pkgname}/archive/${pkgver}.tar.gz"
        '30-opentmpfiles.hook'
        'tmpfiles-hook'
        'chattr-ignore-nocow.patch')
sha256sums=('47d651ccda53a204463285044a9d34b1bd54d927509dac43994a9d6341be084a'
            '23c73b3cc20ec38e0a5166f254293c911d20ae3efec8e133f424c1a1d0703fb8'
            'adbee03530006e8284ea44c5dd7eb99f6cc6c4e383c09fec36d7a22f7d486fc5'
            'cc551d44fb9026fe00205751a149b564db09c6bf0455a741ddd171d0a85f5b62')


prepare()
{
  cd "${pkgbase}-${pkgver}"

  # https://github.com/OpenRC/opentmpfiles/issues/2
  patch -Np1 < ${srcdir}/chattr-ignore-nocow.patch
}

package()
{
    replaces=('opentmpfiles-systemdcompat')
    conflicts=('opentmpfiles-systemdcompat' 'systemd-tools')

    cd "${pkgbase}-${pkgver}"

    make bindir="/usr/bin" DESTDIR="${pkgdir}" install

    # pacman hooks
    install -Dm755 ${srcdir}/tmpfiles-hook "$pkgdir"/usr/share/libalpm/scripts/tmpfiles-hook
    install -Dm644 -t "$pkgdir"/usr/share/libalpm/hooks ${srcdir}/*.hook

    ln -snf "/usr/bin/tmpfiles" "${pkgdir}/usr/bin/systemd-tmpfiles"

    install -d "${pkgdir}"/etc/{conf,init}.d "${pkgdir}"/etc/runlevels/{boot,sysinit}

    install -m755 openrc/opentmpfiles-dev.confd "${pkgdir}"/etc/conf.d/opentmpfiles-dev
    install -m755 openrc/opentmpfiles-dev.initd "${pkgdir}"/etc/init.d/opentmpfiles-dev

    install -m755 openrc/opentmpfiles-setup.confd "${pkgdir}"/etc/conf.d/opentmpfiles-setup
    install -m755 openrc/opentmpfiles-setup.initd "${pkgdir}"/etc/init.d/opentmpfiles-setup

    ln -snf /etc/init.d/opentmpfiles-dev "${pkgdir}"/etc/runlevels/sysinit/opentmpfiles-dev
    ln -snf /etc/init.d/opentmpfiles-setup "${pkgdir}"/etc/runlevels/boot/opentmpfiles-setup

    install -dm755 "${pkgdir}/usr/share/licenses/${pkgname}"
    install -m644 "LICENSE" "${pkgdir}/usr/share/licenses/${pkgname}/"
}
