# -*- coding: utf-8 -*-

# ///////////////////////////////////////////////////////////////////////////////
# // Copyright (C) 2013 Tim Leonard
# ///////////////////////////////////////////////////////////////////////////////	

import os
import datetime
import platform
from optparse import OptionParser
from Libs.Process import *

# w00t, time to actually parse and update the version information.
def GenerateChangelog(output_file):

	changelog = """=================================================================================
 ZombieGrinder Changelog
=================================================================================
 This file lists all changes made to the game between different
 versions. This file is automatically generated by commit 
 messages on our source control server, so you may see some
 odd formatting or non-relevant entries. Apologies :)
=================================================================================
"""

	# TODO: Link to VersionHelper stuff
	REVISIONS_PER_BUILD = 100

	# Step through all changelists.
	print("Going through changelists ...")

	cmd_line = "p4.exe changes -l -s submitted" 		
	proc = Process("C:\\Program Files\\Perforce\\p4.exe", cmd_line, os.getcwd(), False, True);
	proc.Start();

	bInChangelist = False
	description = ""
	changelog_pending = ""
	changelog_pending_version = ""
	bFoundVersion = False
	
	for line in proc.Get_StdOut():
		#print("Line:"+line)
		if (line[:6] == "Change"):		
			description = description.rstrip("\r\n").lstrip("\r\n")			
			#print("Found new changelist")
			
			# Found end of descrpition
			if (description[:18] == "[Changelog] Start"):
				break
				
			# Find changelist log.
			changelogIndex = description.find("[Changelog]")
			if (changelogIndex >= 0):
				print("Found new changelog entry.")

				if (description[12:12+8] == "Version:"):
					bFoundVersion = True
					version = int(description[12+8:])
											
					#print("Found new version: "+str(version))
					
					if (changelog_pending != ""):					
						changelog += "\n---------------------------------------------------------------------------------\n"
						changelog += "  Version "+changelog_pending_version+"\n"
						changelog += "---------------------------------------------------------------------------------\n"		
						changelog += changelog_pending;
					
					changelog_pending = ""
					changelog_pending_version = str(1)+"."+str(0)+"."+str(version / REVISIONS_PER_BUILD)+"."+str(version % REVISIONS_PER_BUILD)
				
				changelogNewlineIndex = description.find("\n", changelogIndex)
				if (changelogNewlineIndex >= 0):
					description = description[changelogNewlineIndex:].rstrip("\r\n").rstrip("\n").lstrip("\r\n").lstrip("\n")			
					if (bFoundVersion):						
						changelog_pending += description + "\n"
						
			description = ""
		else:
			if (line != ""):
				description += line.lstrip("\t").replace("\r", "")
		 
	print(changelog)
		 
	# Write out the version file output.
	print("Written changelog to "+output_file)
	file = open(output_file, "w")
	file.write(changelog)
	file.close()

if __name__=="__main__":
    GenerateChangelog("..\\..\\changelog.txt");
