import re, os, sys
import subprocess

import zipfile
import ftplib

platform = 'unknown'
if sys.platform.startswith('linux'):
	platform = 'linux'
elif sys.platform.startswith('win32'):
	platform = 'windows'
elif sys.platform.startswith('darwin'):
	platform = 'mac'

def HGVersion():
	p = subprocess.Popen(['hg', 'identify',  '-n'], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
	(stdout, stderr) = p.communicate()
	stdout = stdout.decode('UTF-8')

	return stdout.rstrip('+\r\n')

def ReleaseVersion():
	productFile = open('product.version', 'r')
	productContents = productFile.read()
	productFile.close()

	m = re.match('(\d+)\.(\d+)\.(\d+)(.*)', productContents)
	if m == None:
		raise Exception('Could not detremine product version')
	
	major, minor, release, tag = m.groups()
	return '.'.join([major, minor, release])

filename = '-'.join(['tf2items', ReleaseVersion(), 'hg' + HGVersion(), platform])

debug_build = os.environ.get('is_debug_build', False) == "1"

if debug_build:
	filename += '-debug'

filename += '.zip'

zip = zipfile.ZipFile(filename, 'w', zipfile.ZIP_DEFLATED)

for base, dirs, files in os.walk('package'):
	for file in files:
		fn = os.path.join(base, file)
		fns = fn[(len('package') + 1):]

		zip.write(fn, fns)

print("%-33s %-10s %21s %12s" % ("File Name", "CRC32", "Modified      ", "Size"))
for zinfo in zip.infolist():
	date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time[:6]
	print("%-33s %-10d %21s %12d" % (zinfo.filename, zinfo.CRC, date, zinfo.file_size))

zip.close()

if 'ftp_hostname' in os.environ:
	print('')

	ftp = ftplib.FTP(os.environ['ftp_hostname'], os.environ['ftp_username'], os.environ['ftp_password'])
	print('Connected to server, uploading build...')
	ftp.cwd(os.environ['ftp_directory'])
	print(ftp.storbinary('STOR ' + filename, open(filename, 'rb')))
	ftp.quit()
	
	print('Uploaded as \'' + filename + '\'')

	os.unlink(filename)

