#!/bin/bash
# 
# EC2 API Tools dispatcher
# Lubomir Rintel (GoodData) <lubo.rintel@gooddata.com>

# Source functions library
if [ -f /usr/share/java-utils/java-functions ] ; then 
  . /usr/share/java-utils/java-functions
else
  echo "Can't find functions library, aborting"
  exit 1
fi

# Load system-wide configuration
if [ -f /etc/ec2-cmd.conf ]; then
  . /etc/ec2-cmd.conf
fi

# Load user configuration
if [ -f "$HOME/.ec2-cmdrc" ]; then
  . "$HOME/.ec2-cmdrc"
fi

CMD=$1; shift
if [ -z "$CMD" ]
then
	echo "Missing an argument. Usage: $0 <argument>" >&2
	exit 1
fi

# Bundled
BASE_JARS="$BASE_JARS ec2-api-tools"

# Dependencies
BASE_JARS="$BASE_JARS commons-cli"
BASE_JARS="$BASE_JARS commons-codec"
BASE_JARS="$BASE_JARS commons-discovery"
BASE_JARS="$BASE_JARS commons-httpclient"
BASE_JARS="$BASE_JARS commons-logging"
BASE_JARS="$BASE_JARS activation"
BASE_JARS="$BASE_JARS bcprov"
BASE_JARS="$BASE_JARS bea-stax-api"
BASE_JARS="$BASE_JARS javamail"
BASE_JARS="$BASE_JARS jaxme/jaxmeapi"
BASE_JARS="$BASE_JARS jdom"
BASE_JARS="$BASE_JARS log4j"
BASE_JARS="$BASE_JARS wsdl4j"
BASE_JARS="$BASE_JARS xalan-j2"
BASE_JARS="$BASE_JARS xalan-j2-serializer"
BASE_JARS="$BASE_JARS xerces-j2"

# Rest of the configuration
MAIN_CLASS=com.amazon.aes.webservices.client.cmd.$CMD

# Set parameters
set_jvm
set_classpath $BASE_JARS
set_flags $BASE_FLAGS
set_options $BASE_OPTIONS $EC2CMD_OPTS

# Let's start
run "$@"
