#!/usr/bin/env python

import os
import sys
import xml.dom.minidom as DOM

def migrate_guid (project):
	for node in project.getElementsByTagName ('ProjectTypeGuids'):
		old = node.firstChild.nodeValue
		node.firstChild.nodeValue = old.replace (
			'{948B3504-5B70-4649-8FE4-BDE1FB46EC69}',
			'{42C0BBD9-55CE-4FC1-8D90-A7348ABAFB23}')
		return node.firstChild.nodeValue != old

def migrate_reference (project):
	xammac_added = False
	for ref in project.getElementsByTagName ('Reference'):
		if ref.hasAttribute ('Include'):
			name = ref.attributes['Include'].value.lower ()
			if name == 'monomac' or name.startswith ('monomac ') or name.startswith('monomac,'):
				if not xammac_added:
					ref.attributes['Include'] = 'XamMac'
					while ref.firstChild:
						ref.removeChild (ref.firstChild)
					xammac_added = True
	return xammac_added

def add_linker_options (project):
	options = {
		'EnableCodeSigning': 'True',
		'CodeSigningKey': 'Mac Developer',
		'CreatePackage': 'False',
		'EnablePackageSigning': 'False',
		'PackageSigningKey': 'Developer ID Installer',
		'IncludeMonoRuntime': 'True',
		'UseSGen': 'False',
		'LinkMode': 'Full'
	}

	options_updated = False

	for node in project.getElementsByTagName ('PropertyGroup'):
		if not node.hasAttribute ('Condition'):
			continue
		cond = node.attributes['Condition'].value
		if cond == " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' " or \
			cond == " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ":
			first_append_made = False
			for key, value in options.iteritems ():
				children = node.getElementsByTagName (key)
				if len (children) > 0:
					old = children[0].nodeValue
					children[0].nodeValue = value
					if children[0].nodeValue != old:
						options_updated = True
				else:
					child = project.createElement (key)
					child.appendChild (project.createTextNode (value))
					if first_append_made:
						node.appendChild (project.createTextNode ('    '))
					else:
						node.appendChild (project.createTextNode ('  '))
					node.appendChild (child)
					node.appendChild (project.createTextNode ('\n'))
					first_append_made = True
					options_updated = True
			if first_append_made:
				node.appendChild (project.createTextNode ('  '))


	return options_updated

def migrate_project (path):
	with open (path, 'r') as fp:
		eof_newline = fp.read ().endswith ('\n')
	
	project = DOM.parse (path)

	a = migrate_guid (project)
	b = migrate_reference (project)
	c = add_linker_options (project)

	if not a and not b and not c:
		return

	xml = project.toxml ('utf-8')
	xml = xml.replace ('"utf-8"?>', '"utf-8"?>\n')
	xml = xml.replace ('/>', ' />')

	with open (path, 'w') as fp:
		fp.write (xml)
		if eof_newline:
			fp.write ('\n')

migrate_project (sys.argv[1])
