#!/bin/bash
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

source /sbin/functions.sh

usage() {
cat << FOO
usage: rc-update -a|add script runlevel2 [runlevel2 ...]
       rc-update -d|del script [runlevel1 ...]
       rc-update -s|show [runlevel1 ...]

note:  After rc-update executes, the script dependency cache is automatically
       updated.

examples:
       rc-update add net.eth0 default
       Adds the net.eth0 script (in /etc/init.d) to the "default" runlevel.

       rc-update del sysklogd
       Deletes the sysklogd script from all runlevels.  The original script
       is not deleted, just any symlinks to the script in /etc/runlevels/*.

       rc-update del net.eth2 default wumpus
       Delete the net.eth2 script from the default and wumpus runlevels.
       All other runlevels are unaffected.  Again, the net.eth2 script
       residing in /etc/init.d is not deleted, just any symlinks in
       /etc/runlevels/default and /etc/runlevels/wumpus.

       rc-update show
       Show all the available scripts and list at which runlevels they
       will execute.
FOO
	exit 1
}

add() {
	local x=
	local myscript=
	
	if [ $# -lt 3 ]
	then
		eerror "${0}: at least two arguments expected after \"$1\"."
		exit 1
	fi
	shift
	myscript="$1"
	if [ ! -e "/etc/init.d/${myscript}" ]
	then
		eerror "$0: /etc/init.d/${myscript} not found; aborting."
		exit 1
	fi
	shift
	for x in $*
	do
		if [ ! -e "/etc/runlevels/${x}" ]
		then
			ewarn "runlevel ${x} not found; skipping"
			continue
		fi
		if [ -L "/etc/runlevels/${x}/${myscript}" ]
		then
			ewarn "${myscript} already installed in runlevel ${x}; skipping"
			continue
		fi
		if [ ! -x "/etc/init.d/${myscript}" ]
		then
			ewarn "${myscript} not executable; skipping"
			continue
		fi
		ln -snf "/etc/init.d/${myscript}" "/etc/runlevels/${x}/${myscript}"
		if [ "$?" -ne 0 ]
		then
			eerror "$0: failed to add ${myscript} to ${x}."
			exit 1
		fi
		regen=1
		einfo "${myscript} added to runlevel ${x}"
	done
}

del() {
	local x=
	local mylevels=
	local myscript=
	local remlevels=
	
	if [ $# -lt 2 ]
	then
		eerror "$0: at least one argument expected after \"$1\"."
		exit 1
	fi
	shift
	myscript=$1
	shift
	if [ $# -eq 0 ]
	then
		mylevels="`( cd /etc/runlevels; ls )`"
	else
		mylevels="$*"
	fi
	remlevels=""
	for x in ${mylevels}
	do
		if [ -L "/etc/runlevels/${x}/${myscript}" ]
		then
			regen=1
			rm -f "/etc/runlevels/${x}/${myscript}"
			remlevels="${remlevels} ${x}"
		fi
	done
	if [ "${remlevels}" = "" ]
	then
		einfo "${myscript} not found in any of the specified runlevels."
	else
		einfo "${myscript} removed from the following runlevels:${remlevels}"
	fi
}

show() {
	local x=
	local y=
	local mylevels=
	local myscripts=
	
	shift
	if [ $# -eq 0 ]
	then
		mylevels="`( cd /etc/runlevels; ls )`"
	else
		mylevels="$*"
	fi
	myscripts="`( cd /etc/init.d; ls )`"

	for x in ${myscripts}
	do
		if [ "${x%%.sh}" = "${x}" ]
		then
			printf "%20s | " ${x:0:19}
			for y in ${mylevels}
			do
				if [ -L "/etc/runlevels/${y}/${x}" ]
				then
					echo -n "${y} "
				else
					printf "%${#y}s " " "
				fi
			done
			echo ""
		fi
	done
}

check_is_root() {
	if [[ ${EUID} -ne 0 ]] ; then
		eerror "$0: must be root."
		exit 1
	fi
}

if [ $# -lt 1 ]
then
	usage
	exit 1
fi

regen=0

case "$1" in
	add|-a)
		check_is_root
		add "$@"
		;;
	del|delete|-d)
		check_is_root
		del "$@"
		;;
	show|-s)
		show "$@"
		;;
	*)
		usage
		exit 1
		;;
esac

if [ "${regen}" -eq 1 ]
then
	# depscan doesnt actually read runlevels so no point ...
	#/sbin/depscan.sh
	einfo "rc-update complete."
fi


# vim:ts=4
