#!/usr/bin/env python

import sys, os.path
from mod_pbxproj import XcodeProject

install_path = sys.argv[1]
target_platform = sys.argv[2]
fb_app_id = '136228806535961'

if target_platform != "iPhone": sys.exit()

# install Facebook plist settings
info_plist_path = os.path.join(install_path, 'Info.plist')

file = open(info_plist_path, 'r')
plist = file.read()
file.close()

elements_to_add = '''
<key>FacebookAppId</key>
<string>%s</string>
<key>CFBundleURLTypes</key>
<array>
 <dict>
  <key>CFBundleURLSchemes</key>
  <array>
   <string>fb%s</string>
  </array>
 </dict>
</array>
<key>CFBundleLocalizations</key>
<array>
 <string>en</string>
 <string>ja</string>
</array>
''' % (fb_app_id, fb_app_id)

plist = plist.replace('<key>', elements_to_add + '<key>', 1)

file = open(info_plist_path, 'w')
file.write(plist)
file.close()

#modify XCode pbxproj and add framework dependencies
pbxproj_path = os.path.join(install_path, 'Unity-iPhone.xcodeproj/project.pbxproj')
project = XcodeProject.Load(pbxproj_path)

project.add_file('System/Library/Frameworks/Accounts.framework', tree='SDKROOT', weak=True)
project.add_file('System/Library/Frameworks/AdSupport.framework', tree='SDKROOT', weak=True)
project.add_file('System/Library/Frameworks/Social.framework', tree='SDKROOT', weak=True)
project.add_file('usr/lib/libsqlite3.0.dylib', tree='SDKROOT')

if project.modified:
  project.backup()
  project.saveFormat3_2() #IMPORTANT, DONT USE THE OLD VERSION!