#!/usr/bin/env python


# First-party Python libraries
import os
import sys


# Third-party Python libraries
from termcolor import cprint


# AppScale import, the library that we're wrapping this executable
# around
try:
  from appscale.appscale import AppScale
except ImportError:
  lib = os.path.dirname(__file__) + os.sep + ".." + os.sep + "appscale"
  sys.path.append(lib)
  from appscale import AppScale

from custom_exceptions import AppScaleException
from custom_exceptions import AppScalefileException
from custom_exceptions import BadConfigurationException


appscale = AppScale()
if len(sys.argv) < 2:
  appscale.help()

command = sys.argv[1]
if command == "init":
  if len(sys.argv) < 3:
    cprint("Usage: appscale init <cloud or cluster>", 'red')
    print("Specify 'cloud' for EC2 and Eucalyptus deployments, " +
      "and 'cluster' if running over a virtualized cluster.")
    sys.exit(1)

  try:
    appscale.init(sys.argv[2])
  except Exception as e:
    cprint(e, 'red')
    sys.exit(1)

  cprint("AppScalefile successfully created! Be sure to " +
    "customize it for your particular cloud or cluster.", 'green')
  sys.exit(0)
elif command == "up":
  try:
    appscale.up()
  except Exception as e:
    cprint(e, 'red')
    sys.exit(1)
elif command == "ssh":
  if len(sys.argv) < 3:
    index = 0
  else:
    index = sys.argv[2]

  try:
    appscale.ssh(index)
  except Exception as e:
    cprint(e, 'red')
    sys.exit(1)
  except KeyboardInterrupt:
    # don't print the stack trace on a Control-C
    pass
elif command == "status":
  try:
    appscale.status()
  except Exception as e:
    cprint(e, 'red')
    sys.exit(1)
elif command == "deploy":
  try:
    # TODO(cgb): Make sure argv[2] exists, and print helpful info if not
    appscale.deploy(sys.argv[2])
  except Exception as e:
    cprint(e, 'red')
    sys.exit(1)
elif command == "undeploy" or command == "remove":
  try:
    # TODO(cgb): Make sure argv[2] exists, and print helpful info if not
    appscale.undeploy(sys.argv[2])
  except Exception as e:
    cprint(e, 'red')
    sys.exit(1)
elif command == "tail":
  if len(sys.argv) < 3:
    # by default, tail the first node's logs, since that node is
    # typically the head node
    index = 0
  else:
    index = sys.argv[2]

  if len(sys.argv) < 4:
    # by default, tail the AppController logs, since that's the
    # service we most often tail from
    regex = "controller*"
  else:
    regex = sys.argv[3]

  try:
    appscale.tail(index, regex)
  except KeyboardInterrupt:
    # don't print the stack trace on a Control-C
    pass
  except Exception as e:
    cprint(e, 'red')
    sys.exit(1)
elif command == "logs":
  if len(sys.argv) < 3:
    cprint("Usage: appscale logs <location to copy logs to>", 'red')
    sys.exit(1)

  try:
    appscale.logs(sys.argv[2])
  except Exception as e:
    cprint(e, 'red')
    sys.exit(1)
elif command == "destroy" or command == "down":
  try:
    appscale.destroy()
  except Exception as e:
    cprint(e, 'red')
    sys.exit(1)
else:
  appscale.help()
