#!/usr/bin/python3
#
# Copyright (C) 2013-2015   Ian Firns   <firnsy@kororaproject.org>
#                           Chris Smart <csmart@kororaproject.org>
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#
# PYTHON_ARGCOMPLETE_OK

import dnf
import logging
import signal
import sys

logger = logging.getLogger('canvas')

# TODO: REMOVE
sys.path.append('./lib')

import canvas.config
#import canvas.cli.commands
import canvas.cli.commands.config
import canvas.cli.commands.machine
import canvas.cli.commands.object
import canvas.cli.commands.package
import canvas.cli.commands.repo
import canvas.cli.commands.template
import canvas.cli.commands.store


def signal_handler(signal, frame):
    print()
    sys.exit(1)


if __name__ == '__main__':
    # trap ctrl+c, it's polish
    signal.signal(signal.SIGINT, signal_handler)

    # read config file
    config = canvas.config.Config()

    parsers, args, args_extra = canvas.cli.commands.parseCommandLine(config)
    if args.command == None:
        parsers.main.print_help()
        sys.exit(1)
    cli = None

    # processCommandLine
    if args.command == 'config':
        cli = canvas.cli.commands.config.ConfigCommand()

    elif args.command == 'template':
        cli = canvas.cli.commands.template.TemplateCommand()

    elif args.command == 'object':
        cli = canvas.cli.commands.object.ObjectCommand()

    elif args.command == 'package':
        cli = canvas.cli.commands.package.PackageCommand()

    elif args.command == 'repo':
        cli = canvas.cli.commands.repo.RepoCommand()

    elif args.command == 'machine':
        cli = canvas.cli.commands.machine.MachineCommand()

    elif args.command == 'store':
        cli = canvas.cli.commands.store.StoreCommand()

    cli.configure(config, args, args_extra, parsers)

    try:
        sys.exit(cli.run())
    except NotImplementedError:
        print(
            "Command '%s', action '%s' not implemented yet" % (
                args.command,
                args.action
            ),
            file=sys.stderr
        )
