#!/usr/bin/ruby

require 'rubygems'
require 'getoptlong'
require 'esearchy'
ESearchy::LOG.level = ESearchy::APP

@yahoo_key = nil
@bing_key = nil
@maxhits = nil
@domains = []
@output = nil
@company = []
@no_eng = Hash.new

opts = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--disable-google', GetoptLong::NO_ARGUMENT ],
[ '--disable-yahoo', GetoptLong::NO_ARGUMENT ],
[ '--disable-bing', GetoptLong::NO_ARGUMENT ],
[ '--disable-altavisa', GetoptLong::NO_ARGUMENT ],
[ '--disable-linkedin', GetoptLong::NO_ARGUMENT ],
[ '--disable-gprofiles', GetoptLong::NO_ARGUMENT ],
[ '--disable-naymz', GetoptLong::NO_ARGUMENT ],
[ '--disable-ggroups', GetoptLong::NO_ARGUMENT ],
[ '--disable-pgp', GetoptLong::NO_ARGUMENT ],
[ '--disable-usenet', GetoptLong::NO_ARGUMENT ],
['--domain','-d', GetoptLong::REQUIRED_ARGUMENT ],
['--company','-c', GetoptLong::REQUIRED_ARGUMENT ],
['--file','-f', GetoptLong::REQUIRED_ARGUMENT ],
['--filter','-p', GetoptLong::REQUIRED_ARGUMENT ],
['--output','-o', GetoptLong::REQUIRED_ARGUMENT ],
['--yahoo_key','-y', GetoptLong::REQUIRED_ARGUMENT ],
['--bing_key','-b', GetoptLong::REQUIRED_ARGUMENT ],
['--maxhits','-m', GetoptLong::REQUIRED_ARGUMENT ]
)

opts.each do |opt, arg|
  case opt
    when '--help':
      # BEGIN OF HELP
      ESearchy::LOG.puts "\nHELP for Esearchy\n---------------------\n
      --help, -h
      \tWell I guess you know what this is for (To obtain this Help).\n
      --domain, -d [domain.com]
      \t The domain name to search.\n
      --disable-google
      \t Disables Google searches.\n
      --disable-yahoo
      \t Disables Yahoo searches.\n
      --disable-bing
      \t Disables Bing searches.\n
      --disable-linkedin
      \t Disables LinkedIn searches.\n
      --disable-gprogiles
      \t Disables Google Profiles searches.\n
      --disable-naymz
      \t Disables Naymz searches.\n
      --disable-ggroups
      \t Disables Google Groups searches.\n
      --disable-pgp
      \t Disables PGP searches.\n
      --disable-usenet
      \t Disables Usenet searches.\n
      --filter, -p
      \t The pattern to use to filter emails.(not fully implemented)\n
      --file, -f [file_name] 
      \tIf we need to search more than one domain we can provide a list.\n
      --output, -o
      \tThe output file name.
      Copyright 2009 - FreedomCoder\n"
      #END OF HELP
      exit(0)
    when '--disable-google':
      @no_eng[:Google] = false
    when '--disable-yahoo':
      @no_eng[:Yahoo] = false
    when '--disable-bing':
      @no_eng[:Bing] = false
    when '--disable-altavisa':
      @no_eng[:Altavista] = false
    when '--disable-linkedin':
      @no_eng[:LinkedIn] = false
    when '--disable-gprofiles':
      @no_eng[:GoogleProfiles] = false
    when '--disable-naymz':
      @no_eng[:Naymz] = false
    when '--disable-ggroups':
      @no_eng[:GoogleGroups] = false
    when '--disable-pgp':
      @no_eng[:PGP] = false
    when '--disable-usenet':
      @no_eng[:Usenet] = false
    when '--domain':
      @domains << arg
    when '--company':
      @company << arg
    when '--file':
      if File.exists?(arg)
        open(arg,'r').each_line do |line|
          d,c = line.split(',')
          @domains << d
          @company << c
        end
      else
        ESearchy::LOG.puts "File not found"
      end
    when '--yahoo_key':
      @yahoo_key = arg
    when '--bing_key':
      @bing_key = arg
    when '--filter':
      @pattern = arg
    when '--output':
      @output = arg
    when '--licredentials':
      @li_username, @li_password = arg.split(":")
    when '--maxhits':
      @maxhits = arg.to_i
    else
      ESearchy::LOG.puts "Unknown command. Please try again"
      exit(0)
  end
end

require 'esearchy'
puts "DISCLOSURE: This is just an example tool ESearchy is more and more a piece 
of code intended to work as a Library and you should create your own little.rb file :)"
puts "------------------------------------------------------------------------"
puts "REMINDER: if you want to use GoogleProfiles, LinkedIn or Naymz, you will 
need to use the --company (-c) <company_name> option"
@domains.each_with_index do |domain, idx|
  ESearchy.create domain do |d|
    @no_eng.each do |eng,val| 
      search_engine(eng, val)
    end
    d.yahoo_key = @yahoo_key if @yahoo_key
    d.bing_key = @bing_key if @bing_key
    d.maxhits = @maxhits if @maxhits
    if (@li_username and @li_password)
      d.linkedin_credentials= [@li_username, @li_password]
    else
      d.linkedin_credentials = ESearchy::BUGMENOT
    end
    d.company_name = @company[idx] unless @company.empty?
    d.search
    d.save_to_file @output if @output
  end
end
