# Utility script to rename a watershed
# Basically, all you need to do is rename the base watershed folder
# to the name you want, and then just run this script through Python
# It will rename the .sty, .wksp, and .log files and update the text inside
# (also updates the .bak files)
# them at the top lines to use the new names
# This cuts down the multi-step process that renaming a watershed normally is 
# Make sure the watershed is closed when you run this script

import os
import sys
import time

def replaceAndRename(origStub, newStub, extension):
	# replace all the contents of a file where "origStub" had been with "newStub"
	# and then rename the file from origStub to newStub
	# extension is the file extension
	fileObj = open(origStub+extension,"r")
	fileContents = str(fileObj.read())
	newContents = str(fileContents)
	newContents = newContents.replace(origStub, newStub)
	fileObj.close()
	fileObj = open(origStub+extension,"w")
	fileObj.write(newContents)
	fileObj.close()	
	# need to rename the file
	os.rename(origStub+extension, newStub+extension)	

print "start"
extensions = [".wksp", ".sty", ".log"]
curDir = os.path.dirname(sys.argv[0]) + "\\"
newName = curDir.split("\\")[-2] # the new watershed name (the folder name)
for f in os.listdir(curDir): # loop through all files in the directory
	try:
		last5 = f[-5:] # get the last five digits
	except:
		continue # the filename is too short
	for ext in extensions: # loop through all desired extensions
		if ext in last5:	
			oldName = f.split(".")[0]
			try:
				replaceAndRename(oldName, newName, ext)
			except Exception, e:
				print "exception!"
				print type(e)
				raise e
			print "\nReplaced file: ", f
			# try to repeat for .bak files if they exist
			try:
				replaceAndRename(oldName, newName, ext + ".bak")
				print "replaced .bak file as well"
			except:
				print "could not replace .bak file"
				pass
logFileName = os.path.join(curDir, "watershedChangeLog.md")
logFile = open(logFileName, 'a')
logFile.write("\n\n# `%s` [%s / %s]\n" % (newName, time.strftime("%d %b %Y %H:%M"), os.environ["USERNAME"]))
logFile.close()
os.system("notepad.exe %s" % logFileName)

print "done"
raw_input("Press Enter to terminate") 

