#!/bin/bash
KeysLocation=""
amiID=""
export EC2_HOME="/home/mohamed/sources/ec2-api-tools-1.3-62308/";
#export classpath="/home/mohamed/apache-tomcat-6.0.18/lib";
export EC2_PRIVATE_KEY="$1";
export EC2_CERT="$2";
export JAVA_HOME="/usr/lib/jvm/java-6-sun";
IP=""
InstanceID=""
status=""
LocalIP=""
PublicDNS=""

function quit
{
	EC2_HOME=${EC2_HOME}
	r=$EC2_HOME/bin/ec2-release-address $IP
	result=$($r)
#	echo $result
}

function assosiateIP
{
	EC2_HOME=${EC2_HOME}
	r="$EC2_HOME/bin/ec2-associate-address -i $1 $2"
#	echo $r
	result=$($r)
#	echo "IP assosiate"
	sleep 10
	echo $2
}

function parseInstanceData
{
	args=("$@")
	LocalIP=${args[18]}
	PublicDNS=${args[7]}
}

function parseInstanceID {
args=("$@")
InstanceID=${args[5]}
}

function getLocalIP {
	args=("$@")
	echo ${args[18]}
}

function getStatus {
	args=("$@")
	status=${args[9]}
	echo ${args[9]}
}

function allocateIP {
	EC2_HOME=${EC2_HOME}
	r=$EC2_HOME/bin/ec2-allocate-address
	result=$($r)
	ins=0
	#echo $result
for word in $result
do
	if [ "$word" = "ADDRESS" ]; then
		#echo $word
		ins=1
		continue
	fi
	if [ $ins = 1 ]; then
		IP=$word
#		echo "IP: "$IP
		break
	fi
done
}
if [ "$3" = "EU1" ]; then
	export EC2_URL="https://ec2.eu-west-1.amazonaws.com"
	#KeysLocation="MyEUKey"
#	amiID="ami-8bc9f4ff" #HPCloud with Node Management
	amiID="ami-bfe6dbcb" #HPCloud
	#amiID="ami-4a41703e" #By Machine Type
#	amiID="ami-e0dbed94" #By Cores
	#amiID="ami-45b18631"
	
#	echo "EU"
elif [ "$3" = "Asia1" ]; then
	KeysLocation="MyAsianKey"
	export EC2_URL="https://ec2.ap-southeast-1.amazonaws.com"
	amiID="ami-c2f68890"
#	echo "Asia"
elif [ "$3" = "USW1" ]; then
	KeysLocation="MyUSWestKey"
        export EC2_URL="https://ec2.us-west-1.amazonaws.com"

elif [ "$3" = "USE1" ]; then
	KeysLocation="MyUSEastKey"
        export EC2_URL="https://ec2.us-east-1.amazonaws.com"
else
	echo "This is region is not supported."
	exit 1
fi

if [ "$4" == "--create" ]; then
	instanceType=""
	#echo $amiID
	#echo $KeysLocation
	#if [ "$5" = "1" ]; then
	#instanceType="t1.micro"
	#elif [ "$5" = "2" ]; then
	#	instanceType="m1.large"
	#elif [ "$5" = "4" ]; then 
	#	instanceType="m1.xlarge"
	#elif [ "$5" = "8" ]; then
	#	instanceType="c1.xlarge"
	
	#fi 
	echo "Starting EC2 instance.............."
	img="${EC2_HOME}bin/ec2-run-instances $amiID -k $7 -g $8 -t $5 -n $6" 
	echo $img
	instance=$($img)
	
	echo  $instance 
	exit 0
	i=0

elif [ "$4" == "--details" ]; then
 	while [ "$i"=="0" ];
	do
	#echo "Waiting the instance to run........"
	IP=$(${EC2_HOME}bin/ec2-describe-instances $5)
	#echo $IP
	getStatus $IP 
	if [ "$status" = "running" ]; then
		parseInstanceData $IP
		i=1
		echo "URL = '$PublicDNS'"
		echo "Private IP = $LocalIP"	
		break
	fi
	done

elif [ "$4" == "--terminate" ]; then
	echo "Terminating EC2 instance.............."
	img="${EC2_HOME}bin/ec2-terminate-instances $5" 
	echo $($img)
	
elif [ "$4" == "--keypairs" ]; then
        img="${EC2_HOME}bin/ec2-describe-keypairs"
        echo $($img)

elif [ "$4" == "--groups" ]; then
        img="${EC2_HOME}bin/ec2-describe-group"
        echo "$($img)"


fi


#allocateIP
#echo "Instance ID: " $InstanceID
#echo "IP:" $IP
#InstanceID="i-29bf405f"
#IP="46.137.110.148"
#assosiateIP $InstanceID $IP
#echo $result
