# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../runit/runit.conf
# Copyright (C) 2008 - 2012 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 ---

if [ $prefix_auto = 1 ]; then
	prefix=
	set_confopt
fi

. $base/package/*/runit/djb-config admin $sbindir

hook_add postmake 6 'pkg_runit_quirks'

pkg_runit_quirks() {
	local agettyargs speed

	if ! pkginstalled daemontools; then
		[ "$pkg_djb_commanddir" -a "$pkg_djb_commanddir" != "$root$sbindir" ] &&
			add_flist $pkg_djb_commanddir;
		[ "$pkg_djb_servicedir" ] && add_flist $pkg_djb_servicedir;
		[ "$pkg_djb_packagedir" ] && add_flist $pkg_djb_packagedir;
	fi

	# main scripts and virtual consoles
	# only if it is INIT
if [ "$SDECFG_INIT" == "runit" ]; then
	#if [ ! -f $root/$sysconfdir/1 ]; then
		echo "Creating main scripts and virtual consoles..."

		mkdir -p $root/$sysconfdir
		cp -vf ./doc/debian/[123] $root/$sysconfdir/
		cp -vf ./doc/debian/ctrlaltdel $root/$sysconfdir/
		chmod 744 $root/$sysconfdir/[123]
		chmod 744 $root/$sysconfdir/ctrlaltdel
		mkdir -p $root/$sysconfdir/{1,3}.d

		for i in console 1 2 3 4 5 6; do
			if [ "$i" == "console" ]; then
				echo "Creating getty $i..."
				speed=9600
			else
				echo "Creating getty tty$i..."
				i="vc/$i"
				speed=38400
			fi
			mkdir -p $root/$sysconfdir/getty-${i#vc/}
			if [ "$i" == "console" -o "$i" == "vc/1" ]; then
				agettyargs='-L -i -I '"'"'\012\015\012Maintenance Console:\012'"'"
			else
				agettyargs='-f /etc/issue'
			fi
			cat << EOT > $root/$sysconfdir/getty-${i#vc/}/run
#!/bin/sh
exec /sbin/chpst -P /sbin/agetty $agettyargs $speed $i linux
EOT
			cat << EOT > $root/$sysconfdir/getty-${i#vc/}/finish
#!/bin/sh
exec ${pkg_djb_commanddir#$root}/utmpset -w $i
EOT
			chmod 755 $root/$sysconfdir/getty-${i#vc/}/{run,finish}
		done
	#fi

	# getties to svscan
	echo "Make getties available to svscan..."
	for i in 1 2 3 4 5 6; do
		ln -snfv $sysconfdir/getty-$i $pkg_djb_servicedir/
	done

	# HACK, runit need it's own system initializer
	install_init system $confdir/../sysvinit/system.init

	ln -snfv /etc/rc.d/init.d/system "$root/$sysconfdir/1.d/01system"
	ln -snfv /etc/rc.d/init.d/system "$root/$sysconfdir/3.d/99system"

	# init at /sbin
	if [ ${pkg_djb_commanddir} != $root$sbindir ]; then
		echo "Copying init files to /sbin..."
		cp -vf $root$( readlink ${pkg_djb_commanddir}/runit ) $root$sbindir/
		cp -vf $root$( readlink ${pkg_djb_commanddir}/runit-init ) $root$sbindir/init
	else
		echo "Symlinking sbin/init"
		ln -svnf runit-init $root$sbindir/init
	fi

fi

	ln -sfv forced_reboot $pkg_djb_commanddir/forced_poweroff

	# Emulate daemontools if not present using chpst
	if ! pkginstalled daemontools; then
		echo "Emulate functionality of daemontools with chpst."
		for x in softlimit envdir envuidgid pgrphack setlock setuidgid;
		do
			ln -sfv chpst $pkg_djb_commanddir/$x
		done
	fi
	}

if [ "$SDECFG_PKG_DJB_LAYOUT" == "1" ]; then
	hook_add premake 5 "for x in ./doc/debian/*; do \
	if [ -f \$x ]; then	\
		echo Fixing \$x... ;	\
		sed -i -e 's,/var/service,/service,g' \$x ; \
	fi	; \
	done"

	runit_profile_d() {
		cat <<-EOP > $root/etc/profile.d/runit
	export PATH=/command:\$PATH
	EOP
	}
	hook_add postmake 5 'runit_profile_d'
else
	hook_add premake 5 "for x in ./doc/debian/*; do \
	if [ -f \$x ]; then	\
		echo Fixing \$x... ;	\
		sed -i -e 's,/command:,,g' -e 's,/service,/var/service,g' \$x ;	\
	fi	; \
	done"
	hook_add postinstall 8 'sed -i -e "s,/service,/var/service,g" \
		$root/lib/network/runit.sh'
fi

hook_add premake 5 "sed -i -e 's:\(^IT=.*\):\1 svwaitup svwaitdown:' src/Makefile; \
	echo 'svwaitup' >> package/commands; \
	echo 'svwaitdown' >> package/commands"