require 'rubygems'
require 'cucumber'
require 'cucumber/rake/task'
require 'rubocop/rake_task'
require 'json'

task :default => [:inttest]

task :inttest, [:env] => [:environment_setup]
task :operational_data, [:env] => [:environment_setup]
task :patient_sync, [:env] => [:environment_setup]

task :environment_setup, :env do |task, args|
  env = args[:env] || 'virtualbox'

  environment_endpoints(env)
  get_worker_config(env)
end

def environment_endpoints(env)
  case env
  when "virtualbox"
    p "Running with virtualbox environments"
    ENV["VXSYNC_IP"] = knife_search_for_ip("vxsync")
    ENV["JDS_IP"] = knife_search_for_ip("jds")
    begin
      ENV["SOLR_IP"] = knife_search_for_ip("solr")
    rescue
      puts "solr VM not found, trying to find mocks..."
      ENV["SOLR_IP"] = knife_search_for_ip("mocks")
    end
    ENV["KODAK_IP"] = knife_search_for_ip("vista-kodak")
    ENV["PANORAMA_IP"] = knife_search_for_ip("vista-panorama")
    ENV["JMEADOWS_IP"] = knife_search_for_ip("mocks")
    ENV["MVI_IP"] = knife_search_for_ip("mocks")
    ENV["HDR_IP"] = knife_search_for_ip("mocks")
    ENV["VLER_IP"] = knife_search_for_ip("mocks")
    ENV["PGD_IP"] = knife_search_for_ip("mocks")
  when "aws"
    p "Running with aws environments"
    ve_api_machine = ENV["VE_API_NAME"] || "ve-api-local"
    ENV["VXSYNC_IP"] = knife_search_for_ip("vxsync")
    ENV["JDS_IP"] = knife_search_for_ip("jds")
    ENV["SOLR_IP"] = knife_search_for_ip("solr")
    ENV["MOCKS_IP"] = knife_search_for_ip("mocks")
  when "ede"
    p "Running with ede environments"
    # TODO: Update these when we decide on a permanent location for EDE IP addresses
    ENV["VXSYNC_IP"] = Servers::EHMP.localIP
    ENV["JDS_IP"] = Servers::JDS.localIP
    ENV["SOLR_IP"] = Servers::Solr.localIP
  when "ede-lxc"
    p "Running with ede-lxc environments"
    ENV["VXSYNC_IP"] = Containers::EHMP.localIP
    ENV["JDS_IP"] = Containers::JDS.localIP
    ENV["SOLR_IP"] = Containers::Solr.localIP
  else
    fail "Unrecognized environment type: #{env}. Allowable types are: 'virtualbox', 'aws', and 'ede', and 'ede-lxc'"
  end
end

def get_worker_config(env)

case env
when "aws"
  vxsync_ip = knife_search_for_ip("vxsync")
  system "scp -i #{ENV['WORKSPACE']}/.chef/keys/vagrantaws_c82a142d5205 -o StrictHostKeyChecking=no ec2-user@#{vxsync_ip}:/opt/vxsync/worker-config.json ."
when "virtualbox"
  vxsync_ip = knife_search_for_ip("vxsync")
  system "scp -i #{ENV['VAGRANT_HOME']}/insecure_private_key vagrant@#{vxsync_ip}:/opt/vxsync/worker-config.json ."
end
end

task :inttest do
  puts "start npm install"
  sh "npm install"
  puts "completed npm install"
  puts "start integration tests"
  sh "npm run-script int-test"
  puts "completed integration tests"
end

task :operational_data do
  puts "start npm install"
  sh "npm install"
  puts "completed npm install"
  puts "start operational data check against #{ENV['JDS_IP']}"
  sh "npm run-script operational-check"
  puts "completed operational data check"
end

task :patient_sync do
  puts "start npm install"
  sh "npm install"
  puts "completed npm install"
  puts "start patient sync against #{ENV['VXSYNC_IP']}"
  sh "npm run-script sync-patient"
  puts "completed patient"
end

def knife_search_for_ip(machine_name)
  release = File.read(File.dirname(__FILE__) + "/../../../infrastructure/properties/releaseVersion")
  stack = ENV['JOB_NAME'] || "#{ENV['USER']}-#{release}"
  if ENV.key?('BUNDLE_BIN_PATH')
    raw_search = Bundler.with_clean_env { `/opt/chefdk/bin/knife search node \'role:#{machine_name} AND stack:#{stack}\' -Fj --config ~/Projects/vistacore/.chef/knife.rb` }
  else
    raw_search = `/opt/chefdk/bin/knife search node \'role:#{machine_name} AND stack:#{stack}\' -Fj --config ~/Projects/vistacore/.chef/knife.rb`
  end
  parsed_search = JSON.parse(raw_search, :create_additions => false)
  fail "More than one node with that name found" if parsed_search["results"] > 1
  fail "No node with that name found: \'role:#{machine_name} AND stack:#{stack}\'" if parsed_search["results"] == 0
  ip = parsed_search["rows"][0]['automatic']['ipaddress']
end
