#!/usr/bin/env bash
name='openrc'
release='6'
version='0.56'
url='https://github.com/OpenRC/openrc'
description='The OpenRC init system'
email='aliriza.keskin@pardus.org.tr'
maintainer='aliriza'
license=('GPLv3')
source=("https://github.com/OpenRC/openrc/archive/refs/tags/$version.zip"
    "openrc.sysconf"
    "reboot"
    "poweroff"
    "extras/backlight-restore.initd"
    "extras/disable-secondary-gpu.initd"
    "extras/disable-secondary-gpu.sh")
depends=()
makedepends=()
sha256sums=('f7eb8631f4cb64d063271667cd25e26fddfcad47ad39f0a10e9c6d6731bfb4a9'
    'SKIP'
    'SKIP'
    'SKIP'
    'SKIP'
    'SKIP'
    'SKIP')
group=(sys.apps)
uses=()
uses_extra=(pam)
pam_depends=(pam)
arch=('x86_64')

cd $name-$version

prepare(){
    # hidepid=2 by default
    sed -i "s/ proc \/proc/,hidepid=2,gid=31&/g" sh/init.sh.Linux.in
}

setup(){
    meson setup build \
        --sysconfdir=/etc \
        --prefix=/ \
        -Ddefault_library=both \
        -Dzsh-completions=true \
        -Dbash-completions=true \
        -Dpam=$(use_opt pam true false) \
        -Dpkgconfig=true
}

build(){
    ninja -C build
}

package(){
    ninja -C build install
    # disable all services
    rm -f ${DESTDIR}/etc/runlevels/*/*
    rm ${DESTDIR}/etc/init.d/functions.sh
    ln -s ../../libexec/rc/sh/functions.sh ${DESTDIR}/etc/init.d/functions.sh
    # install sysconf script
    mkdir -p ${DESTDIR}/etc/sysconf.d/
    install ../openrc.sysconf ${DESTDIR}/etc/sysconf.d/openrc
    mkdir -p ${DESTDIR}/etc/group.d
    mkdir -p ${DESTDIR}/etc/passwd.d
    # GID: uucp 14
    # GID: proc 31
    echo "uucp:x:14:" >> ${DESTDIR}/etc/group.d/openrc
    echo "proc:x:31:" >> ${DESTDIR}/etc/group.d/openrc
    echo "uucp:x:14:14:uucp:/var/spool/uucp:/bin/false" > ${DESTDIR}/etc/passwd.d/uucp
    # move /share to /usr/share
    mkdir -p ${DESTDIR}/usr ${DESTDIR}/sbin
    mv ${DESTDIR}/{,usr}/share
    # reboot and poweroff script
    install ../reboot ${DESTDIR}/sbin/reboot
    install ../poweroff ${DESTDIR}/sbin/poweroff
    ln -s openrc-shutdown ${DESTDIR}/sbin/shutdown
    # install extras
    mkdir -p ${DESTDIR}/usr/libexec
    install ../disable-secondary-gpu.sh ${DESTDIR}/usr/libexec/disable-secondary-gpu
    install ../disable-secondary-gpu.initd ${DESTDIR}/etc/init.d/disable-secondary-gpu
    install ../backlight-restore.initd ${DESTDIR}/etc/init.d/backlight-restore
    # systemd /proc/1/comm hack
    # move openrc-run as systemd path and symlink
    mkdir -p $DESTDIR/lib/systemd/
    mv $DESTDIR/sbin/openrc-init $DESTDIR/lib/systemd/systemd
    ln -s ../lib/systemd/systemd $DESTDIR/sbin/openrc-init
}

