#!/bin/sh

if [ "$#" -ne 1 ]; then
	echo "use: $0 <config-file>"
	exit 1
fi

CONFIG_FILE=$1

echo "Loading config file $config..."
if [ ! -f $CONFIG_FILE ]
then
	echo "$0: config file $CONFIG_FILE not found"
	exit 1
fi

fetch_field()
{
	grep $1 $CONFIG_FILE | cut -d " " -f 2
}

KEYPAIR_NAME=`fetch_field keypair_name`
SECURITY_GROUP_ID=`fetch_field security_group`
SUBNET=`fetch_field subnet`
ROUTE_TABLE=`fetch_field route_table`
GATEWAY=`fetch_field gateway`
VPC=`fetch_field vpc`

echo -n "Checking for aws command in PATH..."
if which aws >/dev/null 2>&1
then
        echo "ok"
else
        echo "failed"
        echo "$0: \"aws\" command must be in your path to use this script."
        exit 1
fi

echo "Deleting keypair $KEYPAIR_NAME..."
aws ec2 delete-key-pair --key-name $KEYPAIR_NAME && rm -f ${KEYPAIR_NAME}.pem

#echo "Deleting security group $SECURITY_GROUP_ID"
#aws ec2 delete-security-group --group-id $SECURITY_GROUP_ID

echo "Deleting internet gateway $GATEWAY..."
aws ec2 detach-internet-gateway --internet-gateway-id $GATEWAY --vpc-id $VPC
aws ec2 delete-internet-gateway --internet-gateway-id $GATEWAY

echo "Deleting subnet $SUBNET..."
aws ec2 delete-subnet --subnet-id $SUBNET

echo "Deleting virtual private cluster $VPC..."
aws ec2 delete-vpc --vpc-id $VPC

echo "Done!"

