import os
import os.path
import sys
import ssh
from createMD5 import main as createMD5

server = 'cmpdartsvr1.cmp.uea.ac.uk'
usr = 'red08xgu'
pss = 'Matura2000'
localDir = 'VolViewer/'
remoteDir = '/cmpdartsvr1/www/downloads/software/updates/Volviewer/win32/'

print "\n--------------------------------------------------------"
print "MD5: Computing checksums..."
createMD5(localDir)
print "--------------------------------------------------------"
print "CONNECTING: " + server
s = ssh.Connection(host = server, username = usr, password = pss)
print "UPLOAD DESTINATION: " + remoteDir + "--------------------------------------------------------"

#traverse all files in all directories
for dirname, dirnames, files in os.walk("../"):
	#if dirname.find('AutoUpdate') == -1:
	for file in files:

		#get our file path on local machine
		fullfilepath = os.path.join(dirname, file)
		fullfilepath = fullfilepath.replace('\\', '/');
		print "FILE: " + file

		#create corresponding directory structure on remote server
		cmd = 'mkdir '+ remoteDir+dirname
		cmd = cmd.replace("../", localDir)
		cmd = cmd.replace('\\', '/');
		s.execute(cmd)

		#upload the file to the remote server
		remotefilepath = fullfilepath.replace("../", localDir)
		s.put(fullfilepath,remoteDir+remotefilepath)
		
print "--------------------------------------------------------"
print "FINISHED."
print "--------------------------------------------------------"

s.close()
