#!/usr/bin/python
#   Copyright (C) 2013 Canonical Ltd.
#
#   Author: Scott Moser <scott.moser@canonical.com>
#
#   Curtin is free software: you can redistribute it and/or modify it under
#   the terms of the GNU Affero General Public License as published by the
#   Free Software Foundation, either version 3 of the License, or (at your
#   option) any later version.
#
#   Curtin is distributed in the hope that it will be useful, but WITHOUT ANY
#   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#   FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
#   more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with Curtin.  If not, see <http://www.gnu.org/licenses/>.

import argparse
import os
import sys
import traceback

try:
    from curtin import log
    from curtin import util
except ImportError as e:
    _tdir = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
    if (os.path.isdir(os.path.join(_tdir, 'curtin')) and
            _tdir not in sys.path):
        sys.path.append(_tdir)
    else:
        raise
    from curtin import log
    from curtin import util

SUB_COMMAND_MODULES = ['block-meta', 'curthooks', 'extract', 'hook',
                       'in-target', 'install', 'net-meta', 'pack']


def add_subcmd(subparser, subcmd):
    modname = subcmd.replace("-", "_")
    subcmd_full = "curtin.commands.%s" % modname
    __import__(subcmd_full)
    try:
        popfunc = getattr(sys.modules[subcmd_full], 'POPULATE_SUBCMD')
    except AttributeError:
        raise AttributeError("No 'POPULATE_SUBCMD' in %s" % subcmd_full)

    popfunc(subparser.add_parser(subcmd))


def main(args=None):
    if args is None:
        args = sys.argv

    parser = argparse.ArgumentParser()

    stacktrace = (os.environ.get('CURTIN_STACKTRACE', "0").lower()
                  not in ("0", "false", ""))
    parser.add_argument('--showtrace', action='store_true', default=stacktrace)
    parser.add_argument('--verbose', '-v', action='count', default=0)
    parser.add_argument('--log-file', default=sys.stderr,
                        type=argparse.FileType('w'))

    subps = parser.add_subparsers(dest="subcmd")
    for subcmd in SUB_COMMAND_MODULES:
        add_subcmd(subps, subcmd)
    args = parser.parse_args(sys.argv[1:])

    # if user gave cmdline arguments, then lets set environ so subsequent
    # curtin calls get stacktraces.
    if args.showtrace and not stacktrace:
        os.environ['CURTIN_STACKTRACE'] = "1"

    if not getattr(args, 'func', None):
        # http://bugs.python.org/issue16308
        parser.print_help()
        sys.exit(1)

    log.basicConfig(stream=args.log_file, verbosity=args.verbose)

    paths = util.get_curtin_paths(curtin_exe=os.path.realpath(__file__))

    os.environ['PATH'] = ':'.join((paths['helpers'], os.environ['PATH'],))

    try:
        sys.exit(args.func(args))
    except Exception as e:
        if args.showtrace:
            traceback.print_exc()
        sys.stderr.write("%s\n" % e)
        sys.exit(3)


if __name__ == '__main__':
    main()

# vi: ts=4 expandtab syntax=python
