#! /usr/bin/python
#########################
# by Jan-Philip Gehrcke
# 08-10-12
#########################

import os, sys

print "=================== a script to invoke ec2-upload-bundle ======================"
print "make sure that $EC2_HOME, $AWS_ACCESS_KEY_ID and $AWS_SECRET_ACCESS_KEY are set"
print "\n          usage: -b S3bucketName -m PathToImageManifest"
print "===============================================================================\n"


nArgs = len(sys.argv)
if (nArgs < 5):
    print "not enough arguments given!\n\n"
    sys.exit()
    
argDict = {}
for i in range(nArgs):
    if sys.argv[i] == "-b": argDict['bucket'] = sys.argv[i+1]
    if sys.argv[i] == "-m": argDict['manifestPath'] = sys.argv[i+1]


uploadcommand = "ec2-upload-bundle -a $AWS_ACCESS_KEY_ID -s $AWS_SECRET_ACCESS_KEY" + " -b " + argDict['bucket'] + " -m " + argDict['manifestPath']

print "\ncommand:"
print uploadcommand + "\n"

while True: # wait for instring to be a known command
    while True: # wait for an errorfree input
        try:
            instring = raw_input("execute? (y/n): ")
            break
        except:
            pass
    if (instring == "y") or (instring == "n"): 
        break

if instring == "y": os.system(uploadcommand)
