#!/usr/bin/env ruby

require 'optparse'
require File.expand_path(File.join(File.dirname(__FILE__), '..','lib', 'file_processor.rb'))

options = {}

OptionParser.new do |opts|
  opts.banner = "Usage: fs_reader [options] /path/to/scan"

  opts.on("-L", "--local", "files from a local agency") do |l|
    options[:local] = l
  end

  opts.on("-H", "--hotfolder", "files will be erased from source after processing") do |l|
    options[:folder] = l
  end

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end

end.parse!

if ARGV.empty?
  puts 'error : no arguments, use -h or --help for help'
  exit
end

@connection = Bunny.new
begin
  @connection.start
rescue Bunny::ServerDownError => e
  LOG.fatal("check if rabbitmq is up : #{e}")
  raise
  exit
end

ftp_config = YAML.load_file(File.join(FP_ROOT, 'config', 'ftp.yml'))
feed = options[:local] ? 'local' : ftp_config[:feed]
#feed = options[:local] ? 'local' : 'pixpalace'
folder = options[:folder] ? 'hotfolder' : 'coldfolder'

@jpeg_fs_reader = FsReader.new(*ARGV)
channel  = @connection.create_channel
topic = channel.topic('medias')

LOG.info "Starting publishers"

@jpeg_fs_reader.each do |f|
  LOG.debug("publishing #{f} on #{feed}.image.iptc.#{folder}")
  topic.publish(f, :key => "#{feed}.image.iptc.#{folder}")
end

@connection.stop