#!/usr/bin/env python
#
# zip up the titanium mobile SDKs into suitable distribution formats
#
import os, types, glob, shutil, sys, platform, codecs
import zipfile, datetime, subprocess, tempfile, time

sys.path.append(os.path.join(os.path.abspath(os.path.dirname(sys._getframe(0).f_code.co_filename)),'..','support','common'))
import simplejson

if platform.system() == 'Darwin':
	import importresolver

packaging_all = False
os_names = { "Windows":"win32", "Linux":"linux", "Darwin":"osx" }
cur_dir = os.path.abspath(os.path.dirname(sys._getframe(0).f_code.co_filename))
top_dir = os.path.abspath(os.path.join(os.path.dirname(sys._getframe(0).f_code.co_filename),'..'))
template_dir = os.path.join(top_dir,'support')
doc_dir = os.path.abspath(os.path.join(top_dir, 'apidoc'))
all_dir = os.path.abspath(os.path.join(template_dir,'all'))
android_dir = os.path.abspath(os.path.join(template_dir,'android'))
iphone_dir = os.path.abspath(os.path.join(template_dir,'iphone'))
osx_dir = os.path.abspath(os.path.join(template_dir,'osx'))
win32_dir = os.path.abspath(os.path.join(template_dir, 'win32'))
mobileweb_dir = os.path.abspath(os.path.join(template_dir, 'mobileweb'))
blackberry_dir = os.path.abspath(os.path.join(template_dir, 'blackberry'))
tizen_dir = os.path.abspath(os.path.join(template_dir, 'tizen'))
windows_dir = os.path.abspath(os.path.join(template_dir, 'windows'))
ivi_dir = os.path.abspath(os.path.join(template_dir, 'ivi'))

buildtime = datetime.datetime.now()
ts = buildtime.strftime("%m/%d/%y %H:%M")

# get the githash for the build so we can always pull this build from a specific
# commit
gitCmd = "git"
if platform.system() == "Windows":
	gitCmd += ".cmd"

p = subprocess.Popen([gitCmd,"show","--abbrev-commit","--no-color"],stderr=subprocess.PIPE, stdout=subprocess.PIPE)
githash = p.communicate()[0][7:].split('\n')[0].strip()

ignoreExtensions = ['.pbxuser','.perspectivev3','.pyc']
ignoreDirs = ['.DS_Store','.git','.gitignore','libTitanium.a','titanium.jar','bridge.txt', 'packaged']

def remove_existing_zips(dist_dir, version_tag):
	for os_name in os_names.values():
		filename = os.path.join(dist_dir,
				'mobilesdk-%s-%s.zip' % (version_tag, os_name))
		if os.path.exists(filename):
			os.remove(filename)

def ignore(file):
	 for f in ignoreDirs:
		if file == f:
			return True
	 return False

def generate_jsca():
	 process_args = [sys.executable, os.path.join(doc_dir, 'docgen.py'), '-f', 'jsca', '--stdout']
	 print "Generating JSCA..."
	 print " ".join(process_args)
	 jsca_temp_file = tempfile.TemporaryFile()
	 try:
		 process = subprocess.Popen(process_args, stdout=jsca_temp_file, stderr=subprocess.PIPE)
		 process_return_code = process.wait()
		 if process_return_code != 0:
			 err_output = process.stderr.read()
			 print >> sys.stderr, "Failed to generate JSCA JSON.  Output:"
			 print >> sys.stderr, err_output
			 return None
		 jsca_temp_file.seek(0)
		 jsca_json = jsca_temp_file.read()
		 return jsca_json
	 finally:
		 jsca_temp_file.close()

def zip_dir(zf,dir,basepath,subs=None,cb=None, ignore_paths=None, ignore_files=None):
	for root, dirs, files in os.walk(dir):