#!/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:
%ec2din  (aka ls)
%limit   (aka .)
%pop     (aka ..)
%ec2ssh
%ec2run
%ec2start
%ec2stop
%ec2kill
%ec2watch
%account
%region

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

import os
from IPython.config.loader import PyFileConfigLoader
from IPython.frontend.terminal.ipapp import load_default_config

os.environ['IPYTHON_DIR'] = os.path.join(os.getenv('HOME'), '.iboto')

cfg = load_default_config()
cfg.TerminalInteractiveShell.confirm_exit = False # turn off annoyance
cfg.PromptManager.in_template = '{iboto}\n[\#]: '
cfg.InteractiveShell.autocall = 2

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