#!/usr/bin/env ruby

if Encoding.default_external != Encoding::UTF_8
  puts <<-DOC
\e[33mWARNING: CocoaPods requires your terminal to be using UTF-8 encoding.
Consider adding the following to ~/.profile:

export LANG=en_US.UTF-8
\e[0m
DOC
end

if $PROGRAM_NAME == __FILE__ && !ENV['COCOAPODS_NO_BUNDLER']
  ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
  require 'rubygems'
  require 'bundler/setup'
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
elsif ENV['COCOAPODS_NO_BUNDLER']
  require 'rubygems'
  gem 'cocoapods'
end

STDOUT.sync = true if ENV['CP_STDOUT_SYNC'] == 'TRUE'

require 'cocoapods'

if profile_filename = ENV['PROFILE']
  require 'ruby-prof'
  reporter = case (profile_extname = File.extname(profile_filename))
  when '.txt'
    RubyProf::FlatPrinterWithLineNumbers
  when '.html'
    RubyProf::GraphHtmlPrinter
  when '.callgrind'
    RubyProf::CallTreePrinter
  else
    raise "Unknown profiler format indicated by extension: #{profile_extname}"
  end
  File.open(profile_filename, 'w') do |io|
    reporter.new(RubyProf.profile { Pod::Command.run(ARGV) }).print(io)
  end
else
  Pod::Command.run(ARGV)
end
