#!/usr/bin/python

try:
    import lib as larch
    userbanner = '=== DEBUG Version ==='
    
except ImportError:
    import larch
    userbanner = None
import sys, os
from optparse import OptionParser

usage = "usage: %prog [options] file"
parser = OptionParser(usage=usage, prog="larch",
                      version="%s" % larch.__version__)
parser.add_option("-q", "--quiet", dest="quiet", action="store_true",
                  default=False, help="set quiet mode, default = False")

parser.add_option("-d", "--debug", dest="debug", action="store_true",
                  default=False, help="set debug mode, default = False")

parser.add_option("-e", "--exec", dest="exec_script_only", action="store_true",
                  default=False, help="execute script only, default = False")

(options, args) = parser.parse_args()

t = larch.shell(debug=options.debug,userbanner=userbanner)

if len(args)>0:
    for s in args:
        if s.endswith('.py'):
            t.default("import %s" %  s[:-3])
        else:
            t.input.interactive = False
            finp = open(s,'r')
            for itxt, txt in enumerate(finp.readlines()):
                t.input.put(txt[:-1], lineno=itxt, filename=s)
            finp.close()
            t.larch_execute('')
            t.input.interactive = True
            
if not options.exec_script_only:
       t.cmdloop()

