#!/usr/bin/env python3
#
# Kenozooid - dive planning and analysis toolbox.
#
# Copyright (C) 2009-2011 by Artur Wroblewski <wrobell@pld-linux.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import argparse
import logging
import sys

from kenozooid.cli import ArgumentError, add_commands
from kenozooid.component import query, params, inject
import kenozooid.cli.calc
import kenozooid.cli.da
import kenozooid.cli.dc
import kenozooid.cli.logbook

# add common options to command line parser
parser = argparse.ArgumentParser(
        description='Kenozooid {0}.'.format(kenozooid.__version__))
parser.add_argument('-v', '--verbose',
        action='store_true', dest='verbose', default=False,
        help='explain what is being done')
add_commands(parser, title='Kenozooid commands')
args = parser.parse_args()

# configure basic logger
logging.basicConfig()
logging.Logger.manager.loggerDict.clear()
log = logging.getLogger()
log.setLevel(logging.INFO)

if args.verbose:
    logging.root.setLevel(logging.DEBUG)

# import modules implementing supported drivers
# todo: support dynamic import of third party drivers
from kenozooid.driver import DeviceError
import kenozooid.driver.dummy
import kenozooid.driver.ostc
import kenozooid.driver.su

# execute the command line module
try:
    cls = next(query(name=args.cmd))
    cmd = cls()
    cmd(args)
except DeviceError as ex:
    print('kz: {0}'.format(ex), file=sys.stderr)
    sys.exit(3)
except ArgumentError as ex:
    print('kz: {0}'.format(ex), file=sys.stderr)
    sys.exit(2)
except StopIteration:
    parser.print_help()
    sys.exit(2)


# vim: sw=4:et:ai
