#!/bin/sh
# --- T2-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# 
# T2 SDE: package/.../runit/parse-config
# 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.
# --- T2-COPYRIGHT-NOTE-END ---

#
# prepare root links for DJB filesystem hierarchy inside chrooted env.
#
if [ "$SDECFG_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 [ "$SDECFG_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 -e "s,D_logdir,$logdir,g" $1
	}

	# runit_tail_prepare /foo/bar/$pkg-$service.tail
	#
	runit_tail_prepare() {
		local tailfile=$1 pkg=$2
		local service=` echo $tailfile | sed -e "s,^.*/$pkg-,," -e "s,\.tail\$,," -e "s,_,/,g"`
		local servicedir=

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

		echo_status "runit: Setting conf to install ${servicedir#\$root}/ (TAIL)"
		hook_add postdoc 5 "tail_install runit '$tailfile' '$servicedir'"
	}

	runitconfdir=$base/package/*/runit
	runit_has_script=0
	for x in $( ls -1 $runitconfdir/packages/$pkg-*.tail 2> /dev/null ); do
		runit_tail_prepare $x $pkg
		runit_has_script=1
	done
	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=\$root/\$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%/*};} \
			cat $runitconfdir/packages/$x | rock_substitute \
				| runit_substitute > $y; \
			${runitxmode:+chmod 755 $y;}"
	done

	if [ "$SDECFG_INIT" == "runit" ]; 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=$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
	fi
	
	unset runitconfdir runitxmode runit_has_script

	# 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
fi