#!/usr/bin/python
################################################################
#
#              Renumber Notes 
#
################################################################
#
#
import sys
#
import pattern
#
#
if len(sys.argv)< 2:
	raise SystemExit('File not specified.')
filename = sys.argv[1] + '.tex'
try:
	f = open(filename,"r")
except:
	raise SystemExit(filename + ' not found.')
#
# Get numbering lists. 
#
section_number = -1
# 
r = f.readline()
last_section = -1
lines = []
newlines = []
fixlines = []
reflist = []
outlist = []
inlist = []
soutlist = []
sinlist = []
finished_sections = []
newthnum = [] 
reps = []	
while 1:
	propnumm = pattern.propnum.match(r)
	notem =   pattern.note.match(r)
	
	if propnumm or not r:
		if propnumm:
			newthnum = propnumm.group('refnum')
		reps = []	
		if outlist:
			repcheck = outlist[:]
			repcheck.sort()
			for i in range(len(repcheck)-1):
				if repcheck[i] == repcheck[i+1]:
					reps.append(repcheck[i])
		if reps: 
			fixlines = newlines
		elif not outlist:
			fixlines = newlines
		else:
# Process newlines here.
			fixlines = []
			note_num = 1
			for s in newlines:	
				notem = pattern.note.match(s)
				if notem:
					oldnum = notem.group(1)
					newnum = "%d" % note_num
					s = s.replace(oldnum,newnum,1)
					note_num = note_num + 1
				refpatm = pattern.noteref.match(s)
				if refpatm:
					newr = s[refpatm.end(3):]
				else:
					newr = ''
				while refpatm:
					ref = refpatm.group(2)
					index = -1
					if ref in outlist:
						index = outlist.index(ref)
					if index > -1: 
						newr =  inlist[index] + refpatm.group(3) + newr
					else:
						newr = ref+ refpatm.group(3) +  newr 
					starttwo = refpatm.start(2)
					s = s[:starttwo]
					refpatm = pattern.noteref.match(s)
				newr = s + newr 
				fixlines.append(newr)
############################################################
		lines = lines + fixlines
		if reps:
			print('Repeated notes in theorem ' + oldthnum + ':',reps)
		elif inlist:
			print("Renoting theorem ", oldthnum)
			print(inlist)
			print(outlist)
		newlines = []
		inlist = []
		outlist = []
		ncount = 0
	if not r:
		break
	if notem:
		num = notem.group(1)
		ncount = ncount + 1	
		new_ref ="%d" % ncount 
		if new_ref != num :
			outlist.append(num)
			inlist.append(new_ref)
	newlines.append(r)
	oldthnum = newthnum 
	r = f.readline()
f.close()
#
f = open(filename,"w")
f.writelines(lines)
f.close()
