# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Maintainer: David P. <megver83@parabola.nu>
# Contributor: Luke Shumaker <lukeshu@parabola.nu>

# Maintainer (Artix): artoo <artoo@artixlinux.org>
# Maintainer (Artix): Chris Cromer <cromer@artixlinux.org>
# Contributor (Artix): williamh <williamh@gentoo.org>

pkgbase=openrc
pkgname=('openrc' 'openrc-bash-completions' 'openrc-zsh-completions'
         'openrc-init' 'openrc-sysvinit' 'openrc-net')
pkgver=0.42.1
pkgrel=7
pkgdesc="Gentoo's universal init system"
arch=('x86_64' 'i686' 'armv7h')
url="https://github.com/OpenRC/openrc"
license=('BSD2')
source=("${pkgbase}-${pkgver}.tar.gz::${url}/archive/${pkgver}.tar.gz"
        "revert.patch::${url}/commit/1801561c2d36c330df7fd02c7508f503a61ff5ba.patch"
        "${pkgbase}.logrotate"
        'openrc-hook'
        '30-openrc-upgrade.hook'
        '30-sysctl.hook'
        'sysctl.conf'
        '30-binfmt.hook'
        'kmod-static-nodes.initd'
        gcc-10-fix.patch::${url}/commit/375ef42393f3dc6edbaa2cb70c79b2366072db38.patch
        supervise-daemon-fix.patch::${url}/commit/6deda13754f1b60245945e953cce8d97e40e86fc.patch
        openrc-shutdown-fix.patch::${url}/commit/fd852865e06a74ecf8b77ff534fa8053e020160f.patch
        posix-compliant-grep.patch::${url}/commit/a7e7fd2b37a7666f26c2d4de9386b2d04f583b41.patch
        binfmt-fix.patch::${url}/commit/eb610859519292c6164c4ba601d22e642c306beb.patch
        cgroup2-verify.patch::${url}/commit/87cfad3d6cd619c6090e8209092448ae68279b0c.patch)
sha256sums=('91a01ca6d930a0688fb91338209985de57cac7aa2d37feddacb78fd3d95308e2'
            '5211012d0e1bdfc1a32113cee5a2eec3a2be4b3549f88216cdecbadd6ca46d81'
            '0b44210db9770588bd491cd6c0ac9412d99124c6be4c9d3f7d31ec8746072f5c'
            '7de1e65f48ba31fc015cfb77be167b41aabdf0f20cb6617ecf3f4c3ca434c9e8'
            'a3fda2186e023ae60c0e0d3806ffeb95effe675acea03cdfd0bf30e8f1c188d4'
            '28ec075d30096bd54499e994f76f8526460474f1e4c33607045c1a71b0684263'
            '860d5b9ae60e13633bd94023c222fa1ef918a02db3ce2e3255cdd6bb9eef047e'
            '28aa413fd78bca98bb23294eeb3f49149825a2c2f97a7ee10039385f5e8e50e9'
            '3ecbe7761b92a123a5a3d395ec3d9e090b0dfba7910a5987b4a2298c20a7e571'
            'addb1bf115b811fba3d120f5c68535ec07f32a55e570b661f1e5575f74327bac'
            '654dada5ffe56d6ae10d0ea4f9cb62c8c6c39a5398cd02561419a8bbf3f686b7'
            '5576a8b6c9fbe79ede027ed053f922da60d1cbe818803677f5aa5532f98ffac4'
            '46f6e809aabb629212a739af7f5177930e9c4a7f2adfdfc59911d778d3716ec9'
            '7e5d7dc888327ef99637fa7207ac9470d72cc37198ddd7ba2cdf72d10a1c7031'
            '41c6598da76bcdcdc8d9349f559bc6b587a6f6dec55bf7c79669af2b28af43e1')

_args=(
    BRANDING='Parabola GNU/Linux-libre'
    SYSCONFDIR=/etc
    PREFIX=/usr
    SBINDIR=/usr/bin
    LIBEXECDIR=/usr/lib/rc
    MKSELINUX=no
    MKPAM=pam
    MKTERMCAP=ncurses
    MKSYSVINIT=yes
    MKBASHCOMP=yes
    MKZSHCOMP=yes
    MKPKGCONFIG=yes
)

prepare(){
    cd "${srcdir}/${pkgbase}-${pkgver}"
    sed -i 's:0444:0644:' mk/sys.mk
    patch -Np 1 -i ../gcc-10-fix.patch
    patch -Np 1 -i ../supervise-daemon-fix.patch
    patch -Np 1 -i ../openrc-shutdown-fix.patch
    patch -Np 1 -i ../posix-compliant-grep.patch
    patch -Np 1 -i ../binfmt-fix.patch
    patch -Np 1 -i ../cgroup2-verify.patch

    # make bootmisc work with sysvinit too
    patch -RNp1 -i ../revert.patch
}

check(){
    cd "${srcdir}/${pkgbase}-${pkgver}"
    make -k check
}

build(){
    cd "${pkgbase}-${pkgver}"
    make "${_args[@]}"
}

package_openrc() {
    depends=('psmisc' 'pam' 'openrc-pid1')
    optdepends=('openrc-net: OpenRC ("newnet") network management scripts'
                'netifrc: Gentoo ("oldnet") network management scripts')
    backup=('etc/rc.conf'
            'etc/conf.d/agetty'
            'etc/conf.d/bootmisc'
            'etc/conf.d/consolefont'
            'etc/conf.d/devfs'
            'etc/conf.d/dmesg'
            'etc/conf.d/fsck'
            'etc/conf.d/hostname'
            'etc/conf.d/hwclock'
            'etc/conf.d/keymaps'
            'etc/conf.d/killprocs'
            'etc/conf.d/localmount'
            'etc/conf.d/modules'
            'etc/conf.d/mtab'
            'etc/conf.d/net-online'
            'etc/conf.d/netmount'
            'etc/conf.d/swap'
            'etc/conf.d/urandom')
    provides=('kmod-openrc')
    conflicts=('kmod-openrc' 'systemd-tools')
    replaces=('kmod-openrc')

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

    make DESTDIR="${pkgdir}" "${_args[@]}" install

    install -Dm644 "${srcdir}/${pkgbase}".logrotate "${pkgdir}"/etc/logrotate.d/"${pkgbase}"

    sed -e 's/#unicode="NO"/unicode="YES"/' \
        -e 's/#rc_logger="NO"/rc_logger="YES"/' \
        -e 's/#rc_controller_cgroups="YES"/rc_controller_cgroups="YES"/' \
        -e 's|#rc_shell=/bin/sh|rc_shell=/usr/bin/sulogin|' \
        -i "${pkgdir}/etc/rc.conf"

    install -d "${pkgdir}"/usr/lib/{openrc/cache,binfmt.d,sysctl.d}

    # getty
    sed -e 's|#baud=""|baud="38400"|' \
        -e 's|#term_type="linux"|term_type="linux"|' \
        -e 's|#agetty_options=""|agetty_options=""|' \
        -i ${pkgdir}/etc/conf.d/agetty

    # sysctl defaults
    # upstream (systemd) default configuration can be found on github:
    # https://raw.githubusercontent.com/systemd/systemd/master/sysctl.d/50-default.conf
    install -m755 "${srcdir}"/sysctl.conf "${pkgdir}"/usr/lib/sysctl.d/50-default.conf

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

    # license
    install -Dm644 LICENSE "${pkgdir}"/usr/share/licenses/"${pkgbase}"/LICENSE

    # split bash completions
    install -d "${srcdir}"/_bash-completion/usr/share/bash-completion/completions
    mv -v "${pkgdir}"/usr/share/bash-completion/completions/* "${srcdir}"/_bash-completion/usr/share/bash-completion/completions

    # split zsh completions
    install -d "${srcdir}"/_zsh/usr/share/zsh/site-functions
    mv -v "${pkgdir}"/usr/share/zsh/site-functions/* "${srcdir}"/_zsh/usr/share/zsh/site-functions

    # split init
    for file in \
      etc/init.d/agetty.tty{1..6} \
      etc/runlevels/default/agetty.tty{1..6} \
      {usr,usr/lib/rc}/bin/shutdown \
      {usr,usr/lib/rc}/bin/reboot \
      {usr,usr/lib/rc}/bin/halt \
      {usr,usr/lib/rc}/bin/poweroff \
      usr/bin/openrc-{init,shutdown} \
      usr/bin/init \
      usr/share/man/man8/openrc-{init,shutdown}.8
    do
        install -d "${srcdir}"/_init/"${file%/*}"
        mv -v "${pkgdir}"/"${file}" "${srcdir}"/_init/"${file}"
    done

    # split networking scripts
    for file in etc/{init,conf}.d/{network,staticroute}; do
        install -d "${srcdir}"/_net/"${file%/*}"
        mv -v "${pkgdir}"/"${file}" "${srcdir}"/_net/"${file}"
    done

    install -d "${srcdir}"/_init/etc/conf.d
    cp -v "${pkgdir}"/etc/conf.d/agetty "${srcdir}"/_init/etc/conf.d/agetty

    # kmod staticc nodes
    install -m755 "${srcdir}"/kmod-static-nodes.initd "${pkgdir}"/etc/init.d/kmod-static-nodes
    ln -snf /etc/init.d/kmod-static-nodes "${pkgdir}"/etc/runlevels/sysinit/kmod-static-nodes

    # do not enable hwclock by default since our kernels handle the system time
    # https://wiki.gentoo.org/wiki/System_time#OpenRC_2
    rm "${pkgdir}"/etc/runlevels/boot/hwclock
}

package_openrc-bash-completions() {
    depends=('openrc' 'bash')
    pkgdesc="OpenRC completions for Bash"

    mv -v "${srcdir}"/_bash-completion/* "${pkgdir}"
}

package_openrc-zsh-completions() {
    depends=('openrc' 'zsh')
    pkgdesc="OpenRC completions for ZSH"

    mv -v "${srcdir}"/_zsh/* "${pkgdir}"
}

package_openrc-init() {
    pkgdesc="OpenRC alternative to sysvinit"
    depends=('openrc')
    provides=('openrc-pid1' 'init')
    conflicts=('openrc-pid1' 'init')
    backup=(etc/conf.d/agetty.tty{1..6})

    mv -v "${srcdir}"/_init/* "${pkgdir}"

    # license
    install -Dm644 "${pkgbase}-${pkgver}"/LICENSE "${pkgdir}"/usr/share/licenses/"${pkgname}"/LICENSE

    for num in {1..6}; do
        cp -v ${pkgdir}/etc/conf.d/agetty ${pkgdir}/etc/conf.d/agetty.tty$num
    done
    rm ${pkgdir}/etc/conf.d/agetty
}

package_openrc-sysvinit() {
    pkgdesc="OpenRC support for using sysvinit as PID 1"
    depends=('openrc' 'sysvinit')
    provides=('openrc-pid1' 'initscripts')
    conflicts=('openrc-pid1' 'initscripts')
    backup=('etc/inittab')

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

    # license
    install -Dm644 LICENSE "${pkgdir}"/usr/share/licenses/"${pkgname}"/LICENSE

    install -Dm644 support/sysvinit/inittab "${pkgdir}"/etc/inittab
    install -Dm755 support/sysvinit/halt.sh "${pkgdir}"/usr/bin/halt.sh
    sed -i 's|/sbin|/usr/bin|g' "${pkgdir}"/etc/inittab "${pkgdir}"/usr/bin/halt.sh
}

package_openrc-net() {
    pkgdesc="OpenRC new-style networking"
    depends=('openrc')
    backup=('etc/conf.d/network'
            'etc/conf.d/staticroute')

    mv -v "${srcdir}"/_net/* "${pkgdir}"

    # license
    install -Dm644 "${pkgbase}-${pkgver}"/LICENSE "${pkgdir}"/usr/share/licenses/"${pkgname}"/LICENSE
}
