#!/usr/local/bin/python2.6

from core.ui.consoleUi.consoleUi import consoleUi
from core.ui.webUi.webUi import webUi
import core.controllers.outputManager as om
import getopt,sys

om.out.setOutputPlugins( ['console'] )

def usage():
	om.out.information('w3af - Web Application Attack and Audit Framework')
	om.out.information('')
	om.out.information('Options:')
	om.out.information('	-h			Print this help message.')
	om.out.information('	-s			Script file to execute.')
	om.out.information('')
	om.out.information('http://w3af.sourceforge.net/')

def main():
	try:
		opts, args = getopt.getopt(sys.argv[1:], "hs:w", ["help", "script", "webUi"])
	except getopt.GetoptError:
		# print help information and exit:
		usage()
		sys.exit(2)
	scriptFile = None
	web = False
	for o, a in opts:
		if o in ("-s", "--script"):
			scriptFile = a
		if o in ("-w", "--webUi"):
			# Go web
			web = True
		if o in ("-h", "--help"):
			usage()
			sys.exit()
	
	if web:
		# go web!
		om.out.setOutputPlugins( ['console', 'webOutput'] )
		console = webUi()
		om.out.console('Starting w3af Web Interface at: http://localhost:40000/')
		console.sh()
	else:
		# console
		if scriptFile != None:
			try:
				fd = open( scriptFile )
			except:
				om.out.error('Failed to open file : ' + scriptFile )
				sys.exit(2)
			else:
				commandsToRun = []
				for line in fd:
					line = line.strip()
					if line != '' and line[0] != '#':	# if not a comment..
						commandsToRun.append( line )
				fd.close()
				console = consoleUi(commandsToRun)
		else:
			console = consoleUi()
		console.sh()

if __name__ == "__main__":
	main()

