def main(argv):
	tier = argv[2]
	if tier.endswith('current'):
		tier=tier[:-7]
	if tier.startswith('pokebank'):
		tier = tier[8:-4]
	if tier.startswith('oras'):
		tier = tier[4:]
	if tier == 'capbeta':
		tier = 'cap'
	if tier == 'vgc2014beta':
		tier = 'vgc2014'
	if tier.startswith('xybattlespot') and tier.endswith('beta'):
		tier = tier[:-4]
	if tier in ['battlespotdoubles', 'battlespotdoublesvgc2015']:
		tier = 'vgc2015'
	if tier == 'smogondoubles':
		tier = 'doublesou'
	if tier == 'smogondoublesubers':
		tier = 'doublesubers'
	if tier == 'smogondoublesuu':
		tier = 'doublesuu'
	#elif tier[:8]=='seasonal':
	#	tier='seasonal'

	ratings = None
	if len(argv) > 4:
		if argv[3] == '-redoRatings':
			try:
				ratings = json.loads(open(argv[4]).readline())
			except:
				ratings = {}
			print ratings

	outname = "Raw/"+tier#+".txt"
	d = os.path.dirname(outname)
	if not os.path.exists(d):
		os.makedirs(d)
	writeme=[]
	movesets={}
	count=0
	for filename in os.listdir(argv[1]):
		#print filename
		x = LogReader(argv[1]+'/'+filename,tier,movesets,ratings)
		if x:
			writeme.append(x)
			count += 1
			
			if count % 10000 == 0:
				outname = "Raw/"+tier#+".txt"
				outfile=gzip.open(outname,'ab')
				outfile.write(json.dumps(writeme)+'\n')
				outfile.close()

				#write to moveset file
				for species in movesets.keys():
					outname = "Raw/moveset/"+tier+"/"+species#+".txt"
					d = os.path.dirname(outname)
					if not os.path.exists(d):
						os.makedirs(d)
					msfile=gzip.open(outname,'ab')		
					msfile.write(json.dumps(movesets[species]))
					msfile.close()

				writeme = []
				movesets={}
	if writeme:
		outname = "Raw/"+tier#+".txt"
		outfile=gzip.open(outname,'ab')
		outfile.write(json.dumps(writeme)+'\n')
		outfile.close()

		#write to moveset file
		for species in movesets.keys():
			outname = "Raw/moveset/"+tier+"/"+species#+".txt"
			d = os.path.dirname(outname)
			if not os.path.exists(d):
				os.makedirs(d)
			msfile=gzip.open(outname,'ab')		
			msfile.write(json.dumps(movesets[species]))
			msfile.close()

	if ratings != None:
		for player in ratings.keys():
			Glicko.newRatingPeriod(ratings[player])
		ratingfile=open(argv[4],'w+')
		ratingfile.write(json.dumps(ratings))
		ratingfile.close()