#!/usr/bin/python

from xml.dom.minidom import parse;
from xml import xpath;
import xml.dom.ext;
import sys;

vert_text = 'GTK_ORIENTATION_VERTICAL'

def force_orientation_vertical_on_class(classname, dom):
	# Find all vboxes.
	vbox_nodes = xpath.Evaluate(
		'descendant::widget[@class="' + classname + '"]',
		dom.documentElement)
	for v in vbox_nodes:
		v_id = v.attributes['id'].nodeValue

		# If it doesn't have an orient_prop, add it.
		orient_props = xpath.Evaluate('child::property[@name="orientation"]', v)
		if orient_props is None or len(orient_props) < 1:
			new_orient = dom.createElement('property')
			new_orient.setAttribute('name', 'orientation')

			new_text = dom.createTextNode(vert_text)

			new_orient.appendChild(new_text)

			v.insertBefore(new_orient, v.firstChild)

			print 'Appended new orientation node to ' + str(v_id) + '.'
		else:
			# Fix all horizontal settings.
			orient_texts = xpath.Evaluate(
				'child::property[@name="orientation"]/child::text()',
				v)
			for text in orient_texts:
				if text.data != vert_text:
					old_data = text.data
					text.data = 'GTK_ORIENTATION_VERTICAL'
					print 'Fixed orientation on ' + str(v_id) + ';', old_data, '->', text.data
				


for fname in sys.argv[1:]:
	print fname
	dom = parse(fname)


	force_orientation_vertical_on_class('GtkVBox', dom)
	force_orientation_vertical_on_class('GtkVSeparator', dom)

	

	out_fname = fname + '.fixed'
	file_obj = open(out_fname, 'w')
	xml.dom.ext.Print(dom, file_obj)
	file_obj.close()

