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 npm_command(env)
  if env == "aws"
    ret = "scl enable python27 devtoolset-3 'CC=/opt/rh/devtoolset-3/root/usr/bin/gcc CXX=/opt/rh/devtoolset-3/root/usr/bin/g++ /usr/local/bin/npm install --unsafe-perm --registry https://store.vistacore.us/nexus/content/repositories/npm-all/'"
  else
    ret = "npm install --registry https://store.vistacore.us/nexus/content/repositories/npm-all/"
  end
  ret
end

def environment_endpoints(env)
  case env
  when "virtualbox"
    p "Running with virtualbox environments"
    ENV["VXSYNC_IP"] = knife_search_for_ip("primary_vxsync_client")
    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("primary_vxsync_client")
    begin
      ENV["JDS_IP"] = knife_search_for_ip("jds")
    rescue
      ENV["JDS_IP"] = knife_search_for_ip("jds_app_server")
    end
    ENV["SOLR_IP"] = knife_search_for_ip("solr")
    ENV["MOCKS_IP"] = knife_search_for_ip("mocks")
  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("primary_vxsync_client")
  system "scp -i #{ENV['WORKSPACE']}/.chef/keys/vagrantaws_c82a142d5205 -o StrictHostKeyChecking=no DNS     @#{vxsync_ip}:/opt/vxsync_client/worker-config.json ."
when "virtualbox"
  vxsync_ip = knife_search_for_ip("primary_vxsync_client")
  system "scp -i #{ENV['VAGRANT_HOME']}/insecure_private_key vagrant@#{vxsync_ip}:/opt/vxsync_client/worker-config.json ."
end
end



task :inttest, :env do |task, args|
  puts "start npm install"
  sh npm_command(args[:env])
  puts "completed npm install"
  puts "start integration tests"
  sh "npm run-script int-test"
  puts "completed integration tests"
end

task :operational_data, :env do |task, args|
  puts "start npm install"
  sh npm_command(args[:env])
  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, :env do |task, args|
  puts "start npm install"
  sh npm_command(args[:env])
  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
