#coded for Python 2.7.10

#builds all spatial audio sample projects
import os
import sys

#include python script that's outside of this directory (without using heavyweight packages for this lightweight script)
fileAbsolutePath = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, r"%s\..\..\pythonScripts" % fileAbsolutePath)
from python_common import projectNames


# collect command line arguments
clean = compile = package = deploy = installAssets = False
cleanStr = "clean".lower()
compileStr = "compile".lower()
packageStr = "package".lower()
deployStr = "deploy".lower()
installAssetsStr = "installAssets".lower()
for commandLineArg in sys.argv:
	commandLineArgLower = commandLineArg.lower()
	if(commandLineArgLower == cleanStr):
		clean = True
	elif(commandLineArgLower == compileStr):
		compile = True
	elif(commandLineArgLower == packageStr):
		package = True
	elif(commandLineArgLower == deployStr):
		deploy = True
	elif(commandLineArgLower == installAssetsStr):
		installAssets = True

if clean == False and compile == False and package == False and deploy == False and installAssets == False:
	sys.exit("No valid command line arguments provided.  Valid command line arguments should be space-delimited, and can come in any combination and in any order.\n\nFor example:\npython %s %s %s %s\n\nAll valid arguments:\n%s %s %s %s %s" % (__file__, compileStr, packageStr, deployStr, cleanStr, compileStr, packageStr, deployStr, installAssetsStr))
		
# collect project directories		
projectNameIndex=0
projectDirs = [None]*len(projectNames)
for projectName in projectNames:
	projectDirs[projectNameIndex] = r"%s\..\%s" % (fileAbsolutePath, projectNames[projectNameIndex])
	projectNameIndex += 1

def IssueCommandOnProjects(projectDirList, shellCommand, delimiterString, failString):
	for projectDir in projectDirList:
		print "\n%s\n%s\n%s%s\n" % (delimiterString, projectDir, shellCommand, delimiterString)	
		os.chdir(projectDir)
		if os.system(shellCommand) != 0:
			raise Exception("%s --%s" % (failString, projectDir))


# process command line arguments
delimiterString = "\n------------------------------"
if clean:
	IssueCommandOnProjects(projectDirs, r"%NDK_ROOT%/ndk-build clean", delimiterString, "%s\nFailed ndk clean step for " % delimiterString)

if compile:
	IssueCommandOnProjects(projectDirs, r"%NDK_ROOT%/ndk-build -j", delimiterString, "%s\nFailed compile step for " % delimiterString)

if clean:
	IssueCommandOnProjects(projectDirs, r"ant clean", delimiterString, "%s\nFailed ant clean step for " % delimiterString)	
	
if package:
	IssueCommandOnProjects(projectDirs, "ant debug", delimiterString, "%s\nFailed package step for " % delimiterString)
	
if deploy:
	IssueCommandOnProjects(projectDirs, "ant installd", delimiterString, "%s\nFailed deploy step for " % delimiterString)
	
if installAssets:
	IssueCommandOnProjects(projectDirs, "InstallAssetsAndroid.bat", delimiterString, "%s\nFailed asset install step for " % delimiterString)
					
# no exceptions raised; succeeded
print(delimiterString)
print "Successful execution for all projects"