#!/bin/sh

INIT="/etc/rc.d/init.d"
COL=70
SET_COL="echo -en \\033[${COL}G"
HELP(){
cat << EOF

Usage: 
service [ -h ] 
        [ -a ]
	[ -s ] service_name runlevel order_num
	[service_name ] [start | stop | restart | status ] 

where service_name is a valid file in /etc/rc.d/init.d/
-h		display help information
-a		display all services status
-s		create K???? and S??? symlinks to a service in /etc/rc.d/rc?.d/
-r              remove a service 
service_name 	one of /etc/rc.d/init.d/ files with exec mode (755)
runlevel	any runlevel from 2 to 5
order_num	the order which service should be started or killed (eg S01log )
		if omited get next avaliable
EOF
}


check() {
case "$?" in
0)
TMP2=`echoc "Active" green`
;;
1)
TMP2=`echoc "Stopped" red`
;;
esac
}


case "$1" in

-a)

	# Display all status
	for N in `ls $INIT` ;   do
		if [ ! -d $INIT/$N ]; then
			TMP1=`$INIT/$N status`
			check
			echo -ne "$TMP1"
			$SET_COL
			echo "$TMP2"  
		fi
	done
;;

-h)
	# Display help
	HELP
;;

-s)
	# Colocar symlinks no runlevels
	# service -s service_name runlevel order_num
	#ls /etc/rc.d/rc${3}.d/ | cut -f1 -d' ' 
	#echo $NEXT
	rm -f /etc/rc.d/rc${3}.d/*${2}*
	ln -s ../init.d/${2} /etc/rc.d/rc${3}.d/S${4}${2} 
	ln -s ../init.d/${2} /etc/rc.d/rc${3}.d/K${4}${2} 

;;
-r)
	rm -f /etc/rc.d/rc${3}.d/S*${2}
	rm -f /etc/rc.d/rc${3}.d/K*${2}
	;;	

"")
	HELP
;;

*)
	$INIT/$1 $2
;;

esac

