#! /usr/bin/env python
#
# Copyright (c) 2015 Red Hat, Inc.
#
# This software is licensed to you under the GNU Lesser General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (LGPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of LGPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/lgpl-2.0.txt.
#
# Jeff Ortel <jortel@redhat.com>
#

import os
import sys

from gofer import utf8
from gofer.tools import mgt, call


COMMAND = {
    'mgt': mgt,
    'rmi': call
}


def usage():
    for name, cmd in sorted(COMMAND.items()):
        program = os.path.basename(sys.argv[0])
        parser = cmd.parser
        parser.set_usage('%s %s %s' % (program, name, cmd.USAGE))
        parser.print_help()
        print '\n'


if __name__ == '__main__':
    if len(sys.argv) < 2:
        usage()
        sys.exit(0)

    # get command
    try:
        cmd = COMMAND[sys.argv[1]]
        del sys.argv[1]
    except KeyError:
        usage()
        sys.exit(1)

    # run command
    try:
        cmd.main()
        sys.exit(0)
    except Exception, e:
        print utf8(e)
        sys.exit(1)
