#!/bin/sh -e

LANG=POSIX
CHKCONFIG=/sbin/chkconfig
INITDIR=/etc/rc.d/init.d
RUNLEVEL=`/sbin/runlevel | cut -c3`
RUNLEVEL2=
SERVICES=
CUR_OFF=
CUR_ON=

. /usr/share/alterator/build/backend3.sh

_()
{
LANG=${in_language%%;*}.utf8 gettext "alterator-services" "$1"
}

find_scripts()
{
	find -L "$INITDIR" -mindepth 1 -maxdepth 1 -type f -perm +0111 ! -name '*.rpm*' ! -name '*~' -printf '%f\n' | sort
}

find_disabled_services()
{
	local SERVICES=`/sbin/chkconfig --list|cut -f1`
	local SCRIPTS=`find_scripts`
	for i in $SERVICES; do
	    SCRIPTS=${SCRIPTS/$i/}
	done
	for i in $SCRIPTS; do
	    echo $i
	done
}

filter_description()
{
	awk 'BEGIN { is_desc=0; text="" }
/^#[[:space:]]*description:/ {
    is_desc=1
    gsub(/description:[[:space:]]*/,"description \"")
}
/^#/ {
    if (is_desc == 1) {
	gsub(/^#[[:space:]]*/,"")
	if (gsub(/[[:space:]]*\\$/,"") < 1) {
	    is_desc=0
	}
	text=text " " $0;
	if (is_desc == 0) {
	    print text "\""
	    exit
	}
    }
}'

}

filter_levels()
{
	awk '/^#[[:space:]]*chkconfig:/ {
    gsub (/^#[[:space:]]*chkconfig:[[:space:]]+/,"")
    gsub (/[[:space:]]+.*$/,"")
    count=split($0, a, //)
    for (i=0; i<=6; i++) {
	level[i]="#f"
    }
    for (i=1; i<=count; i++) {
	level[a[i]]="#t"
    }
    for (i=0; i<=6; i++) {
	print "level" i, level[i]
    }

}'

}

wrap()
{
	while read i; do echo "(\"${i%% *}\")"; done
}

on_message()
{
        case "$in_action" in
		constraints)
			echo '('
			echo 'status (default "unknown")'
			echo 'level0 (default #f)'
			echo 'level1 (default #f)'
			echo 'level2 (default #f)'
			echo 'level3 (default #f)'
			echo 'level4 (default #f)'
			echo 'level5 (default #f)'
			echo 'level6 (default #f)'
			echo 'selected (default #f)'
			printf 'name (label "%s")' "$(_ "Service")"
			if [ "$in_orig_action" = "write" ]; then
				[ -n "$in_start" ] && service "$in_name" start &> /dev/null ||:
				[ -n "$in_stop" ] && service "$in_name" stop &> /dev/null ||:
			fi
			echo ')'
			;;
                list)
			shift; service="$1"
			[ "$service" != "/" ] || service="*"
                        echo '( '

			[ -n "$in_kind" ] || RUNLEVEL=
			[ -n "$in_kind" ] || in_kind="${in__objects##*/}"
			case "$in_kind" in
				enabled)
					/sbin/chkconfig --list | /bin/fgrep $RUNLEVEL:on | cut -f1 | wrap
					;;
				disabled)
					/sbin/chkconfig --list | /bin/fgrep $RUNLEVEL:off | cut -f1 | wrap
					;;
				status)
					echo "( \"" `/sbin/service $in_name status 2> /dev/null` "\" )"
					;;
				start)
					echo "( \""Start"\" )"
					/sbin/service $in_name start >>start
					;;
				stop)
					echo "( \""Stop"\" )"
					/sbin/service $in_name stop >>stop 
					;;
				new)
					find_disabled_services | wrap
					;;
				*)
				#	find_scripts | wrap
				# show available services
					/sbin/chkconfig --list | grep ':o' | cut -f1 | wrap
					;;
			esac
                        echo ')'
			in_kind=
                        ;;
		read)
			echo '('
			if [ "${in__objects%%/*}" = "new" ]; then
				cat "$INITDIR/${in__objects##*/}" | filter_description
				cat "$INITDIR/${in__objects##*/}" | filter_levels
			else
			    if [ "$in__objects" != "/" ]; then
				local status=$(/sbin/service "${in__objects##*/}" status | \
				tail -n 1 | \
				sed -r  -e 's,^[^[:space:]]+[[:space:]]+(.*)$,\1,g')
				test "$status" = "" && status="unknown"
				echo "status \"$status\""
				/sbin/chkconfig --list "${in__objects##*/}" | \
				sed -r  -e 's,^[^[:space:]]+,,g' \
					-e 's,([[:digit:]]):on,level\1 #t,g' \
					-e 's,([[:digit:]]):off,level\1 #f,g'
				cat "$INITDIR/${in__objects##*/}" | filter_description
			    else
				[ -n "$in_name" ] && echo "name \"$in_name\""
			    fi
			fi
			echo ')'
			;;
                write)
			[ -n "$RUNLEVEL" -a -n "$in_service" -a -n "$in_command" ] && {
				"$CHKCONFIG" --levels "$RUNLEVEL" "$in_service" "$in_command" #\
				#&& renew_services
			}
			[ -n "$in_add" ] && chkconfig --add "$in_disabled" &> /dev/null ||:
			if [ -n "$in_cardindexapply" -o -n "$in_add" ]; then
				local STOPLEVELS=
				local STARTLEVELS=
				local i=
				for i in `seq 0 6`; do
					eval "test \"\$in_level$i\" = \"#t\"" && STARTLEVELS="$STARTLEVELS$i"
					eval "test \"\$in_level$i\" = \"#f\"" && STOPLEVELS="$STOPLEVELS$i"
				done
				[ -n "$in_name" ] || in_name="$in_disabled"
				chkconfig --levels "$STARTLEVELS" "$in_name" on &> /dev/null ||:
				chkconfig --levels "$STOPLEVELS" "$in_name" off &> /dev/null ||:
			fi
                        echo "()"
                        ;;
		new)
			if [ -n "$in_name" ]; then
				chkconfig --add "$in_name" &> /dev/null ||:
			fi
			local STOPLEVELS=
			local STARTLEVELS=
			local i=
			for i in `seq 0 6`; do
			    eval "test \"\$in_level$i\" = \"#t\"" && STARTLEVELS="$STARTLEVELS$i"
			    eval "test \"\$in_level$i\" = \"#f\"" && STOPLEVELS="$STOPLEVELS$i"
			done
			chkconfig --levels "$STARTLEVELS" "$in_name" on &> /dev/null ||:
			chkconfig --levels "$STOPLEVELS" "$in_name" off &> /dev/null ||:
                        echo "()"
			;;
		delete)
			if [ -n "$in_name" ]; then
				chkconfig --del "$in_name" &> /dev/null ||:
			fi
                        echo "()"
			;;
		*)
			echo '#f'
			;;
        esac
			RUNLEVEL=$in_rv
			[ -n "$RUNLEVEL" ] || RUNLEVEL=`/sbin/runlevel | cut -c3`

} 
                         
message_loop
