#!/usr/bin/bash
# JOBoRun		: Jwm OpenBox Obarun RUNit
# Maintainer		: Joe Bo Run <joborun@disroot.org>
# PkgSource		: url="https://gittea.disroot.org/joborun/jobcore/$pkgname"
# Maintainer	: Chris Cromer <chris@cromer.cl>
# Maintainer	: Muhammad Herdiansyah <koni@artixlinux.org>
#-----------------------------------------| DESCRIPTION |---------------------------------------

_url=https://gitea.artixlinux.org/artix/runit-artix
_pkgname=runit-artix
_pkgver=20200825

# Many thanks to the artix team members Konimex & c-cromer for refining
# runit's adoption to Arch.
# After the changes that took place on 9/2020 we decided to stay
# with the old Gentoo opentmpfiles and opensysusers and not adopt the 
# large systemd counterparts.
# Since runit's development has stopped years ago for reaching its goals
# 100%  we will leave it as is ourselves.  So both runit & runit-rc are 
# the last releases before that change.
# set default service path to /run/runit/service
_servicedir=/run/runit/service

pkgname=runit
pkgver=2.1.2
pkgrel=023
pkgdesc='a cross-platform Unix init scheme with service supervision'
arch=('x86_64')
groups=(base)
url='http://smarden.org/runit/'
depends=('runit-rc' 'joborun66')
makedepends=('git')
backup=('etc/rc.local' 'etc/rc.shutdown')
install=runit.install
provides=('svc-manager')
conflicts=('svc-manager')
source=("$url/${pkgname}-${pkgver}.tar.gz"
        "${_pkgname}-${_pkgver}.tar.gz::${_url}/archive/${_pkgver}.tar.gz"
        "halt.c"
        "shutdown"
        "clearmem.patch"
        "svlogd.patch"
        "utmpset-time_t.patch"
        "runit-hook"
        "runit-install.hook"
        "runit-remove.hook"
        "30-binfmt.hook"
        "30-sysctl.hook")

prepare() {
	# set default service path to /run/runit/service
	cd "${srcdir}/admin/${pkgname}-${pkgver}"
	sed -i "s|/service/|${_servicedir}/|" src/sv.c

	# apply patches
	patch -p1 -i "${srcdir}/clearmem.patch"
	patch -p1 -i "${srcdir}/svlogd.patch"
	patch -p1 -i "${srcdir}/utmpset-time_t.patch"
}

build() {
	cc ${CFLAGS} halt.c -o halt ${LDFLAGS}

	cd ${_pkgname}
	make SERVICEDIR="${_servicedir}"

	cd "${srcdir}/admin/${pkgname}-${pkgver}"

	CFLAGS="${CFLAGS} -static"
	LDFLAGS="${LDFLAGS} -static"

	package/compile

}

check() {
	cd "${srcdir}/admin/${pkgname}-${pkgver}"
	package/check
}

package() {
	# commands
	install -dm755 "${pkgdir}/usr/bin/run"
	install -Dm755 ${srcdir}/admin/${pkgname}-${pkgver}/command/* "${pkgdir}/usr/bin"
	install -Dm755 ${srcdir}/halt ${pkgdir}/usr/bin/run/halt
	ln -s halt ${pkgdir}/usr/bin/run/poweroff
	ln -s halt ${pkgdir}/usr/bin/run/reboot
	install -Dm755 ${srcdir}/shutdown ${pkgdir}/usr/bin/run/shutdown

	cd ${_pkgname}
	make DESTDIR="${pkgdir}" PREFIX="/usr" SERVICEDIR="${_servicedir}" install-runit

	# man pages
	install -dm755 "${pkgdir}/usr/share/man/man8"
	install -Dm644 ${srcdir}/admin/${pkgname}-${pkgver}/man/* "${pkgdir}/usr/share/man/man8"

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

	# license
	install -dm755 ${pkgdir}/usr/share/licenses/${pkgname}
	install -Dm644 "${srcdir}/admin/${pkgname}-${pkgver}/package/COPYING" ${pkgdir}/usr/share/licenses/${pkgname}/COPYING
	sed -n '/To the extent/,/0/p' README.md > "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

#---- license gpg-key sha256sums ----

license=('BSD')

sha256sums=(6fd0160cb0cf1207de4e66754b6d39750cff14bb0aa66ab49490992c0c47ba18 # runit-2.1.2.tar.gz
	60435808bdaf2478776bc31e7509a30e037f67990db3f92602064696401d49df # runit-artix-20200825.tar.gz
	a9e4b3e09fb72fd44e62f052fd2302d6fcde64a112d53037febd221564e62b1a # halt.c
	7f11c5cab16f10178612b6a81d37184c25c4426b9c2d5d3f8ef7ed7ab2b52c56 # shutdown
	19b5e99135f153d07052f2dff707430af9faeabc717a5740ea58a8a783f609de # clearmem.patch
	9cf269264c0713975be1b3bc9dd3b5b3bb1e2078ac596527370a902d8d025a5a # svlogd.patch
	527f68f501cc9eb4bbab33f8ad374822624342c7d74b5c795efe4aac6b3f170a # utmpset-time_t.patch
	4fd0751bce6cefff13ea24f3dd5ea8c91db7090170668736d9ff132aad07c6b8 # runit-hook
	87c2eef83dceb42c5f10f31cbdba07e4751a2165b18ad00dd2f3a5819456d271 # runit-install.hook
	dc583a40ce68d33e2c1b1039527427f941568a0189014f564afcfc02b01232c8 # runit-remove.hook
	20d0c5297f20f5f48d9bad24203603f7768c2dc2085b51eafac45dc62aa7ac4c # 30-binfmt.hook
	7b1c4928483df7715a3a897f82baccf4552a3f9b61fba87139aad80addf53202) # 30-sysctl.hook
