#@+leo-ver=4
#@+node:@file ___script_leo.py
"""Run Leo non-interactively if sys.arg contains -script x.py."""

from leoPlugins import *
from leoGlobals import *

import leoFrame,leoGui
import sys

if 0: # Register the handlers...

	#@	@+others
	#@+node:getScript
	def getScript ():
		
		name = None ; i = 1 # Skip the dummy first arg.
		while i + 1 < len(sys.argv):
			arg = sys.argv[i].strip().lower()
			if arg in ("--script","--script"):
				name = sys.argv[i+1].strip() ; break
			i += 1
	
		if not name: return None	
		name = os_path_join(app.loadDir,name)
		try:
			f = None
			try:
				f = open(name,'r')
				script = f.read()
				# trace("script",script)
			except IOError:
				es("can not open script file: " + name, color="red")
				script = None
		finally:
			if f: f.close()
			return script
	#@nonl
	#@-node:getScript
	#@-others

	if 0: # Testing
		sys.argv.append("--script")
		sys.argv.append(r"..\test\batchScript.py")

	script = getScript()
	if script:
		if app.gui is None:
			#@			<< create null gui >>
			#@+node:<< create null gui >>
			app.gui = leoGui.nullGui("nullGui")
			app.root = app.gui.createRootWindow()
			app.gui.finishCreate()
			#@-node:<< create null gui >>
			#@nl
			app.gui.setScript(script)
			__version__ = "0.1" # Set version for the plugin handler.
			plugin_signon(__name__)
		else:
			s = "Can't run Leo in batch mode: previous gui installed"
			print s ; es(s,color="red")
			
#@nonl
#@-node:@file ___script_leo.py
#@-leo
