import argparse, os, sys, glob, json

ARCH = "archives/"


def run(l):

	if not l.startswith(ARCH):
		l = os.path.join(ARCH, l)

	if not os.path.isdir(l):
		sys.exit(l + ' is not a valid archive. Aborting.')

	files = [f for f in glob.glob(os.path.join(l, "*.json"))]

	total_chars = 0
	total_words = 0
	total_lines = 0
	for f in files:		
		with open(f) as fp:
			d = json.load(fp)
			# print(d['name'])
			chars = 0
			words = 0
			lines = 0
			for t in d['threads']:
				chars += len(t["content"])
				words += len(t["content"].split())
				lines += len(t["content"].split('\n'))
			# print("	chars: " + str(chars))
			# print("	words: " + str(words))
			# print("	lines: " + str(lines))
			total_chars += chars
			total_words += words
			total_lines += lines

	print("\n\n" + l)
	print("Total chars: " + str(total_chars))
	print("Total words: " + str(total_words))
	print("Total lines: " + str(total_lines))


if __name__ == "__main__":

	p = argparse.ArgumentParser(description='Mailinglists are dead. Long live mailinglists!')
	p.add_argument('list', metavar="list", help="mailinglist to ana", nargs="+")

	args = p.parse_args()

	if not args.list:
		sys.exit('No list(s). Aborting.')

	for l in args.list:
		run(l)

	print("\n\n . . . . ")
