require 'xcodeproj'
require 'pathname'

current_directory=File.dirname(__FILE__)
ios_project_directory=`cd #{current_directory}; cd ..; 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 == "[AirbridgeQA] Prepare build parameter"}[0]
script = 'sh "$SRCROOT/../../script/PrepareBuildParameter.sh"'
if phase == nil
    phase = target.new_shell_script_build_phase("[AirbridgeQA] Prepare build parameter")
    target.build_phases.move(phase, 0)
end
if phase.shell_script != script
    phase.shell_script = script
end

project.save()
