#!/bin/bash -f

PATH=$path:/bin:/usr/bin:/sbin:/usr/sbin

plugin_dir="/etc/repairer"
config="$plugin_dir/services.conf"
PLACE=".1.3.6.1.4.1.7088.3"
REQ="$2"

if [ "x$1" = "x" ]; then
    exit 0
fi

handle_process() {
    if [ -f $plugin_dir/$1/config ]; then
	. $plugin_dir/$1/config
    fi
    COMMAND=`echo $2|sed 's#\"##g'`
    case "$COMMAND" in
	    start)
		initlog -s "Starting $1"
		if [ -f /etc/rc.d/init.d/$1 ]; then
		    if [ -f $plugin_dir/$1/pre-start ]; then
			$plugin_dir/$1/pre-start
		    fi
		    if [ ! "x$NOT_SUBSYS" = "xyes" ]; then
			initlog -c "/etc/rc.d/init.d/$1 start"
		    fi
		    if [ -f $plugin_dir/$1/post-start ]; then
			$plugin_dir/$1/post-start
		    fi
		fi
		;;
	    stop)
		initlog -s "Stopping $1"
		if [ -f /etc/rc.d/init.d/$1 ]; then
		    if [ -f $plugin_dir/$1/pre-stop ]; then
			$plugin_dir/$1/pre-stop
		    fi
		    if [ ! "x$NOT_SUBSYS" = "xyes" ]; then
			initlog -c "/etc/rc.d/init.d/$1 stop"
		    fi
		    if [ -f $plugin_dir/$1/post-stop ]; then
			$plugin_dir/$1/post-stop
		    fi
		fi
		;;
	    restart)
		initlog -s "Restarting $1"
		if [ -f /etc/rc.d/init.d/$1 ]; then
		    if [ -f $plugin_dir/$1/pre-stop ]; then
			$plugin_dir/$1/pre-stop
		    fi
		    if [ ! "x$NOT_SUBSYS" = "xyes" ]; then
			initlog -c "/etc/rc.d/init.d/$1 stop"
		    fi
		    if [ -f $plugin_dir/$1/post-stop ]; then
			$plugin_dir/$1/post-stop
		    fi
		    if [ -f $plugin_dir/$1/pre-start ]; then
			$plugin_dir/$1/pre-start
		    fi
		    if [ ! "x$NOT_SUBSYS" = "xyes" ]; then
			initlog -c "/etc/rc.d/init.d/$1 start"
		    fi
		    if [ -f $plugin_dir/$1/post-start ]; then
			$plugin_dir/$1/post-start
		    fi
		fi
		;;
	    *)
		initlog -s "Unknown command ($2) for $1"
		;;
    esac
    exit 0
}

find_name_by_number() {
    number=`echo $1|sed "s/$PLACE.//"`
    cat $config | while read num subsys_name ; do
	if [ "$num" = "$number" ] ; then
	    echo ${subsys_name}
	    return
	fi
    done
}


if [ "$1" = "-s" ]; then
    PROCESS=`find_name_by_number "$REQ"`
    if [ "x$PROCESS" != "x" ]; then
	handle_process $PROCESS $4
    fi
    exit 0
fi

if [ "$1" = "-n" ]; then
  MAX=`cat $config |grep -v ^#|tail -1|cut -f 1`
  case "$REQ" in
    $PLACE)      		RET=$PLACE.1 ;;
    "$PLACE.$(($MAX-1))")	RET=$PLACE.$MAX ;;
    *)         	  		
				declare -i number
				number=`echo $REQ|sed "s/$PLACE.//"`
				if [ $number -lt $(($MAX-1)) -a $number -gt 0 ] ; then
				    RET=$PLACE.$(($number+1))
				else
				    exit 0;
				fi
				;;
  esac
else
  case "$REQ" in
    $PLACE)    exit 0 ;;
    *)         RET=$REQ ;;
  esac
fi

RUNLEVEL=`runlevel|cut -d ' ' -f 2`

echo "$RET"
PROCESS=`find_name_by_number "$RET"`
if [ "x$PROCESS" != "x" ]; then
    if [ ! -e /etc/rc.d/rc${RUNLEVEL}.d/S??$PROCESS -a -e /etc/rc.d/rc${RUNLEVEL}.d/K??$PROCESS ]; then
	STATUS="$PROCESS in not activated on runlevel $RUNLEVEL"
    elif [ ! -e /etc/rc.d/init.d/$PROCESS ]; then
	STATUS="$PROCESS in not installed"
    else
	STATUS=`/etc/rc.d/init.d/$PROCESS status`
    fi
    echo "string"; echo "$STATUS"
else
    echo "string"; echo "ack... $RET $REQ"
fi
exit 0

