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", "")

package=JSON.parse(File.read(File.join(current_directory, '..', 'package.json')))
project = Xcodeproj::Project.open(ios_project_file)
target = project.targets.first
phase = target.shell_script_build_phases().select {|phase| phase.name == "[Airbridge] Create LibraryInfo.json"}[0]
script = (
    "echo '{\"wrapperName\": \"" + package["name"] + "\", \"wrapperVersion\": \"" + package["version"] + "\"}' > \"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/LibraryInfo.json\"" + "\n"
)
if phase == nil
    phase = target.new_shell_script_build_phase("[Airbridge] Create LibraryInfo.json")
end
if phase.shell_script != script
    phase.shell_script = script
end

project.save()