#!/bin/bash 
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/initng/ngcontrol/ng-update,v 1.5 2005/03/15 21:57:39 cvs Exp $

export	RED='\E[31;01m'
export	GREEN='\E[32;01m'
export	YELLOW='\E[33;01m'
export	NORMAL="\033[0m"

if [ "${EUID}" -ne 0 ]
then
	echo -e "\n"$RED"Error:"$NORMAL" must be root""\n"
	exit 1
fi

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

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

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

       ng-update del daemon/sysklogd
       Removes sysklogd from all runlevels.

       ng-update del net/eth2 default wumpus
       Removes net/eth2 from runlevel default and wumpus

FOO
	exit 1
}

add() {
	local x=
	local y=
	local myscript=
	local mydirs=
	
	if [ $# -lt 3 ]
	then
		echo -e "\n"$RED"Error:"$NORMAL" at least two arguments expected after \"$1\".""\n"
		exit 1
	fi
	shift
	myscript="$1"

	mydirs=$( ls -lA /etc/initng | grep '^d' | awk '{ print $NF }' )
	for y in ${mydirs}
	do
		if [ -e "/etc/initng/${y}/${myscript}.i" ]
		then
			myscript=${y}/${myscript}
		fi
	done

	if [ ! -e "/etc/initng/${myscript}.i" ]
	then
		echo -e "\n"$RED"Error:"$NORMAL" /etc/initng/${myscript}.i not found; aborting""\n"
		exit 1
	fi
	shift
	for x in $*
	do
		if [ ! -e "/etc/initng/${x}.runlevel" ]
		then
			echo -e "\n"$YELLOW"Warning:"$NORMAL" runlevel ${x} not found; skipping""\n"
			continue
		fi
		
		grep /etc/initng/${x}.runlevel -x -e ${myscript}
		if [ "$?" -ne 1 ]
		then
			echo -e "\n"$YELLOW"Warning:"$NORMAL" ${myscript} already installed in runlevel ${x}; skipping""\n"
			continue
		fi
		
		echo ${myscript} >> /etc/initng/${x}.runlevel
		if [ "$?" -ne 0 ]
		then
			echo -e "\n"$RED"Error:"$NORMAL" failed to add ${myscript} to ${x}""\n"
			exit 1
		fi
		regen=1
		echo -e "\n"$GREEN"Success:"$NORMAL" ${myscript} added to runlevel ${x}""\n"
		/sbin/ngc -r ${myscript} -r ${x} &> /dev/null
	done
}

del() {
	local x=
	local y=
	local mylevels=
	local myscript=
	local remlevels=
	local mydirs=
	
	if [ $# -lt 2 ]
	then
		echo -e "\n"$RED"Error:"$NORMAL" at least one argument expected after \"$1\"""\n"
		exit 1
	fi
	shift
	myscript=$1
	
	mydirs=$( ls -lA /etc/initng | grep '^d' | awk '{ print $NF }' )
	for y in ${mydirs}
	do
                if [ -e "/etc/initng/${y}/${myscript}.i" ]
                then
                        myscript=${y}/${myscript}
                fi
        done

	shift
	if [ $# -eq 0 ]
	then
		mylevels=$( cd /etc/initng; ls *.runlevel )
		for x in ${mylevels}
		do
		    if [ -e "/etc/initng/${x}" ]
		    then
			grep /etc/initng/${x} -x -e ${myscript} &> /dev/null
			if [ ! $? -ne 0 ]
			then
			    grep /etc/initng/${x} -x -v -e ${myscript} > /etc/initng/${x}.new
			    mv /etc/initng/${x}.new /etc/initng/${x}
			    regen=1
			    remlevels="${remlevels} ${x}"
			fi
		    fi
		done
	else
		mylevels="$*"
		for x in ${mylevels}
		do
		    if [ -e "/etc/initng/${x}.runlevel" ]
		    then
			grep /etc/initng/${x}.runlevel -x -e ${myscript} &> /dev/null
			if [ ! $? -ne 0 ]
			then
			    grep /etc/initng/${x}.runlevel -x -v -e ${myscript} > /etc/initng/${x}.new
			    mv /etc/initng/${x}.new /etc/initng/${x}.runlevel
			    regen=1
			    /sbin/ngc -r ${x} &> /dev/null
			    remlevels="${remlevels} ${x}"
			fi
		    fi
		done
	fi


	if [ "${remlevels}" = "" ]
	then
		echo -e "\n"$YELLOW"Warning:"$NORMAL" ${myscript} not found in any of the specified runlevels""\n"
	else
		echo -e "\n"$GREEN"Success:"$NORMAL" ${myscript} removed from the following runlevels:${remlevels}""\n"
		/sbin/ngc -r ${myscript} &> /dev/null
	fi
}

show() {
        local x=
        local y=
	local z=
        local mylevels=
        local myscripts=
	local mydirs=

        shift
        if [ $# -eq 0 ]
        then
                mylevels="`( cd /etc/initng; ls *.runlevel | sed 's/.runlevel//g' )`"
        else
                mylevels="$*"
        fi
        mydirs=$( ls -lA /etc/initng | grep '^d' | awk '{ print $NF }' )

	for z in ${mydirs}
	do
		myscripts="${myscripts} $( ls /etc/initng/${z}/*.i | cut -d/ -f5 | cut -d. -f1 )"
	done
	
	myscripts=$( echo ${myscripts} | tsort | sort -u )
	
        for x in ${myscripts}
        do
	printf "%20s | " ${x:0:19}
		for y in ${mylevels}
		do
			if [ -n "$( grep /etc/initng/${y}.runlevel -e ${x} )" ]	
			then
				echo -n "${y} "
			else
				printf "%${#y}s " " "
			fi
		done
	echo ""
        done
}

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

regen=0

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

if [ "${regen}" -eq 1 ]
then
	echo -e "\n"$GREEN"Success:"$NORMAL" ng-update complete.""\n"
fi
