# Maintainer (Artix): artoo <artoo@manjaro.org>
# Contributor (Artix): williamh <williamh@gentoo.org>
# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>

pkgbase=openrc
pkgname=('openrc' 'openrc-init')
pkgver=0.34.8
pkgrel=1.parabola3
arch=('i686' 'x86_64' 'armv7h')
url="https://wiki.gentoo.org/wiki/Project:OpenRC"
license=('BSD2')
options=('!emptydirs')
source=("${pkgbase}-${pkgver}.tar.gz::https://github.com/OpenRC/openrc/archive/${pkgver}.tar.gz"
        "${pkgbase}.logrotate"
        'openrc-install.hook'
        'openrc-remove.hook')
sha512sums=('2309e0d330c35467dd6008f1458ac9680207a4c783d4f4e6008a84a313088cfe796dd134dfec79699b8f9c90002b0ae621bb78a9925b987ae98fbd8ad75b947a'
            '690612fddfb2c4cf8f6b5ba7239b9faf29eb3d9b152ab4dcf62694aa2852780440d08cee56d98a9597607f446b3697c911269562821a8402bb5747cbbae34bd9'
            'c31c94574f1c604930b572ed74eabdfcbedb344c54668ddeb3214d71de660e9fa8ff445b04ff817f16e62bce43376680d0a1b6c65baa8f6dba30e4ffa230c04a'
            '96bd71dd4fbe1417a640ac7526e83e1b2290732d5e558d7942c87db0dc1ccb7614763d7748b219ac5982b0db66cba9c124506ec939a661d73db4d0b2642f431e')

_args=(
    SYSCONFDIR=/etc
    PREFIX=/usr
    SBINDIR=/usr/bin
    LIBEXECDIR=/usr/lib/rc
    MKSELINUX=no
    MKPAM=pam
    MKTERMCAP=ncurses
    MKNET=no
    MKSYSVINIT=yes
)

if [ -f /usr/lib/os-release ]; then
    . /usr/lib/os-release
    _args+=(BRANDING="${NAME}")
else
    _args+=(BRANDING='Unknown GNU/Linux')
fi

prepare(){
    cd "${srcdir}/${pkgbase}-${pkgver}"
    sed -e "s|/sbin|/usr/bin|g" -i support/sysvinit/inittab
    sed -i 's:0444:0644:' mk/sys.mk
}

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

package_openrc() {
    pkgdesc="Gentoo's universal init system"
    install=${pkgbase}.install
    depends=('psmisc' 'pam' 'init')
    optdepends=('netifrc: Network Interface Management Scripts'
                'networkmanager-openrc: NetworkManager')
    backup=('etc/rc.conf'
            'etc/conf.d/consolefont'
            'etc/conf.d/keymaps'
            'etc/conf.d/hostname'
            'etc/conf.d/modules'
            'etc/conf.d/hwclock')
        
    cd "${srcdir}/${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"/' \
        -i "${pkgdir}/etc/rc.conf"

    install -d ${pkgdir}/usr/lib/openrc/cache

    # 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

    # alpm hooks
    for h in openrc-{install,remove}.hook;do
        install -Dm644 ${srcdir}/$h ${pkgdir}/usr/share/libalpm/hooks/$h
    done

    # license
    install -Dm644 ${srcdir}/${pkgbase}-${pkgver}/LICENSE "$pkgdir/usr/share/licenses/${pkgbase}/LICENSE"
    
    # move these to $srcdir    
    for bin in shutdown reboot halt poweroff openrc-init init; do
        mv -v ${pkgdir}/usr/bin/${bin} ${srcdir}/${bin}
    done
    mv -v ${pkgdir}/etc/conf.d/agetty ${srcdir}/agetty.confd
    mv -v ${pkgdir}/etc/init.d/agetty ${srcdir}/agetty.initd
}

package_openrc-init() {
    pkgdesc="OpenRC replacement for /sbin/init"
    depends=('openrc')
    provides=('init')
    groups=('init')
    conflicts=('systemd-sysvcompat' 'sysvinit' 'init')
    replaces=('sysvinit')
    install=${pkgbase}-init.install
    
    cd ${srcdir}
    install -d ${pkgdir}/usr/bin/ \
            -d ${pkgdir}/etc/conf.d/ \
            -d ${pkgdir}/etc/init.d/ \
            -d ${pkgdir}/etc/runlevels/default/
          
    for bin in shutdown reboot halt poweroff openrc-init init; do
        mv -v ${bin} ${pkgdir}/usr/bin/${bin}
    done
    
    mv -v agetty.confd ${pkgdir}/etc/conf.d/agetty
    mv -v agetty.initd ${pkgdir}/etc/init.d/agetty
    for num in 1 2 3 4 5 6;do
        cp -v ${pkgdir}/etc/conf.d/agetty ${pkgdir}/etc/conf.d/agetty.tty$num
        ln -sf /etc/init.d/agetty ${pkgdir}/etc/init.d/agetty.tty$num
        ln -sf /etc/init.d/agetty.tty$num ${pkgdir}/etc/runlevels/default/agetty.tty$num
    done

}