# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/package/mnemoc/runit/parse-config
# ROCK Linux is Copyright (C) 1998 - 2005 Clifford Wolf
# 
# 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; either version 2 of the License, or
# (at your option) any later version. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---

#
# prepare root links for DJB filesystem hierarchy inside chrooted env.
#
if [ "$ROCKCFG_PKG_DJB_LAYOUT" == "1" ]; then
	for x in package command service; do
		eval "pkg_djb_${x}dir='$x'"
		var_append flistroot ' ' $x
	done
	#pkg_djb_compatdir="usr/local/bin"
elif [ "$ROCKCFG_PKG_DJB_LAYOUT" == "0" ]; then
	pkg_djb_packagedir=""
	pkg_djb_commanddir=""
	pkg_djb_servicedir="var/service"
	#pkg_djb_compatdir=""
fi

# package
if [ "$pkg_djb_packagedir" ]; then
	mkdir -p $xroot/$pkg_djb_packagedir
	chmod 1755 $xroot/$pkg_djb_packagedir
	pkg_djb_packagedir=$root/$pkg_djb_packagedir
fi

# service
if [ "$pkg_djb_servicedir" ]; then
	mkdir -p $xroot/$pkg_djb_servicedir
	chmod 1755 $xroot/$pkg_djb_servicedir
	pkg_djb_servicedir=$root/$pkg_djb_servicedir
fi

# command
if [ "$pkg_djb_commanddir" ]; then
	mkdir -p $xroot/$pkg_djb_commanddir
	pkg_djb_commanddir=$root/$pkg_djb_commanddir
fi

#
# add init scripts
#
if pkginstalled runit; then
	# runit_substitute
	#
	runit_substitute() {
		local logdir

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

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

	runitconfdir=$base/package/mnemoc/runit
	runit_has_script=0
	for x in $( cd $runitconfdir/packages; ls -1 $pkg-*.txt 2> /dev/null ); do
		y=${x#$pkg-}; y=${y%.txt}; y=${y//_/\/}
		if [ "${y#runit-}" != "$y" ]; then
			y=\$root/etc/runit/${y#runit-}
		else
			y=\$sysconfdir/$y
		fi
		echo_status "runit: Setting conf to install $y"
		runit_has_script=1
		case "${y##*/}" in
			run|finish)	runitxmode=1 ;;
			*)		runitxmode=  ;;
		esac
		hook_add postmake 8 "mkdir -p ${y%/*}; \
			${runitxmode:+chmod 755 ${y%/*};} \
			rock_substitute $runitconfdir/packages/$x > $y; \
			runit_substitute $y; \
			${runitxmode:+chmod 755 $y;}"
	done

	# fake ROCK' standard install_init if sysvinit will not be built
	if ! pkgcheck sysvinit X; then
		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=$ROCKCFG_PKG_SYSVINIT_INITSTYLE \
				   -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
	fi
	
	unset runitconfdir runitxmode runit_has_script

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

