#!/usr/bin/python2
import os, fnmatch, sys, operator

# locate xorg
XORG = "/etc/X11/xorg.conf"

if len(sys.argv) > 1:
	XORG = sys.argv[1]

XORGOUT = XORG+".promethean"
XORGBACKUP = XORG+".promethean.backup.conf"

infile = open(XORG)
outfile = open(XORGOUT,'w')

inputdevice = -1
serverlayout = -1
identifier = -1
endsection = -1
idname = ''
id=''
serverlayoutlines = []
fileok = False
promethean = False

try:

	for line in infile:
		# comment: just ignore
		if (line[0] == '#'):
			outfile.write(line)
			continue

		# error in xorg from ubuntu 6.10 remastered with reconstructor: there's a -e in xorg...
		if (line[0] == '-'):
			continue

		# first read a whole section, that is everything between
		# Section "..." and "EndSection"
		# then we parse the section for
		# the following possible cases:

		# serverlayout:
		# need to find the mouse that is already configured, and
		# make sure "CorePointer" is present
		# we also need to add a pen input device
		# we store the whole section because the order is not guaranteed, we may
		# get identifier after driver, etc..
		sectionlines = []

		id = ''

		endsection = line.lower().find("endsection")
		section = line.lower().find("section")

		if (section == 0): # beginning of section
			print "Processing section : " + line[0:-1]

			sectionlines += [line]

			for line in infile:
				sectionlines += [line]
				endsection = line.lower().find("endsection")
				promethean = line.lower().find("promethean") >=0
				if promethean:
					break
				if (endsection >= 0):
					break

			if promethean:
				print "\nYour xorg.conf has already been modified. You may need to modify it manually if required."
				break

			# first line is section name
			sectionlinesout = []
			sectionline = sectionlines[0]

			inputdevice = sectionline.lower().find("inputdevice")
			serverlayout = sectionline.lower().find("serverlayout")

			if serverlayout >= 0: # add the new input device
				sectionlinesout += ["# >> ADDED BY PROMETHEAN INSTALLER\n"];
				sectionlinesout += ["Section \"InputDevice\"\n","\tIdentifier\t\"activpen\"\n"];
				sectionlinesout += ["\tDriver \"promethean\"\n","\tOption\t\"Device\" \"/dev/input/activdevices\"\n"]
				sectionlinesout += ["\tOption\t\"Type\"	\"stylus\"\n\tOption\t\"Mode\"	\"Absolute\"\n"]
				sectionlinesout += ["\tOption\t\"Cursor\" \"stylus\"\n","\tOption\t\"USB\" \"on\"\n"]
				sectionlinesout += ["\tOption\t\"debuglevel\" \"5\"\n","\tOption\t\"Vendor\" \"promethean\"\n"]
				sectionlinesout += ["EndSection\n"]
				sectionlinesout += ["# END ADDED BY PROMETHEAN INSTALLER <<\n\n"];

			sectionlinesout += [sectionlines[0]]

			if (serverlayout >=0):
				# Section "ServerLayout"

				# add activpen to devices
				serverlayoutlines = ["# >> ADDED BY PROMETHEAN INSTALLER\n"];
				serverlayoutlines += ["Section \"InputDevice\"\n","\tIdentifier\t\"activpen\"\n"];
				serverlayoutlines += ["\tDriver \"promethean\"\n","\tOption\t\"Device\" \"/dev/input/activdevices\"\n"]
				serverlayoutlines += ["\tOption\t\"Type\"	\"stylus\"\n\tOption\t\"Mode\"	\"Absolute\"\n"]
				serverlayoutlines += ["\tOption\t\"Cursor\" \"stylus\"\n","\tOption\t\"USB\" \"on\"\n"]
				serverlayoutlines += ["\tOption\t\"debuglevel\" \"5\"\n","\tOption\t\"Vendor\" \"promethean\"\n"]
				serverlayoutlines += ["\tOption\t\"SendCoreEvents\" \"true\"\n"]
				serverlayoutlines += ["EndSection\n"]
				serverlayoutlines += ["# END ADDED BY PROMETHEAN INSTALLER <<\n\n"];
				serverlayoutlines += sectionlines
				sectionlinesout = []

			else: # not a section we want, just copy the whole lot
				sectionlinesout = sectionlines

			# dump the new section to file
			for sectionline in sectionlinesout:
				outfile.write(sectionline)
		else:
			outfile.write(line)

	sectionlinesout = []
	foundlayout = False
	if len(serverlayoutlines):
		for sectionline in serverlayoutlines:
			added = False
			if (not foundlayout):
				foundlayout = sectionline.lower().find("serverlayout") >= 0

			if sectionline[0] != '#' and foundlayout:
				# first, have we found the mouse device ?
				if sectionline.lower().find("\"activpen\"") >= 0: # skip duplicate "activpen"
					added=True
				elif sectionline.lower().find("endsection") >= 0:
					# add the promethean device
					sectionlinesout += ["\tInputDevice\t\"activpen\"\t\"SendCoreEvents\"\n"]
			if not added:
				sectionlinesout += sectionline
		for sectionline in sectionlinesout:
			outfile.write(sectionline)

	fileok = not promethean


finally:
	infile.close()
	outfile.close()

if fileok:
	print "Backing up "+XORG+" into "+XORGBACKUP
	infile = open(XORG)
	outfile = open(XORGBACKUP,'w')
	try:
		for line in infile:
			outfile.write(line)
	finally:
		infile.close()
		outfile.close()

	infile = open(XORGOUT)
	outfile = open(XORG,'w')
	try:
		for line in infile:
			outfile.write(line)
	finally:
		infile.close()
		outfile.close()

