description "Eucalyptus web services"
author "Matt Zimmerman <mdz@canonical.com>"

start on runlevel [23]

respawn

pre-start script
	# Check if installed
	[ -f /usr/sbin/euca_conf ] || { stop; exit 0; }

	mkdir -p /var/run/eucalyptus/net
	chown eucalyptus:eucalyptus /var/run/eucalyptus /var/run/eucalyptus/net

	. /etc/eucalyptus/eucalyptus.conf

	if [ "$EUCALYPTUS" = "not_configured" ]; then
		echo "Eucalyptus is not configured yet"
		stop
		exit 0
	fi
end script

script
	. /etc/eucalyptus/eucalyptus.conf

	opts="-h $EUCALYPTUS -u $EUCA_USER --pidfile /var/run/eucalyptus/eucalyptus.pid -L console-log"
	services=""

	# If the -cloud package is not installed, disable the cloud service
	if [ ! -f /usr/share/eucalyptus/eucalyptus-cloud-1.6-devel.jar ]; then
		opts="$opts --disable-cloud"
	else
		services="$services cloud"
	fi

	# If the -walrus package is not installed, disable the walrus service
	if [ ! -f /usr/share/eucalyptus/eucalyptus-walrus-1.6-devel.jar ]; then
		opts="$opts --disable-walrus"
	else
		services="$services walrus"
	fi

	# If the -sc package is not installed, disable the storage controller
        # service
	if [ ! -f /usr/share/eucalyptus/eucalyptus-storagecontroller-1.6-devel.jar ]; then
		opts="$opts --disable-storage"
	else
		services="$services sc"
	fi

	# handle eucalyptus.conf options
	if [ "$DISABLE_DNS" = "Y" ]; then
		opts="$opts --remote-dns"
	fi
	if [ "$DISABLE_EBS" = "Y" ]; then
		opts="$opts --disable-ebs"
	fi

	[ -n "$services" ] || { stop; exit 0; }
	exec eucalyptus-cloud $opts
end script
