#!/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 ElastiCache 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_ELASTICACHE_HOME=${AWS_ELASTICACHE_HOME:-"/usr/share/elasticache"}
export jshared=${AWS_ELASTICACHE_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_ELASTICACHE_HOME}/lib" ] && LIBDIR="${AWS_ELASTICACHE_HOME}/lib" ||
   LIBDIR="${AWS_ELASTICACHE_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_ELASTICACHE_HOME
if [ -z "${AWS_ELASTICACHE_HOME}" ]; then
        echo AWS_ELASTICACHE_HOME is not set
        exit 1
fi

# Construct a list of the valid commands
valid_cmds=( "elasticache-authorize-cache-security-group-ingress"
	"elasticache-create-cache-cluster"
	"elasticache-create-cache-parameter-group"
	"elasticache-create-cache-security-group"
	"elasticache-delete-cache-cluster"
	"elasticache-delete-cache-parameter-group"
	"elasticache-delete-cache-security-group"
	"elasticache-describe-cache-clusters"
	"elasticache-describe-cache-parameter-groups"
	"elasticache-describe-cache-parameters"
	"elasticache-describe-cache-security-groups"
	"elasticache-describe-engine-default-parameters"
	"elasticache-describe-events"
	"elasticache-describe-reserved-cache-nodes"
	"elasticache-describe-reserved-cache-nodes-offeringsDescribes"
	"elasticache-modify-cache-cluster"
	"elasticache-modify-cache-parameter-group"
	"elasticache-purchase-reserved-cache-nodes-offering"
	"elasticache-reboot-cache-cluster"
	"elasticache-reset-cache-parameter-group"
	"elasticache-revoke-cache-security-group-ingress"
	"elasticache-version"
)

JAVA_CMD="${JAVA_COMMAND:-`which java`} ${AWS_ELASTICACHE_JVM_ARGS} -classpath ${cp} com.amazon.webservices.Cli"
ld=${AWS_ELASTICACHE_HOME:-/usr/share/elasticache}

[ "${link_cmd}" == "elasticache-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 elasticache was executed directly
if [ "${link_cmd}" == "elasticache" ]; 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}" == "elasticache" ]; 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 ElastiCache"
		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
