import os
from sys import argv
from mod_pbxproj import XcodeProject

#path = "/Users/jesudaslobo/VservAdUnitySample/VservUnityResources"
# path = "/Volumes/Development/Unity Projects/Test Unity/UnityEditorExtensionVmax/iosBuild1"
path = argv[1]
#fileToAddPath = "/Users/jesudaslobo/VservAdUnitySample/Assets/WebPlayerTemplates"
# fileToAddPath = "/Users/jesudasjosephlobo/Library/Application Support/DefaultCompany/UnityEditorExtensionVmax/VMaxiOS/Adapter"
fileToAddPath = argv[2]

#Adding Required Frameworks for Ad SDK
#if framework is optional, add `weak=True`


project = XcodeProject.Load(path +'/Unity-iPhone.xcodeproj/project.pbxproj')
frameworks = project.get_or_create_group('Frameworks')
vmax = project.get_or_create_group('VMAX')
project.add_file_if_doesnt_exist('System/Library/Frameworks/CoreTelephony.framework', parent=frameworks, weak=True, tree='SDKROOT')
project.add_file_if_doesnt_exist('System/Library/Frameworks/MobileCoreServices.framework', parent=frameworks, weak=True, tree='SDKROOT')
project.add_file_if_doesnt_exist('System/Library/Frameworks/StoreKit.framework', parent=frameworks, weak=True, tree='SDKROOT')
project.add_file_if_doesnt_exist('System/Library/Frameworks/Social.framework', parent=frameworks, weak=True, tree='SDKROOT')
project.add_file_if_doesnt_exist('System/Library/Frameworks/EventKit.framework', parent=frameworks, weak=True, tree='SDKROOT')
project.add_file_if_doesnt_exist('System/Library/Frameworks/EventKitUI.framework',parent=frameworks, weak=True, tree='SDKROOT')
project.add_file_if_doesnt_exist('System/Library/Frameworks/AdSupport.framework', parent=frameworks, weak=True, tree='SDKROOT')
project.add_file_if_doesnt_exist('System/Library/Frameworks/MessageUI.framework', parent=frameworks, weak=True, tree='SDKROOT')
project.add_file_if_doesnt_exist('System/Library/Frameworks/WebKit.framework', parent=frameworks, weak=True, tree='SDKROOT')
project.add_file_if_doesnt_exist('usr/lib/libsqlite3.dylib', tree='SDKROOT')
project.add_file_if_doesnt_exist('usr/lib/libz.dylib', tree='SDKROOT')


#Adding the Vserv Ad SDK
files_in_dir = os.listdir(fileToAddPath)

for f in files_in_dir:
    if not f.startswith('.'): #exclude .DS_STORE on mac
        print (f)

    pathname = os.path.join(fileToAddPath, f)
    fileName, fileExtension = os.path.splitext(pathname)

    if not fileExtension == '.meta': #skip .meta file
        if os.path.isfile(pathname):
            print "is file : " + pathname
            if fileExtension == '.framework':
                project.add_file_if_doesnt_exist(pathname, parent=frameworks, weak=True, tree='SDKROOT')
            else :
                project.add_file_if_doesnt_exist(pathname)

        if os.path.isdir(pathname):
            print "is dir : " + pathname
            project.add_folder(pathname, parent=vmax, excludes=["^.*\.meta$"])


#Adding Linker Flags
project.add_other_ldflags(['-ObjC','-lxml2'])
project.add_single_valued_flag('ENABLE_BITCODE', 'NO')
project.add_single_valued_flag('CLANG_ENABLE_MODULES', 'YES')
project.add_single_valued_flag('GCC_ENABLE_OBJC_EXCEPTIONS', 'YES')
project.add_framework_search_paths(fileToAddPath+"/Adapter",recursive=True)
project.add_framework_search_paths(fileToAddPath+"/VMaxNativeIconAdHelper",recursive=True)
# project.add_other_buildsetting('GCC_ENABLE_OBJC_EXCEPTIONS', 'YES')

#Save changes to Xcode project file
if project.modified:
    project.backup()
    project.save()
