require 'xcodeproj'
require 'pathname'

current_directory=File.dirname(__FILE__)

ios_project_directory=`cd #{current_directory}; cd #{Pod::Config.instance.installation_root}; pwd`.gsub("\n", "")
ios_project_file=File.join(
    ios_project_directory,
    `cd #{ios_project_directory}; find . -maxdepth 1 -name "*.xcodeproj" | tail -1`,
).gsub("\n", "")

project = Xcodeproj::Project.open(ios_project_file)
target = project.targets.first
phase = target.shell_script_build_phases().select {|phase| phase.name == "[Airbridge] Copy airbridge.json"}[0]
script = (
    "if [ -r \"${SRCROOT}/../airbridge.json\" ]; then" + "\n" \
    + "    cp \"${SRCROOT}/../airbridge.json\" \"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/airbridge.json\"" + "\n" \
    + "fi"
)
if phase == nil
    phase = target.new_shell_script_build_phase("[Airbridge] Copy airbridge.json")
end
if phase.shell_script != script
    phase.shell_script = script
end

project.save()