#!/usr/bin/python

# 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 2 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# See the COPYING file for license information.
#
# Copyright (c) 2012,2013 peo3 <peo314159265@gmail.com>
import sys

import cgutils.command
import cgutils.commands


def main():
    COMMANDS = cgutils.commands.__all__

    parser = cgutils.command.Command.parser
    subparsers = parser.add_subparsers(dest='subcmd_name')

    # Set subparsers of all subcommands
    for cmd in COMMANDS:
        mod = __import__('cgutils.commands' + '.' + cmd, fromlist=[cmd,])
        mod.Command.add_subparser(subparsers)

    # Run to know subcmd_name
    args = parser.parse_args()
    if args.debug:
        print(args)

    mod = __import__('cgutils.commands' + '.' + args.subcmd_name, fromlist=[args.subcmd_name,])
    cmd = mod.Command()
    cmd.run()


if __name__ == '__main__':
    main()
