#!/usr/bin/env ruby

require 'rubygems'
require 'react'
require 'optparse'
require 'daemons'

if ARGV.empty?
  ARGV << '--help'
else 
  if File.exists?(ARGV[0])
    options[:commands] = YAML.load_file(ARGV[0])
  else
    puts "ERROR: File not found: `#{ARGV[0]}`"
    exit
  end
end

options = { 
  :redis => {:host => '127.0.0.1', :port => 6379, :db => 0}, 
  :commands => {}, 
  :queue => 'queue' 
}

opts = OptionParser.new do |opts|
  opts.banner = "Usage: react commands.yml [options]"
  opts.on('-q', '--queue [QUEUE]', 'Specify queue which will be consumed') {|val| val and options[:queue] = val }
  opts.on('-h', '--host [HOST]', 'Select redis host') {|val| val and options[:redis][:host] = val }
  opts.on('-p', '--port [PORT]', Integer, 'Select redis port') {|val| val and options[:redis][:port] = val }
  opts.on('-D', '--db [DATABASE]', 'Select redis database number') {|val| val and options[:redis][:db] = val }
  opts.on('-d', '--daemon', 'Run in background') { options[:daemon] = true }
end.parse(ARGV)

React.start(options).join