def main():
	if len(sys.argv)==1 or '--help' in sys.argv:
		print('usage: ./rusthon.py [python files] [markdown files] [tar file] [--release] [--run=] [--data=]')
		print('		[tar file] is the optional name of the output tar that contains the build')
		print
		print('		source files, transpiled source, and output binaries.')
		print
		print('		--release generates optimized output without extra runtime checks')
		print
		print('		--run is given a list of programs to run, "--run=a.py,b.py"')
		print('		a.py and b.py can be run this way by naming the code blocks in the markdown')
		print('		using the tag syntax "@a.py" on the line before the code block.')
		print
		print('		--data is given a list of directories to include in the build dir and tarfile.')
		print
		print('		--obfuscate produces random names for unicode functions and variables.')
		print
		print('		--literate-unicode enables unicode variables names to be output for the javascript backend.')
		print
		print('		--convert2python=MYOUTPUT strips away static type annotations, to run your script in regular Python.')
		print
		print('		--anaconda run scripts with Anaconda Python, must be installed to ~/anaconda')
		print
		print('		--transparent linux and NW.js only (forces window transparency and disables GPU acceleration)')
		print
		print('		--debug-inter print intermediate translation code and then exit.')
		print
		print('		note: when using regular python files (.py) as input instead of markdown (.md)')
		print('		you can use these extra options to set which backend is used by the transpiler:')
		for backend in '--c++ --javascript --go --rust'.split():
			print('		'+backend)
		return

	modules = new_module()

	save = False
	paths = []
	scripts = []
	markdowns = []
	gen_md = False
	output_tar  = 'rusthon-build.tar'
	output_dir  = None
	output_file = None
	launch = []
	datadirs = []
	j2r = False
	anaconda = False
	convert2py = False

	for arg in sys.argv[1:]:
		if os.path.isdir(arg):
			paths.append(arg)
		elif arg.startswith('--data='):
			datadirs.extend( arg.split('=')[-1].split(',') )
		elif arg.startswith('--run='):
			launch.extend( arg.split('=')[-1].split(',') )
			save = True
		elif arg.startswith('--output='):
			output_file = arg.split('=')[-1]
		elif arg.startswith('--output-dir='):
			output_dir = arg.split('=')[-1]
			if output_dir.startswith('~'):
				output_dir = os.path.expanduser(output_dir)
		elif arg.startswith('--convert2python='):
			convert2py = arg.split('=')[-1]

		elif arg.endswith('.py'):
			scripts.append(arg)
		elif arg.endswith('.md'):
			markdowns.append(arg)
		elif arg.endswith('.tar'):
			output_tar = arg
			save = True
		elif arg =='--generate-markdown':
			gen_md = True
		elif arg == '--tar':
			save = True
		elif arg == '--java2rusthon':
			j2r = True
		elif arg == '--anaconda':
			anaconda = True

	datadirs = [os.path.expanduser(dd) for dd in datadirs]

	if j2r:
		for path in paths:
			m = convert_to_markdown_project(path, java=True, java2rusthon=True)
			raise RuntimeError('TODO: %s'%m)

	if gen_md:
		for path in paths:
			mds = convert_to_markdown_project(path)
			if not output_file:
				raise RuntimeError('%s \n ERROR: no output file given `--output=myproject.md`'%mds)
			elif os.path.isdir(output_file):
				## write as multiple markdowns into directory
				for m in mds:
					if m['name'].count('.')==1:
						mname = m['name'].split('.')[0] + '.md'
					else:
						mname = m['name'] + '.md'
					mpath = os.path.join(output_file, mname)
					print('writing-> %s'%mpath)
					open(mpath, 'wb').write(m['markdown'])
			else:
				if not output_file.endswith('.md'):
					output_file += '.md'
				md = '\n'.join([m['markdown'] for m in mds])
				open(output_file, 'wb').write(md)
			sys.exit()


	if convert2py:
		## strips away rusthon type annotations ##
		if not len(scripts):
			raise RuntimeError('the option --convert2python=myoutput requires an input script')
		if len(scripts)!=1:
			raise RuntimeError('the option --convert2python=myoutput requires a single input script')
		a = typedpython.transform_source(
			open(scripts[0],'rb').read(), 
			strip=True
		)
		open(convert2py, 'wb').write(a)
		sys.exit()

	base_path = None
	singleout = None
	for path in scripts:
		## note: .decode('utf-8') is not required here,
		## should also check the strip to ensure the user has not
		## used unicode strings starting with the `u` prefix,
		## because that will break the translator, because it
		## promotes those strings to unicode objects in the AST (which is written in Python2)
		script = open(path,'rb').read()
		if '--c++' in sys.argv:          script = '#backend:c++\n'+script
		elif '--javascript' in sys.argv: script = '#backend:javascript\n'+script
		elif '--rust' in sys.argv: script = '#backend:rust\n'+script
		elif '--go' in sys.argv:   script = '#backend:go\n'+script
		elif '--dart' in sys.argv: raise RuntimeError('dart backend removed')
		elif '--lua' in sys.argv:  raise RuntimeError('lua backend removed')
		elif '--verilog' in sys.argv: script = '#backend:verilog\n'+script
		else: script = '#backend:javascript\n'+script

		fpath,fname = os.path.split(path)
		tag = fname.split('.')[0]
		singlemod = {'name':'main', 'tag':tag, 'code':script}
		modules['rusthon'].append( singlemod )
		if base_path is None:
			base_path = os.path.split(path)[0]
		if singleout is None and output_file:
			singleout = singlemod

	for path in markdowns:
		import_md( path, modules=modules )
		if base_path is None:
			base_path = os.path.split(path)[0]

	package = build(modules, base_path, datadirs=datadirs )
	if singleout:
		pak = get_first_build_from_package(package)
		if 'source' in pak:
			open(output_file, 'wb').write(pak['source'])
		elif 'code' in pak:
			open(output_file, 'wb').write(pak['code'])
		elif 'script' in pak:
			open(output_file, 'wb').write(pak['script'])
		else:
			raise RuntimeError(pak)
		print('saved output to: %s'%output_file)

	elif not save:
		tmpdir = tempfile.gettempdir()
		## copy jar and other extra libraries files files ##
		for p in datadirs:
			## saves jar and other files like dynamic libraries,
			## needed to do quick testing.
			if '.' in p:
				dpath,dname = os.path.split(p)
				open(os.path.join(tmpdir,dname),'wb').write(open(p,'rb').read())

		for exe in package['executeables']:
			print('running: %s' %exe)
			subprocess.check_call(
				exe,
				cwd=tmpdir ## jvm needs this to find the .class files
			)

		if package['html']:
			for i,page in enumerate(package['html']):
				tname = 'rusthon-webpage%s.html' %i

				if 'name' in page and page['name']:
					tname = page['name']

				tmp = tempfile.gettempdir() + '/' + tname
				if sys.platform=='darwin':  ## force /tmp directory on OSX, makes debugging the output simpler
					tmp = '/tmp/' + tname

				## note in Chrome UTF-8 javascript will fail with this error: 
				## `Unexpected token ILLEGAL` with unicode variables
				## the file must be written as UTF-16.
				## http://stackoverflow.com/questions/22543354/how-well-is-node-js-support-for-unicode
				#open(tmp, 'wb').write( page['code'].encode('utf-8') )
				open(tmp, 'wb').write( page['code'].encode('utf-16') )

				if i<len(package['html'])-1:  ## only launch the last html file.
					pass
				elif sys.platform=='darwin' and not nodewebkit_runnable:  ## hack for OSX
					subprocess.call(['open', tmp])
				elif nodewebkit_runnable:
					## nodewebkit looks for `package.json` in the folder it is given ##
					nwcfg = '{"name":"test", "main":"%s", ' %os.path.split(tmp)[1]
					if '--frameless' in sys.argv:
						nwcfg += '"window":{"width":1200, "height":680, "toolbar":false, "frame":false}}'
					elif '--desktop' in sys.argv:
						## note as_desktop is broken in new NW.js
						## https://github.com/nwjs/nw.js/issues/2833
						nwcfg += '"window":{"width":1200, "height":680, "toolbar":false, "as_desktop":true, "frame":false}}'
					else:
						nwcfg += '"window":{"width":1200, "height":680, "toolbar":false}}'

					open(os.path.join(tmpdir,"package.json"),'wb').write(nwcfg)
					nwcmd = [nodewebkit]
					if '--v8-natives' in sys.argv:
						nwcmd.append('--allow-natives-syntax')
					if sys.platform=='linux2' and '--transparent' in sys.argv:
						nwcmd.extend(['--enable-transparent-visuals', '--disable-gpu'])
					nwcmd.append(tmpdir)
					subprocess.Popen(nwcmd, cwd=tmpdir)
				else:
					webbrowser.open(tmp)

		elif package['javascript']:
			tmpdir = tempfile.gettempdir()
			for pak in package['javascript']:
				fname = pak['name']
				if fname is None:
					fname = 'rusthon-temp.js'
				elif not fname.endswith('.js'):
					fname += '.js'

				fpath = os.path.join(tmpdir, fname)
				open(fpath, 'wb').write( pak['script'] )
				#xxx = pak['script'].decode('utf-8')
				#open(fpath, 'wb').write( xxx.encode('utf-16') )  ## TODO fix nodejs unicode
				#codecs.open(fpath, 'w', 'utf-8').write( pak['script'] )  ## TODO fix nodejs unicode
				if '--v8-natives' in sys.argv:
					subprocess.check_call([nodejs_exe, '--allow-natives-syntax', fpath])
				else:
					subprocess.check_call([nodejs_exe, fpath])

	else:
		save_tar( package, output_tar )
		print('saved build to:')
		print(output_tar)

		if launch:
			tmpdir = output_dir or tempfile.gettempdir()
			tmptar = os.path.join(tmpdir, 'temp.tar')
			open(tmptar, 'wb').write(
				open(output_tar, 'rb').read()
			)
			subprocess.check_call( ['tar', '-xvf', tmptar], cwd=tmpdir )

			run = subprocess.call

			for name in launch:
				if name==launch[-1]:
					run = subprocess.check_call
				else:
					run = subprocess.Popen

				if name.endswith('.py'):
					firstline = open(os.path.join(tmpdir, name), 'rb').readlines()[0]
					python = 'python'
					if firstline.startswith('#!'):
						if 'python3' in firstline:
							python = 'python3'

					if anaconda:
						## assume that the user installed anaconda to the default location ##
						anabin = os.path.expanduser('~/anaconda/bin')
						if not os.path.isdir(anabin):
							raise RuntimeError(anabin %'Anaconda Python not installed to default location: %s')

						run( [os.path.join(anabin,python), name], cwd=tmpdir )

					else:
						run( [python, name], cwd=tmpdir )

				elif name.endswith('.js'):
					run( ['node', name],   cwd=tmpdir, env={'NODE_PATH':'/usr/lib/node_modules/'} )

				elif name.endswith('.nim'):
					run( ['nim', 'compile', '--run', name],   cwd=tmpdir )

				elif name.endswith('.go'):
					run( ['go', 'run', name],   cwd=tmpdir )

				elif name.endswith('.lua'):
					run( ['luajit', name],   cwd=tmpdir )

				elif name.endswith('.dart'):
					dartbin = os.path.expanduser('~/dart-sdk/bin/dart')
					run( [dartbin, name],   cwd=tmpdir )

				elif name.endswith('.html'):
					if sys.platform=='darwin':
						subprocess.call(['open', tmpdir+'/'+name])
					elif nodewebkit_runnable:
						raise RuntimeError('todo nodewebkitxx')
						subprocess.call([nodewebkit, tmp], cwd=tmpdir)
					else:
						webbrowser.open(tmpdir+'/'+name)

				else:
					print 'running: %s' %name
					run( [os.path.join(tmpdir,name)], cwd=tmpdir )
dataset/ETHPy150Open rusthon/Rusthon/rusthon.py/main/swapped operands of `%`