#!/usr/bin/python

import logging
import optparse
import readline
import sys

import blueprint
from blueprint import cli
from blueprint import context_managers
from blueprint import interactive

parser = optparse.OptionParser(
    'Usage: %prog [-m <message>] [-q] <src> <dest-a> <dest-b>')
parser.add_option('-m', '--message',
                  dest='message',
                  default=None,
                  help='commit message')
parser.add_option('-q', '--quiet',
                  dest='quiet',
                  default=False,
                  action='store_true',
                  help='operate quietly')
options, args = parser.parse_args()

if options.quiet:
    logging.root.setLevel(logging.CRITICAL)

if len(args) not in (2, 3):
    parser.print_usage()
    sys.exit(1)

b_s = blueprint.cli.read(options, args)
try:
    b_da = blueprint.Blueprint(args[-2])
except blueprint.NameError:
    logging.error('invalid blueprint name {0}'.format(args[-2]))
    sys.exit(1)
try:
    b_db = blueprint.Blueprint(args[-1])
except blueprint.NameError:
    logging.error('invalid blueprint name {0}'.format(args[-1]))
    sys.exit(1)

def choose():
    """
    Return the blueprint object indicated by the user's input.
    """
    prompt = '"{0}" or "{1}"? '.format(args[-2], args[-1])
    prefix = raw_input(prompt)
    while args[-2].startswith(prefix) == args[-1].startswith(prefix):
        print('Ambiguous; please give a unique prefix of a blueprint name.')
        prefix = raw_input(prompt)
    return {(True, False): b_da,
            (False, True): b_db}[(args[-2].startswith(prefix),
                                  args[-1].startswith(prefix))]

try:
    with context_managers.mkdtemp():
        interactive.walk(b_s, choose)
        b_da.commit(options.message or '')
        b_db.commit(options.message or '')
except IOError:
    pass
except KeyboardInterrupt:
    print('') # Expect this kind of thing in an interactive process.
