#!/bin/bash
#
## Copyright 2012, Ben Howard <ben.howard@ubuntu.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
## This is a simple wrapper around the AWS Auto Scaling JAR files. This
## script replaces all the Auto Scaling scripts and makes it easy to
## both maintain and add new features.

# Find out what command are we running
link_cmd="${0##*/}"

usage() {
	echo "${0##*/} is a wrapper script for invoking Auto Scaling commands"
	echo "Usage:"
	echo "   --desc <name>   : Shows help for a single Auto Scaling Command"
	echo "   --commands      : Shows available command line programs"
	echo "   --help          : Shows this menu"
	echo "   <COMMAND> <OPT> : Run Auto Scaling commands directly"
}


# Set home paths
export AWS_AUTO_SCALING_HOME=${AWS_AUTO_SCALING_HOME:-"/usr/share/ascli"}
export jshared=${AWS_AUTO_SCALING_JAVA_SHARED_CLASSPATH:-"/usr/share/java/activation.jar:/usr/share/java/commons-cli.jar:/usr/share/java/commons-codec.jar:/usr/share/java/commons-discovery.jar:/usr/share/java/commons-httpclient.jar:/usr/share/java/commons-logging.jar:/usr/share/java/commons-logging-api.jar:/usr/share/java/jdom1.jar:/usr/share/java/joda-time.jar:/usr/share/java/log4j-1.2.jar:/usr/share/java/serializer.jar:/usr/share/java/stax-api.jar:/usr/share/java/wsdl4j.jar:/usr/share/java/wss4j.jar:/usr/share/java/xalan2.jar"}

# Discover service home for jar file discovery
[ -d "${AWS_AUTO_SCALING_HOME}/lib" ] && LIBDIR="${AWS_AUTO_SCALING_HOME}/lib" ||
   LIBDIR="${AWS_AUTO_SCALING_HOME}"

# Preserve classpaths an
for j in "${LIBDIR}"/*.jar; do cp="${cp:+${cp}:}${j}"; done
[ -n "${CLASSPATH%:}" ] && cp="${CLASSPATH%:}:${cp}"
[ "${jshared#@@}" != "${jshared}" ] || cp="${cp}:${jshared}"


# Check AWS_AUTO_SCALING_HOME
if [ -z "${AWS_AUTO_SCALING_HOME}" ]; then
        echo AWS_AUTO_SCALING_HOME is not set
        exit 1
fi

# Construct a list of the valid commands
valid_cmds=("as-create-auto-scaling-group"
	"as-create-launch-config"
	"as-create-or-update-tags"
	"as-delete-auto-scaling-group"
	"as-delete-launch-config"
	"as-delete-notification-configuration"
	"as-delete-policy"
	"as-delete-scheduled-action"
	"as-delete-tags"
	"as-describe-adjustment-types"
	"as-describe-auto-scaling-groups"
	"as-describe-auto-scaling-instances"
	"as-describe-auto-scaling-notification-types"
	"as-describe-launch-configs"
	"as-describe-metric-collection-types"
	"as-describe-notification-configurations"
	"as-describe-policies"
	"as-describe-process-types"
	"as-describe-scaling-activities"
	"as-describe-scheduled-actions"
	"as-describe-tags"
	"as-disable-metrics-collection"
	"as-enable-metrics-collection"
	"as-execute-policy"
	"as-put-notification-configuration"
	"as-put-scaling-policy"
	"as-put-scheduled-update-group-action"
	"as-resume-processes"
	"as-set-desired-capacity"
	"as-set-instance-health"
	"as-suspend-processes"
	"as-terminate-instance-in-auto-scaling-group"
	"as-update-auto-scaling-group"
	"as-version")

JAVA_CMD="${JAVA_COMMAND:-`which java`} ${AWS_AUTO_SCALING_JVM_ARGS} -classpath ${cp} com.amazon.webservices.Cli"
ld=${AWS_AUTO_SCALING_HOME:-/usr/share/ascli}

[ "${link_cmd}" == "as-version" ] && exec ${JAVA_CMD} version

# Execute the command
for cmd in ${valid_cmds[@]}
do
	[ "${link_cmd}" == "as-version" ] && link_cmd="version"

	if [ "${link_cmd}" = "${cmd}" ]; then
		exec ${JAVA_CMD} "${cmd}" "${@}"
	fi
done

# Check if ascli was executed directly
if [ "${link_cmd}" == "ascli" ]; then
	if [ "${1}" == "--commands" ]; then

		# Show valid commands
		for cmd in "${valid_cmds[@]}"; do echo "$cmd"; done
		exit 0

	elif [ "${1}" == "--desc" ]; then

		if [ "${2}" == "Command" ]; then
			# If no command is defined, show all
			exec ${JAVA_CMD}

		elif [ "${2}" == "ascli" ]; then
			usage
			exit 0

		else
			# Describe individual command
			exec ${JAVA_CMD} "${2}" "--help"
		fi

	elif [ "${1}" == "--version" ]; then

		# Get the version
		exec ${JAVA_CMD} version

	elif [ "${1}" == "--help" -o "${1}" == "-h" ]; then

		usage
		exit 0

	elif [[ "${1}" =~ "as-" ]]; then

		# Direct execution of command, by passing link
		exec ${JAVA_CMD} "${@}"

	else

		# Otherwise command is invalid
		ver=$($JAVA_CMD version)
		echo "${ver}"
		echo "${0} is a simple wrapper script used for executing AWS Auto Scaling"
		echo "commands. Please see --help or --commands determine what commands"
		echo "are available"
		exit 1

	fi
fi

echo "${link_cmd} is an invalid command!"
exit 1
