#!/bin/sh
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../runit/parse-config
# Copyright (C) 2008 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
# Copyright (C) 1998 - 2003 Clifford Wolf
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

#
# add init scripts
#
if [ "$SDECFG_INIT" == "runit" ]; then
	# runit_substitute
	#
	runit_substitute() {
		local logdir=

		if [ "${localstatedir%$pkg}" != "$localstatedir" ]; then
			logdir=$localstatedir/log
		else
			logdir=$localstatedir/log/$pkg
		fi

		sed -e "s,D_logdir,$logdir,g" $1
	}

	# runit_tail_install $pkg-$service.tail
	#
	runit_tail_install() {
		local tailfile=
		local service=
		local servicedir=

		for tailfile; do
			service=$(echo $tailfile | sed -e "s,^$pkg-,," -e "s,\.tail\$,," -e "s,_,/,g")

			if [ "${service#runit-}" != "$service" ]; then
				servicedir="$root/etc/runit/${service#runit-}"
			else
				servicedir="$root$sysconfdir/$service"
			fi

			echo_status "runit: $tailfile -> ${servicedir#$root}"
			tail_install runit "$(pkgdesc confdir runit)/packages/$tailfile" "$servicedir"
		done
	}

	runitconfdir=$(pkgdesc confdir runit)
	runit_has_script=0

	runit_files="$(cd "$runitconfdir/packages" && ls -1 $pkg-*.tail 2> /dev/null | tr '\n' ' ')"
	if [ -n "$runit_files" ]; then
		hook_add postdoc 5 "runit_tail_install $runit_files"
		runit_has_script=1
	fi

	runit_files="$(cd "$runitconfdir/packages" && ls -1 $pkg-*.txt 2> /dev/null | tr '\n' ' ')"
	if [ -n "$runit_files" ]; then
		hook_add postmake 5 "runit_raw_install $runit_files"
		runit_has_script=1
	fi

	runit_raw_install() {
		local x= y=

		for x; do
			y=${x#$pkg-}; y=${y%.txt}; y=${y//_/\/}

			if [ "${y#runit-}" != "$y" ]; then
				y="$root/etc/runit/${y#runit-}"
			else
				y="$root$sysconfdir/$y"
			fi

			echo_status "runit: $x -> ${y#$root}"

			mkdir -p "${y%/*}"
			cat "$(pkgdesc confdir runit)/packages/$x" | rock_substitute |
				runit_substitute > "$y"

			case "${y##*/}" in
			run|finish)	chmod 755 "$y" ;;
			esac
		done
	}

	if [ "$SDECFG_PKG_DJB_LAYOUT" == "1" ]; then
		for x in package command service; do
			var_append flistroot ' ' $x
		done
	fi

	runit_layout() {
		if [ -z "$pkg_djb_servicedir" ]; then
			if [ "$SDECFG_PKG_DJB_LAYOUT" == "1" ]; then
				for x in package command service; do
					eval "pkg_djb_${x}dir='$root/$x'"
				done
			else
				pkg_djb_packagedir=
				pkg_djb_commanddir="$root$bindir"
				pkg_djb_servicedir="$root/var/service"
			fi
		fi

		# create them if it doesn't exist
		for x in package command service; do
		        y=$( eval echo \$pkg_djb_${x}dir )
		        if [ -n "$y" ]; then
		                mkdir -p "$y"
				# TODO: research where 1755 comes from
		                [ "$x" = command ] || chmod 1755 "$y"
		        fi
		done
	}

	hook_add preconf 5 'runit_layout'

		if [ $runit_has_script -eq 1 ]; then
			install_init() {
				# i have already installed the script
				true
				}
		else
			install_init() {
				name=$1 ; file=$2 ; initdir="$root/etc/rc.d"

				echo -n "Install SysV Init script '$name' (xx/xx): "
				m4 -Dinitstyle=$SDECFG_INIT_STYLE \
				   -D"D_prefix=/$prefix"  -D"D_sysconfdir=$sysconfdir" \
				   -D"D_docdir=$docdir"   -D"D_localstatedir=$localstatedir" \
				   -D"D_datadir=$datadir" -D"D_infodir=$infodir" \
				   -D"D_bindir=$bindir"   -D"D_sbindir=$sbindir" \
				   -D"D_libdir=$libdir"   -D"D_mandir=$mandir" \
				   $base/package/base/sysvinit/init_macros.m4 \
				   $file > $initdir/init.d/$name
				chmod +x $initdir/init.d/$name
				echo " done."
				}
		fi

		# minimal services
		case "$pkg" in
		cron)
			hook_add postmake 9 'ln -svnf /etc/runit/cron $pkg_djb_servicedir/cron'
			;;
		sysklogd)
			hook_add postmake 9 'ln -svnf /etc/runit/syslogd $pkg_djb_servicedir/syslogd'
			hook_add postmake 9 'ln -svnf /etc/runit/klogd $pkg_djb_servicedir/klogd'
			;;
		esac

	unset runitconfdir runitxmode runit_has_script
fi