#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK

"""
Keymaker: Lightweight SSH key management on AWS EC2

See https://github.com/kislyuk/keymaker for documentation.
"""

from __future__ import absolute_import, division, print_function, unicode_literals

import os, sys, argparse, logging

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from keymaker.printing import KEYMAKER_LOGO

from keymaker import configure, upload, install, get_authorized_keys, get_uid, logger

parser = argparse.ArgumentParser(description=__doc__.replace("Keymaker:", KEYMAKER_LOGO()),
                                 formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("--debug", action="store_true")
subparsers = parser.add_subparsers(dest="command")

parser_configure = subparsers.add_parser("configure",
                                         help="Perform one-time setup on an AWS account. Run this command for each AWS account that will use Keymaker to authenticate users.",
                                         description="TODO")
#parser_configure.add_argument("--yes",
#                          help='Assume a "yes" response to all prompts. Required if running non-interactively',
#                          action="store_true")

parser_upload = subparsers.add_parser("upload",
                                      help="Upload public SSH key for a user. Run this command for each user who will be accessing EC2 hosts.",
                                      description="By default, an SSH public key is extracted from the output of ``ssh-add``. You can also specify an existing SSH key file with --identity.")
parser_upload.add_argument("--identity", help="An existing SSH key filename to get the public key from. Commonly stored in ~/.ssh with .pub or .pem extensions.")

parser_install = subparsers.add_parser("install",
                                       help="Install Keymaker SSH auth module. Run this command on each EC2 host that will use Keymaker to authenticate users.",
                                       description="TODO")
parser_install.add_argument("--user", help="User that the SSH auth module will run as. If automatic user and group creation is enabled, this user must have the privileges to do so.")
# action.completer = ...

parser_get_authorized_keys = subparsers.add_parser(
    "get_authorized_keys",
    help="Get authorized keys for a given IAM/SSH user. This command is used by sshd as AuthorizedKeysCommand when processing authentication requests."
)
parser_get_authorized_keys.add_argument("user", help="User to get authorized keys for")

parser_get_uid = subparsers.add_parser(
    "get_uid",
    help="Get canonical UID for a given IAM/SSH user. This command is used internally by Keymaker when provisioning user accounts."
)
parser_get_uid.add_argument("user", help="User to get UID for")

if len(sys.argv) == 1:
    parser.print_help()
    sys.exit(1)

args = parser.parse_args()
if args.debug:
    logger.setLevel(logging.DEBUG)
locals()[args.command](args)
