import os
import sys

def main():
	# NOTE: Developed with Python 3.4
	# Validate the input
	if len(sys.argv) < 2:
		print('\nERROR: Not enough arguments provided!')
		print('\nUSAGE: total.py <output directory>\n')
		return -1

	path = sys.argv[1]
	
	# Iterate over each output file
	total_counts = {}
	for filename in os.listdir(path):
		full_path = os.path.join(path, filename)
		print(full_path)

		# Iterate over the contents of each output file and parse the index 
		# and counts
		with open(full_path, 'r') as output_file:
			for line in output_file:
				if not line or line.startswith('Parsing'):
					continue

				index, count = line.split(' ')
				index = int(index)
				count = int(count)

				if index not in total_counts:
					total_counts[index] = count
				else:
					total_counts[index] += count

	# After building the histogram print out the results
	print('Usage: {:d}'.format(len(total_counts)))
	for index in sorted(total_counts.keys()):
		print(index, total_counts[index])

if __name__ == '__main__':
	sys.exit(main())