#!/usr/bin/env python

# Copyright (c) 2000-2001 LOGILAB S.A. (Paris, FRANCE).
#
# 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, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

__revision__ = "$Id: Horn.py,v 1.76 2001/11/20 14:16:00 syt Exp $"

# make sure that NARVAL_HOME exist and is populated
from bibal import *

# HornMain #######################################################################

class HornMain:
    """
    HornMain is the main start of Horn
    """

    _single = None

    def __init__(self):
        if HornMain._single:
            raise HornMain._single

        HornMain._single = self

    # start CLI mode

    def startcli(self,script=None):
        # launch cli
        from gui.HornCli import HornCli
        self._cli = HornCli()
        if script:
            self._cli.set_script(script)
        self._cli.thread.start()

    # start GUI mode

    def startgui(self):
        # launch gui
        from gui.ConnectionWindow import ConnectionWindow
        win = ConnectionWindow()
        win.show()
        from gtk import mainloop
        mainloop()

# MAIN #########################################################################
USAGE = """Usage: Horn [options]
   --home DIR   : overwrite NARVAL_HOME environment variable with DIR
   --exec FILE  : execute all the instructions listed in FILE
   --gui        : start the Graphical User Interface version of Horn (default)
   --cli        : start the Command-Line Interface version of Horn
   --help       : print this help message
   """


def Run(SysArgs):
    # process options
    import getopt, sys
    short_list = ''
    long_list = ['home=','exec=','exec-file=','cli','gui','help']
    optlist, args = [], []
    try:
        optlist, args = getopt.getopt(SysArgs[1:], short_list, long_list)
    except getopt.error,e :
        print 'Warning',e
        print USAGE
        sys.exit(1)
    horn = HornMain()

    to_start = 'GUI'
    script_lines = []
    
    for opt, val in optlist:
        if opt == '--cli' :
            to_start = 'CLI'
        elif opt == '--gui' :
            to_start = 'GUI'
        elif opt == '--exec-file' :
            to_start = 'CLI'
            script_lines = open(val).readlines()
        elif opt == '--exec' :
            import string
            to_start = 'CLI'
            if val == '-' :
                script_lines = sys.stdin.readlines()
            else :
                script_lines = string.split(val,'\n')
        elif opt == '--home':
            from os.path import abspath
            from narval import config
            config.set_home(abspath(val))
        else:
            print USAGE
            sys.exit(1)

    import narval.gui
    narval.gui.init_gettext()
    
    if to_start == 'CLI':
        horn.startcli(script_lines)
    else:
        horn.startgui()
    

if __name__ == "__main__" :
    Run(sys.argv)


