#!/bin/bash
### BEGIN INIT INFO
# Provides:          eucalyptus-cloud
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Eucalyptus cloud controller
# Description:       Start the Eucalyptus cloud controller
### END INIT INFO
#
# Init script for the Eucalyptus cloud controller.
#
# chkconfig: 2345 99 05
# description: script for starting and stopping eucalyptus
#
# $Id: eucalyptus.in,v 1.6 2009-01-06 00:14:41 graziano Exp $
#

# Do NOT "set -e"

# if we are on rocks we need the rocks binaries 
[ -e /etc/profile.d/rocks-binaries.sh ] && . /etc/profile.d/rocks-binaries.sh

# I just wanted to set PATH to be the system PATH, but if a user install
# eucalyptus manually, it may have binaries in a non-standard position:
# hence we need to keep the PATH we receive.
PATH=/sbin:/usr/sbin:/bin:/usr/bin:$PATH
DESC="Eucalyptus Cloud Controller"
NAME=eucalyptus
BRCTL="`which brctl 2> /dev/null`"
ANT="`which ant 2> /dev/null`"
JAVA="`which java 2> /dev/null`"
MODPROBE="`which modprobe 2> /dev/null`"

EUCALYPTUS="/"
export EUCALYPTUS
AXIS2C_HOME="/usr/lib/axis2"
export AXIS2C_HOME

. /lib/lsb/init-functions

# crude way to start the axis2c services
do_start() {
    mkdir -p /var/run/eucalyptus
    chown eucalyptus:eucalyptus /var/run/eucalyptus
    cd /etc/eucalyptus
    (su $EUCA_USER -c "/bin/bash $ANT -f cloud.xml run" > /var/log/eucalyptus/cloud-output.log 2>&1)  &
    echo "$!" > /var/run/eucalyptus/eucalyptus-cloud.pid
}

do_stop() {
    pidfile=/var/run/eucalyptus/eucalyptus-cloud.pid
    if test -s $pidfile
    then
        pid=`cat $pidfile 2> /dev/null`
        kill $pid > /dev/null 2>&1 
    fi
    timeout=5
    while [ $timeout -ge 0 ]
	do
        if ps $pid > /dev/null 2>&1
        then
            sleep 1
            timeout=$(($timeout - 1))
        else
            break
        fi
    done
    if [ $timeout -eq 0 ]
    then
        kill -9 $pid > /dev/null 2>&1
    fi
    rm -f $pidfile
}

# Read configuration variable file if it is present
if [ -r $EUCALYPTUS/etc/eucalyptus/$NAME.conf ]; then
	. $EUCALYPTUS/etc/eucalyptus/$NAME.conf
else
	echo "Cannot find eucalyptus configuration file!"
	exit 1
fi
if [ "$EUCALYPTUS" = "not_configured" ]; then
	echo "EUCALYPTUS not configured!" 
	exit 1
fi
if [ -n "$TEMPLATE_PATH" ]; then
	echo "TEMPLATE_PATH is defined: do you have an old configuration?"
	exit 1
fi

START_CLOUD="`echo $START_CLOUD | tr 'a-z' 'A-Z'`"
START_CC="`echo $START_CC | tr 'a-z' 'A-Z'`"
START_NC="`echo $START_NC | tr 'a-z' 'A-Z'`"
if [ -z "$EUCA_USER" ] ; then
	EUCA_USER="root"
fi

# let's see if the user exists
ID="`which id 2> /dev/null`"
if [ -n "$ID" ]; then
	if ! $ID $EUCA_USER > /dev/null 2> /dev/null ; then
		echo "User $EUCA_USER doesn't exists!"
		exit 1
	fi
fi

# let's try to pick the system apache2
HTTPD="`which apache2 2> /dev/null`"		# .deb based machines
if [ -z  "$HTTPD" ]; then
	HTTPD="`which httpd 2> /dev/null`"	# .rpm based machines
fi
HTTPD_HOME="/tmp/"

# set up eucalyptus var locations to be owned by EUCA_USER
for y in $EUCALYPTUS/var/lib/eucalyptus/ $EUCALYPTUS/var/run/eucalyptus/ $EUCALYPTUS/var/log/eucalyptus/ $INSTANCE_PATH; do
    if [ $y != "/" -a -d $y ]; then
	chown -R $EUCA_USER:$EUCA_USER $y
    fi
done

# let's find our dependencies
for y in $EUCALYPTUS/opt $EUCALYPTUS/packages $EUCALYPTUS /opt ; do
	for x in `/bin/ls $y 2> /dev/null`; do
		# this is for the RPM installed pacakges
		if [ "$x" = "euca-libvirt" ]; then
			export LIBVIRT_HOME="$y/$x"
		fi
		if [ "$x" = "euca-httpd" ]; then
			export HTTPD=$y/$x/bin/httpd
			export HTTPD_HOME="$y/$x"
		fi
		if [ "$x" = "euca-axis2c" ]; then
			export AXIS2C_HOME="$y/$x"
		fi
		# this is for system or source installed
		if [ "`echo $x | cut -f 1 -d -`" = "axis2c" ]; then
			export AXIS2C_HOME="$y/$x"
		fi
		if [ "`echo $x | cut -f 1 -d -`" = "libvirt" ]; then
			export LIBVIRT_HOME="$y/$x"
		fi
		if [ "`echo $x | cut -f 1 -d -`" = "httpd" ]; then
			if [ -x $y/$x/bin/httpd ]; then
				export HTTPD="$y/$x/bin/httpd"
				export HTTPD_HOME="$y/$x"
			elif [ -x $y/$x/usr/bin/httpd ]; then
				export HTTPD="$y/$x/usr/bin/httpd"
				export HTTPD_HOME="$y/$x"
			elif [ -x $y/$x/usr/sbin/httpd ]; then
				export HTTPD="$y/$x/usr/sbin/httpd"
				export HTTPD_HOME="$y/$x"
			fi
		fi
	done
done

# finally let's see if the basics tools are here
if [ "$START_CC" = "Y" -o "$START_NC" = "Y" ]; then
	if [ -z "$AXIS2C_HOME" ]; then
		echo "Cannot find Eucalyptus webservices: missing AXIS2C_HOME?"
		exit 1
	fi
fi

# set the library path correctly
export LD_LIBRARY_PATH="$AXIS2C_HOME/lib:$LIBVIRT_HOME/lib:$LIBVIRT_HOME/usr/lib:$EUCALYPTUS/lib:$LD_LIBRARY_PATH"

case "$1" in
    start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
       	case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac

	;;
  stop)
		log_begin_msg "Stopping $DESC"
        do_stop
		log_end_msg 0
	;;
    restart|reload)
        log_begin_msg "Restarting $DESC"
        do_stop
        do_start
  	;;
    *)
        echo "Usage: $NAME {start|stop|restart}" >&2
        exit 3
	;;
esac

:
