#!/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 Cloud Watch JAR files. This
## script replaces all the Cloud Watch 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 Cloud Watch commands"
	echo "Usage:"
	echo "   --desc <name>   : Shows help for a single Cloud Watch Command"
	echo "   --commands      : Shows available command line programs"
	echo "   --help          : Shows this menu"
	echo "   <COMMAND> <OPT> : Run Cloud Watch commands directly"
}


# Set home paths
export AWS_CLOUDWATCH_HOME=${AWS_CLOUDWATCH_HOME:-"/usr/share/moncli"}
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_CLOUDWATCH_HOME}/lib" ] && LIBDIR="${AWS_CLOUDWATCH_HOME}/lib" ||
   LIBDIR="${AWS_CLOUDWATCH_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_CLOUDWATCH_HOME
if [ -z "${AWS_CLOUDWATCH_HOME}" ]; then
        echo AWS_CLOUDWATCH_HOME is not set
        exit 1
fi

# Construct a list of the valid commands
valid_cmds=( "mon-delete-alarms"
	"mon-describe-alarm-history"
	"mon-describe-alarms"
	"mon-describe-alarms-for-metric"
	"mon-disable-alarm-actions"
	"mon-enable-alarm-actions"
	"mon-get-stats"
	"mon-list-metrics"
	"mon-put-data"
	"mon-put-metric-alarm"
	"mon-set-alarm-state"
        "mon-version"
)

JAVA_CMD="${JAVA_COMMAND:-`which java`} ${AWS_CLOUDWATCH_JVM_ARGS} -classpath ${cp} com.amazon.webservices.Cli"
ld=${AWS_CLOUDWATCH_HOME:-/usr/share/moncli}

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

# Execute the command
for cmd in ${valid_cmds[@]}
do
	if [ "${link_cmd}" = "${cmd}" ]; then
		exec ${JAVA_CMD} "${cmd}" "${@}"
	fi
done

# Check if moncli was executed directly
if [ "${link_cmd}" == "moncli" ]; 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}" == "moncli" ]; 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 Cloud Watch"
		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
