#!/usr/bin/env bash
name='openrc'
release='2'
version='0.53'
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=()
md5sums=('4c539c934497e15fc038a18465268f00'
    'SKIP'
    'SKIP'
    'SKIP'
    'SKIP'
    'SKIP'
    'SKIP')
group=(sys.apps)
uses=()
uses_extra=(pam)
pam_depends=(pam)
arch=('x86_64')

cd $name-$version

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(){
    export DESTDIR=${DESTDIR}//
    ninja -C build install
    # disable all services
    rm -f ${DESTDIR}/etc/runlevels/*/*
    rm ${DESTDIR}//etc/init.d/functions.sh
    ln -s ../../lib/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
    echo "uucp:x:14:" > ${DESTDIR}/etc/group.d/uucp
    echo "uucp:x:10: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
    install ../backlight-restore.initd ${DESTDIR}/etc/init.d
}

