#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))

require 'localeapp'

# Don't connect to the net if we're running under cucumber for testing
# Use FakeWeb to simulate api.localeapp.com
if ENV['FAKE_WEB_DURING_CUCUMBER_RUN'] && fake_data_as_json = ENV['FAKE_WEB_FAKES']
  require 'fakeweb'
  FakeWeb.allow_net_connect = false
  fakes = JSON.parse(fake_data_as_json)
  fakes.each do |fake|
    FakeWeb.register_uri fake['method'].to_sym, fake['uri'], { :body => fake['body'], :status => fake['status'] }.merge(fake['headers'])
  end
end

def include_config_file
  unless Localeapp.include_config_file
    puts "Could not load config file"
    exit 1
  end
end

require 'gli'
include GLI

version Localeapp::VERSION

desc "Creates new configuration files and confirms key works"
arg_name "<api_key>"
command :install do |c|
  c.action do |global_options, options, args|
    key = args.first
    installer = Localeapp::CLI::Install.new
    unless installer.execute(key)
      exit_now! "", 1
    end
  end
end

desc "Sends the key and content to localeapp.com"
arg_name "<key> <locale:content> (<locale:content> ...)"
command :add do |c|
  c.action do |global_options, options, args|
    key = args.shift
    if key.nil? || args.size.zero?
      exit_now! "localeapp add requires a key name and at least one translation", 1
    else
      include_config_file
      Localeapp::CLI::Add.new.execute(key, *args)
    end
  end
end

desc "Pulls all translations from localeapp.com"
command :pull do |c|
  c.action do |global_options, options, args|
    include_config_file
    Localeapp::CLI::Pull.new.execute
  end
end

desc "Pushes a translation file or directory to localeapp.com"
arg_name "<file>"
command :push do |c|
  c.action do |global_options, options, args|
    if args.empty?
      exit_now! "localeapp push requires an file or directory to push", 1
    else
      include_config_file
      path = args.first
      pusher = Localeapp::CLI::Push.new
      pusher.execute(path)
    end
  end
end

desc "Gets any changes since the last poll and updates the yml"
command :update do |c|
  c.action do |global_options, options, args|
    include_config_file
    Localeapp::CLI::Update.new.execute
  end
end

desc "Simple daemon (checks for new translations in the background)"
command :daemon do |c|
  c.desc "Interval to wait between checks"
  c.arg_name 'interval'
  c.default_value 5
  c.flag [:i, :interval]

  c.desc "run the daemon in the background"
  c.switch [:b, 'background']

  c.action do |global_options, options, args|
    include_config_file

    interval = options[:interval].to_i

    if interval <= 0
      exit_now! "interval must be a positive integer greater than 0", 1
    end

    command = Proc.new do
      loop do
        Localeapp::CLI::Update.new.execute
        sleep interval
      end
    end
    if options[:background]

      if File.exists? Localeapp.configuration.daemon_pid_file
        begin
          daemon_pid = File.read(Localeapp.configuration.daemon_pid_file)
          Process.kill("QUIT", daemon_pid.to_i)
        rescue Errno::ESRCH
          File.delete(Localeapp.configuration.daemon_pid_file)
        end
      end

      STDOUT.reopen(File.open(Localeapp.configuration.daemon_log_file, 'a'))
      pid = fork do
        command.call
      end

      File.open(Localeapp.configuration.daemon_pid_file, 'w') {|f| f << pid}
    else
      command.call
    end
  end
end

exit GLI.run(ARGV)