#!/usr/bin/env ruby
require File.join(File.dirname(__FILE__), '..', 'lib', 'fog')
require 'irb'
require 'yaml'
require File.join('fog', 'credentials')
Fog.credential = ARGV.first ? :"#{ARGV.first}" : :default
unless Fog.credentials
  exit
end

require 'fog/bin'

if ARGV.length > 1
  print(instance_eval(ARGV[1..-1].join(' ')).to_json)
else

  ARGV.clear # Avoid passing args to IRB
  IRB.setup(nil)
  @irb = IRB::Irb.new(nil)
  IRB.conf[:MAIN_CONTEXT] = @irb.context
  IRB.conf[:PROMPT][:FOG] = IRB.conf[:PROMPT][:SIMPLE].dup
  IRB.conf[:PROMPT][:FOG][:RETURN] = "%s\n"
  @irb.context.prompt_mode = :FOG
  @irb.context.workspace = IRB::WorkSpace.new(binding)

  services = Fog.services.map{|service| service.to_s}
  available_services = if services.length > 1
    services[0...-1].join(', ') << ' and ' << services[-1]
  else
    services.first
  end
  print "Welcome to fog interactive!\n"
  print "Your '#{Fog.credential.to_s}' configuration provides access to #{available_services}.\n"
  Fog.services.each do |service|
    if service.respond_to?(:startup_notice)
      service.send(:startup_notice)
    end
  end

  catch(:IRB_EXIT) { @irb.eval_input }

end
