# Maintainer (Artix): artoo <artoo@cromnix.org>
# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Maintainer: David P <megver83@parabola.nu>

pkgver=20201202 # date +%Y%m%d
_commit=5cf13c1eee7fdb74078d79a2c85d1fe2fdde192c
_url="https://raw.githubusercontent.com/gentoo/gentoo/${_commit}"

pkgbase=openrc-desktop
pkgname=('acpid-openrc'
         'alsa-utils-openrc'
         'avahi-openrc'
         'bluez-openrc'
         'displaymanager-openrc'
         'gpm-openrc'
         'wpa_supplicant-openrc'
         'chrony-openrc'
         'tlp-openrc')
pkgrel=1
arch=('any')
url='https://github.com/gentoo/gentoo'
license=('GPL2')
groups=('openrc-desktop')
source=('acpid.confd'
        'acpid.initd'
        "alsasound.confd::${_url}/media-sound/alsa-utils/files/alsasound.confd-r4"
        "alsasound.initd::${_url}/media-sound/alsa-utils/files/alsasound.initd-r8"
        'avahi-daemon.initd'
        'avahi-dnsconfd.initd'
        'autoipd-openrc.sh'
        'bluetoothd.initd'
        'xdm.confd'
        'xdm.initd'
        'xdm-setup.initd'
        'startDM.sh'
        "gpm.confd::${_url}/sys-libs/gpm/files/gpm.conf.d"
        "gpm.initd::${_url}/sys-libs/gpm/files/gpm.rc6-2"
        "wpa_supplicant.confd::${_url}/net-wireless/wpa_supplicant/files/wpa_supplicant-conf.d"
        "wpa_supplicant.initd::${_url}/net-wireless/wpa_supplicant/files/wpa_supplicant-init.d"
        "${_url}/net-wireless/wpa_supplicant/files/wpa_cli.sh"
        "chronyd.confd::${_url}/net-misc/chrony/files/chronyd.conf-r1"
        "chronyd.initd::${_url}/net-misc/chrony/files/chronyd.init-r2"
        "tlp.initd::https://git.hyperbola.info:50100/packages/community.git/plain/tlp/tlp.initd?id=241231d55f767d87e9cc883feb84b7f42a44434e")
sha256sums=('3755d4eb8bb64a1304e5defedb949305ac550565da36fe4f94d5f31beee821ba'
            '5dcf5721a129b3782c13fc453853c390c34e2ad4ee00706388ce014454b063ee'
            'd1c55400b701a72dcb8bb85e016b5013fa3eb6a2766ffc20dae278d0ee4c1a43'
            '37495f08f2aeb934180c1fb0ef6528f8108a41b614be1fe4f596c783871ce68b'
            '876788303553fe773e64917f76f0208f5e8adf7b91d4af24aa9d6a68a147d646'
            'e128576d72981e402ff106bb481108ab6d5ba941ab1b0f5f53e96a7831fc1d15'
            '064a4cedc00f67e93a6f2ef99489e2614305a673a75a2135010283e99da6d6bf'
            'b80a38737be3a1112bc52d2474e8ece3adb405a92623cfb36c3df5aed99ebf51'
            '24dc07139087bd2261eb2de268ae5228a37a4db4fc695a7d9e647d95c36044df'
            'a65d8c3a0210e4ec336f6b19900122d3a27c097893773aaf5c7de91d96399b36'
            'cb49240e32b5bb22d7764890e765c94276ec66b597caeacd5121618f614c3bec'
            '28dbefc67b332712273047ff0a5776b379217368f9a572665b43c2f486591058'
            '73e7483fdc4b12ab4225a4cb13bbe7da71b07b9e69b17e3a6a4c63cb5e2287c8'
            'b48fd8ef0c4e64ca3f3ada72e8e2148a276f86bf3cbb1a165504058bd15385c1'
            '27833f60a091464612e74e6171cc98c3e3f994e2fde0ccc4535dd0d84ed041fa'
            '98985142371d39ea6294d757e85a3d3fc9fc21718063afbf81869e1d2ca648a7'
            'f3aa34b99a90213c53496f8ea014c487266338791f3526043a7fd97adc651fc6'
            'a55216d5bc267004ebfd16f3371acaf04953b9cfdbc55518b26c80492584f647'
            'fc2ff6922c4b64686bba9c6f482a64b2bebc1156d63e09f49adb0ffcfb240a1c'
            '10428455ffe5812d45d9008f1cacbb5351b71566faf6329b8101ba71e213d36a')

_inst_initd(){
    install -Dm755 ${srcdir}/$1.initd ${pkgdir}/etc/init.d/$1

    local _sed_args=()
    sed -e 's|#!/sbin/openrc-run|#!/usr/bin/openrc-run|g' \
        -e 's|/var/run|/run|g' \
        -e 's|/usr/sbin|/usr/bin|g' \
        -e 's|/sbin|/usr/bin|g' \
        -i ${pkgdir}/etc/init.d/$1
}

_inst_confd(){
    install -Dm755 ${srcdir}/$1.confd ${pkgdir}/etc/conf.d/$1
}

package_acpid-openrc() {
    pkgdesc="OpenRC acpid init script"
    depends=('openrc' 'acpid')
    backup=('etc/conf.d/acpid')

    _inst_confd 'acpid'
    _inst_initd 'acpid'
}

package_alsa-utils-openrc() {
    pkgdesc="OpenRC alsa-utils init script"
    depends=('openrc' 'alsa-utils')
    backup=('etc/conf.d/alsasound')

    _inst_confd 'alsasound'
    _inst_initd 'alsasound'
}

package_avahi-openrc() {
    pkgdesc="OpenRC avahi init script"
    depends=('avahi' 'dbus-openrc')

    _inst_initd 'avahi-daemon'
    _inst_initd 'avahi-dnsconfd'

    install -Dm755 autoipd-openrc.sh ${pkgdir}/usr/lib/netifrc/net/autoipd.sh
}

package_bluez-openrc() {
    pkgdesc="OpenRC bluez init script"
    depends=('bluez' 'openrc')

    _inst_initd 'bluetoothd'
}

package_displaymanager-openrc() {
    pkgdesc="OpenRC display manager init script"
    depends=('xorg-server' 'xorg-xinit' 'openrc')
    backup=('etc/conf.d/xdm')
    install=displaymanager.install

    _inst_confd 'xdm'
    _inst_initd 'xdm'
    _inst_initd 'xdm-setup'

    install -Dm755 "${srcdir}/startDM.sh" "${pkgdir}/etc/X11/startDM.sh"
}

package_gpm-openrc() {
    pkgdesc="OpenRC gpm init script"
    depends=('openrc' 'gpm')
    backup=('etc/conf.d/gpm')

    _inst_confd 'gpm'
    _inst_initd 'gpm'
}

package_wpa_supplicant-openrc() {
    pkgdesc="OpenRC wpa_supplicant init script"
    depends=('openrc' 'wpa_supplicant')
    backup=('etc/conf.d/wpa_supplicant')

    _inst_confd 'wpa_supplicant'
    _inst_initd 'wpa_supplicant'

    install -Dm755 "${srcdir}/wpa_cli.sh" "${pkgdir}/etc/wpa_supplicant/wpa_cli.sh"

    sed -e "s|gentoo-release|parabola-release|" -i "${pkgdir}/etc/wpa_supplicant/wpa_cli.sh"
}

package_chrony-openrc() {
    pkgdesc="OpenRC chrony init script"
    depends=('openrc' 'chrony')
    backup=('etc/conf.d/chronyd')

    _inst_confd 'chronyd'
    sed -e 's|ARGS=".*"|ARGS=""|' \
        -e 's|CFGFILE=".*"|CFGFILE="/etc/chrony.conf"|' \
        -i "${pkgdir}/etc/conf.d/chronyd"

    _inst_initd 'chronyd'
    sed -e 's|/sbin|/usr/bin|' \
        -e 's|/etc/chrony/|/etc/|' \
        -i "${pkgdir}/etc/init.d/chronyd"
}

package_tlp-openrc() {
    pkgdesc="OpenRC tlp init script"
    depends=('openrc' 'tlp')

    _inst_initd 'tlp'
    sed -e 's|/usr/sbin|/usr/bin|' \
        -e 's|/sbin/openrc-run|/usr/bin/openrc-run|' \
        -i "${pkgdir}/etc/init.d/tlp"
}
