# Maintainer (Manjaro): artoo <artoo@manjaro.org>
# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>

_gentoo_uri="https://raw.githubusercontent.com/gentoo/gentoo/master"

pkgbase=openrc-devel
pkgname=('git-openrc'
        'mysql-openrc'
        'postgresql-openrc'
        'subversion-openrc'
        'php-fpm-openrc')
pkgver=20151014
pkgrel=1
pkgdesc="OpenRC init scripts"
arch=('any')
url="https://github.com/manjaro/packages-openrc"
license=('GPL2')
groups=('openrc-devel')
conflicts=('systemd-sysvcompat')
source=("git-daemon.confd::${_gentoo_uri}/dev-vcs/git/files/git-daemon.confd"
        "git-daemon.initd::${_gentoo_uri}/dev-vcs/git/files/git-daemon-r1.initd"
        "mysql.confd::${_gentoo_uri}/dev-db/mysql-init-scripts/files/conf.d-2.0"
        "mysql.initd::${_gentoo_uri}/dev-db/mysql-init-scripts/files/init.d-2.0"
        "svn.confd::${_gentoo_uri}/dev-vcs/subversion/files/svnserve.confd"
        "svn.initd::${_gentoo_uri}/dev-vcs/subversion/files/svnserve.initd3"
        "postgresql.confd::${_gentoo_uri}/dev-db/postgresql/files/postgresql.confd"
        "postgresql.initd::${_gentoo_uri}/dev-db/postgresql/files/postgresql.init-9.3"
        "php-fpm.initd::${_gentoo_uri}/dev-lang/php/files/php-fpm-r4.init")
sha256sums=('4703ba2372c661fb674a29fea7f64983f8b1b3136d971663509249655bca6e21'
            '069878aba8d7c7f229b6049f679d1f93873bdada1107b77fc9a9b2bd8be94516'
            '6f8720ccbb241b08e5f9abb4be0899f55c857f6308ac82c7f12417ee2c02c1a5'
            '9fbee10105b6be9dd128a11cc9ed67f85ba92fac8f3640628e6cd6356824511b'
            '45f2dc1a718aed885559e71d98112e670c92bd6b4f19c5cf593eced6cd2bbd97'
            '9e0eabfd8f07e0c77c4addf21b963b254c84432dd1983083b7da79216f4df80f'
            '57c1ad0b14e8458024c713dd8cc2390023b95c27ba4cbd637333b1020f11f398'
            'fd3d3cd1913969edf51a98189cdb9b166eef52c9ea984ae467c6036194c0aef1'
            '37e34461babfb5881169f9729fbdde7d4aba533f123e2c480fe25ac3b863d3e7')

pkgver() {
	date +%Y%m%d
}

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

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

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

package_git-openrc() {
	pkgdesc="OpenRC git-daemon init script"
	depends=('git' 'openrc')
	backup=('etc/conf.d/git-daemon')
	install=git.install

	_inst_confd 'git-daemon'
	_inst_initd 'git-daemon'

	sed -e 's|/var/git|/srv/git|' -i "${pkgdir}/etc/conf.d/git-daemon"
}

package_mysql-openrc() {
	pkgdesc="OpenRC mysql init script"
	depends=('mysql' 'openrc')
	optdepends=('bind-openrc: bind initscript')
	backup=('etc/conf.d/mysql')
	install=mysql.install

	_inst_confd 'mysql'
	_inst_initd 'mysql'

	sed -e 's|/sbin/mysqld|/bin/mysqld|g' -i "${pkgdir}/etc/init.d/mysql"
}

package_php-fpm-openrc() {
	pkgdesc="OpenRC php-fpm init script"
	depends=('php-fpm' 'openrc')
	optdepends=('apache-openrc: apache initscript'
			'lighttp-openrc: lighttp initscript'
			'nginx-openrc: nginx initscript')
	install=php-fpm.install

	_inst_initd 'php-fpm'

	sed -e 's|/lib/${PHPSLOT}||g' \
		-e 's|/etc/php/fpm-${PHPSLOT}|/etc/php|' \
		-e 's|/run/php-fpm-${PHPSLOT}|/run/php-fpm|' \
		-e 's|PHPSLOT=${SVCNAME#php-fpm-}||' \
		-e 's|^.*${PHPSLOT}.*||' \
		-i "${pkgdir}/etc/init.d/php-fpm"
}

package_postgresql-openrc() {
	pkgdesc="OpenRC postgresql init script"
	depends=('postgresql' 'openrc')
	backup=('etc/conf.d/postgresql')
	install=postgresql.install

	_inst_confd 'postgresql'
	_inst_initd 'postgresql'

	sed -e 's|/@LIBDIR@/postgresql-@SLOT@||g' \
		-e 's|/etc/conf.d/postgresql-@SLOT@|/etc/conf.d/postgresql|' \
		-i "${pkgdir}/etc/init.d/postgresql"
}

package_subversion-openrc() {
	pkgdesc="OpenRC svnserve init script"
	depends=('subversion' 'openrc')
	backup=('etc/conf.d/svn')
	install=subversion.install

	_inst_confd 'svn'
	_inst_initd 'svn'

	sed -e 's|/var/svn|/srv/svn|g' -i ${pkgdir}/etc/conf.d/svn

	sed -e 's|/var/svn|/srv/svn|g' \
		-e 's|-apache|-http|g' \
		-e 's|/run/svnserve.pid|/run/svnserve/svnserve.pid|g' \
		-e 's/--make-pidfile//' \
		-i "${pkgdir}/etc/init.d/svn"
}