#!/usr/bin/python
# this will hack a csproj file to set the conditional compilation symbols to EDITION_OSS
	
import sys
import re
	
	
def hack_file(path):
	lines= open(path, "r").readlines()
	nlines= []
	changed= False
	indebuggroup= False
	
	for line in lines:
		l= line.strip()
		r= re.match("<PropertyGroup.*'Debug.*>", l)
		if r:
			indebuggroup= True
			
		if l == "</PropertyGroup>" and indebuggroup:
			indebuggroup= False
	
		if indebuggroup:
			r= re.match("<DefineConstants>(.*)</DefineConstants>", l)
			if r:
				defs= r.groups()[0]
				ndefs= defs.replace("COMMERCIAL_CODE", "")
				ndefs= ndefs.replace("EDITION_SE", "EDITION_OSS")
				if defs != ndefs:
					changed= True
					line= re.sub(">(.*)<", ">"+ndefs+"<", line)
		nlines.append(line)
	
	if changed:
		print "Fixing", path
		open(path, "w+").write("".join(nlines))
	else:
		print "No Changes in", path

for f in sys.argv:
	hack_file(f)

