#! /bin/sh
#
# vargus
#
# chkconfig: 345 99 60
# description: Start vargus daemons 
# pidfile: /var/run/vargus/*.pid

# Source function library.
. /etc/init.d/functions


RETVAL=0
BASE=vargus
BASEBIN=/usr/bin/$BASE
CONFIG_DIR=/etc/$BASE
PIDFILEDIR=/var/run/
PIDFILEBASE="$PIDFILEDIR/$BASE"
LOCKFILEDIR=/var/lock/subsys
LOCKFILEBASE="$LOCKFILEDIR/$BASE"
VARGUS_USER=vargus


enabled_roles()
{
	ROLES=`(cd $CONFIG_DIR; /bin/ls [0-9][0-9]-*[^~] 2>/dev/null)`
}

expand_role()
{
	RET=0
	local ROLE=$1
	local SEARCH_RESULT
	if [ -f "$CONFIG_DIR/$ROLE" ] ; then
		echo $ROLE
	else
		SEARCH_RESULT=`(cd $CONFIG_DIR; /bin/ls [0-9][0-9]-$ROLE 2>/dev/null)`
		if [ -z "$SEARCH_RESULT" ]; then
			echo "No role $ROLE found"
			RET=1
		elif [ `echo "$SEARCH_RESULT" |wc -l` -gt 1 ]; then
			echo "More than one roles $ROLE found, specify exactly"
			RET=1
		else
			echo $SEARCH_RESULT
		fi
	fi
	return $RET
}

is_locked()
{
	INSTANCES_LIST=`/bin/ls $LOCKFILEBASE-[0-9]* 2>/dev/null`
	if [ -n "$INSTANCES_LIST" ]; then
		return 0
	else
		return 1
	fi
}


#===== Put the list of running roles to the $ROLES =====
running_roles()
{
        local LIST
        local foo
        local bar

        LIST=`/bin/ls $PIDFILEBASE/[A-Za-z0-9]*.pid 2>/dev/null`
        ROLES=""
        if [ -n "$LIST" ]; then
                for foo in $LIST; do
                        bar=`echo $foo | sed -e 's?.pid??' | sed -e "s?$PIDFILEBASE/??"`
                        [ -n "$ROLES" ] && ROLES="$ROLES "
                        ROLES="$ROLES$bar"
                done
        fi
        return 0
}



start_role()
{
	local ROLE

	if ROLE=`expand_role $1`; then
		if [ ! -x $CONFIG_DIR/$ROLE -a -z "$MANUAL_MODE" ]; then
			msg_starting $BASE
			echo -n "$ROLE not active"
			passed
			echo ""
			return
		fi

		if [ $COUNTER -gt 0 ]; then
			sleep 0.5
		fi

		start_daemon 	--pidfile "$PIDFILEBASE/$ROLE.pid" \
				--lockfile "$LOCKFILEBASE-$ROLE" \
				--displayname "vargus role $ROLE" \
				--name $BASE -- \
				$BASEBIN $CONFIG_DIR/$ROLE
		RETVAL=$?
	else
		msg_starting $BASE 
		echo -n "$ROLE"
		failure
		echo
		RETVAL=1
	fi

	return $RETVAL
	
}

start()
{
	local ROLE
	local RETVAL

	RETVAL=0
	if [ -z "$ROLES" ]; then
		enabled_roles
	else
		MANUAL_MODE=1
	fi

	COUNTER=0
	if [ -z "$ROLES" ]; then
		msg_starting $BASE
		echo -n "No roles to start!"
		failure "$BASE startup"
		echo
		echo "Place configuration files in $CONFIG_DIR"
		echo
		RETVAL=1
	else
		for ROLE in $ROLES; do
			start_role $ROLE
			RETVAL=$?
			let COUNTER=COUNTER+1
		done
	fi
	return $RETVAL
}



stop_role()
{
	local ROLE
	local RETVAL

	if ROLE=`expand_role $1`; then
		stop_daemon 	--pidfile "$PIDFILEBASE/$ROLE.pid" \
				--retry 5 \
				--lockfile "$LOCKFILEBASE-$ROLE" \
				--expect-user $VARGUS_USER \
				--name $BASE \
				--displayname "vargus role $ROLE" -- \
				$BASEBIN
		RETVAL=$?
	else
		msg_stopping $BASE
		echo -n "$ROLE"
		failure
		echo
		RETVAL=1
	fi

	return $RETVAL	
}

stop()
{
	local ROLE
	local RETVAL

	RETVAL=0
	if [ -z "$ROLES" ]; then
		running_roles
	fi

	ROLES=`echo $ROLES |tac -s ' '`

        if [ -z "$ROLES" ]; then
                msg_not_running $BASE
                echo
                RETVAL=1
	else
		for ROLE in $ROLES; do
			stop_role $ROLE
			RETVAL=$?
		done
	fi
	return $RETVAL
}


restart()
{
        local RETVAL
	local ROLE

        RETVAL=0

        if [ -z "$ROLES" ]; then # Restart every running role
                running_roles
        fi

	COUNTER=0
        if [ -z "$ROLES" ]; then
                msg_not_running $BASE
                echo
                RETVAL=1
        else
		for ROLE in $ROLES; do
			stop_role $ROLE
			RETVAL=$?
			start_role $ROLE
			let RETVAL=$RETVAL+$?
			let COUNTER=COUNTER+1
		done
	fi
        return $RETVAL
}

# Print list of running roles
list_roles()
{
        local ROLE

        running_roles
        if [ -z "$ROLES" ]; then
                echo "No roles have running now, $BASE is stopped."
        else
                echo "Running roles:"
                for ROLE in $ROLES; do
                        echo "    $ROLE"
                done
                echo
        fi
        return 0
}

show_status()
{
        local RETVAL
        local ROLE
        local st

        RETVAL=0
        if [ -z "$ROLES" ]; then # Show status of every running role
                running_roles
        fi
        if [ -z "$ROLES" ]; then
                msg_not_running $BASE
                echo
                RETVAL=1
        else
                for ROLE in $ROLES; do
			if ROLE=`expand_role $ROLE`; then
				status 	--displayname "vargus role $ROLE" \
					--expect-user $VARGUS_USER --lockfile "$LOCKFILEBASE-$ROLE" \
					--name $BASE --pidfile "$PIDFILEBASE/$ROLE.pid" -- $BASEBIN
			else
				echo "$ROLE"
				RETVAL=1
			fi
		done
	fi

        return $RETVAL
}


OP=$1
shift
ROLES=$@


# See how we were called.
case "$OP" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload|restart)
		restart
        	;;
	condstop)
		is_locked && stop
		;;
	condreload|condrestart)
		is_locked && restart
		;;
	status)
		show_status	
	        ;;
        list)
                list_roles
                ;;
	*)
		msg_usage "${0##*/} {start|stop|restart|list|status}"
		RETVAL=1
esac

exit $RETVAL
