#!/usr/bin/env python

import sys

# check boto installed and new enough
try:
    import boto
    major, minor, _ = boto.Version.split('.', 2)
    if 'b' in minor:
        minor, beta = minor.split('b')
        if beta:
            beta = int(beta)
        else:
            beta = 1
    else:
        beta = sys.maxint
    major = int(major)
    minor = int(minor)
    if major < 2 or (major == 2 and minor == 0 and beta < 3):
        print >>sys.stderr, "You need at least boto 2.0b3 installed to run iboto (found %s)." % boto.Version
        sys.exit(-1)
except:
    print >>sys.stderr, "You need boto installed to run iboto:\n$ easy_install boto\nor\n$ pip install boto"
    sys.exit(-1)

try:
    from IPython.frontend.terminal.embed import InteractiveShellEmbed
    from IPython.config.loader import Config
except:
    print >>sys.stderr, "You need ipython >= 0.11 installed to run iboto:\n$ pip install --upgrade ipython"
    sys.exit(-1)

banner = """iboto ready

Commands available:
%ec2ssh
%ec2run   (aka %ec2-run-instances)
%ec2start (aka %ec2-start-instances)
%ec2stop  (aka %ec2-stop-instances)
%ec2kill  (aka %ec2-terminate-instances)
%ec2din   (aka %ec2-describe-instances)
%ec2watch
%region

'%command?' for more information.
"""

cfg = Config()
cfg.TerminalInteractiveShell.confirm_exit = False # turn off annoyance
cfg.PromptManager.in_template = '{ec2_region_name} [\#]: '
cfg.PromptManager.in_template

ipshell = InteractiveShellEmbed(
    config = cfg,
    banner1 = banner,
    exit_msg = 'Leaving iboto',
    user_ns = {})
ipshell.extension_manager.load_extension('iboto.ipythonext')
ipshell()
