#!/usr/bin/python
# Copyright (C) 2012-2014 Peter Hatina <phatina@redhat.com>
#
# 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 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 sys

from lmi.shell import LMIConsole
from lmi.shell import LMIShellOptions
from lmi.shell.LMIShellLogger import lmi_setup_logger

if __name__ == "__main__":
    options = LMIShellOptions(sys.argv)

    # Setup logging
    lmi_setup_logger(options.log)

    console = LMIConsole(options.cwd_first_in_path)
    console.set_verify_server_certificate(options.verify_server_cert)
    if options.interactive:
        console.interact()
    else:
        script_name = options.script_name
        script_argv = options.script_argv
        interactive = options.interact
        exit_code = console.interpret(
            script_name,
            script_argv,
            interactive=interactive)
        if interactive:
            console.interact(console.locals)
        else:
            sys.exit(exit_code)
    sys.exit(0)
