#!/usr/bin/python
import sys,os,re
#
tex_path = os.getenv('TEXPATH') or ''
#
#

if len(sys.argv) == 1:
	print "Usage: checkall <source file> [regular expression]"
	sys.exit(1)

try:
	f = open(tex_path + sys.argv[1] + ".pfs" ,"r")
	line_list = f.readlines()
	f.close()
except:
	print "Error reading file: ",tex_path + sys.argv[1] + ".pfs" 
	sys.exit(1)
	
if len(sys.argv) == 3:
	try:
		selectm = re.compile(sys.argv[2])
	except:
		print "Bad selection pattern"
		sys.exit(1)
else:
	selectm = ''

tried = []
checked = []
for yn in line_list:
	y = yn.strip()
	if selectm:
		if selectm.match(y):
			tried.append(y) 
		else:
			continue
#	z = os.popen(" check " + sys.argv[1] + " " + y + "|grep 'Proof checked'").read()

	if os.name == 'nt':
		check_output = os.popen("check.py " + sys.argv[1] + " " + y).readlines()
	else:
		check_output = os.popen("check " + sys.argv[1] + " " + y).readlines()
	for eachline in check_output:
		if "Proof checked" in eachline:
			checked.append(y)
			print y, " checked"
			break
	else:
		print y

#	if z.find("Proof checked") !=-1:
#		checked.append(y)
#		print y," checked"
#	else:
#		print y

n_checked = len(checked)
if n_checked == 1:
	print n_checked, "proof checked."
else:
	print n_checked, "proofs checked."
g = open("checked","w")
for y in checked:
	g.write(y + '\n')
g.close()
print "\a"
