regions = {}

puts "running grepy findy command"
%x[find /home/gravitystorm/public_html/redactions/logs-live/ -name "*.log" -type f -print0 | xargs -0 grep -F "Processing region"].each_with_index do |line, line_no |

  #break if line_no==200

  filename = line.split(":")[0]
  filename = filename.split("logs-live/")[1]

  status = "UNKNOWN"
  tail = `tail /home/gravitystorm/public_html/redactions/logs-live/#{filename}`
  if tail.include?(': Summary')
    tail = tail.split(': Summary')[1]
    summary = ""
    successful_changesets = true
    successful_candidates = true
    failed_changesets = true
    failed_candidates = true
    tail.lines.each do | tailline |
      if tailline.include?('-- :')
        summary += tailline.split('-- : ')[1].strip + "<br>"
        successful_changesets = false if tailline.include?(" 0 successful changesets")
        successful_candidates = false if tailline.include?(" 0 successful candidates")
        failed_changesets = false if tailline.include?(" 0 failed changesets")
        failed_candidates = false if tailline.include?(" 0 failed candidates")
      end
    end
    status = "SUCCESS"
    status = "ZEROS" if not successful_changesets and not successful_candidates
    status = "ERRORS" if failed_changesets or failed_candidates
  elsif tail.include?('Marking region failed')
    status="FAIL"
    summary="Region failed"
  end

  line = line.split("[")[1]
  time = line.split(".")[0]

  region_hash = line.split("region {")[1]
  region_hash.gsub!(":","")
  region_hash.gsub!("=>",":")
  #line = "{ filename:\"" + filename + "\", status:\"" + status + "\", summary:\"" + summary.to_s + "\", time:\"" + time + "\", " + region_hash.strip
  # output_lines << line

  chomped_string = region_hash.split("lat:")[1]
  lat = chomped_string.split(", ")[0]
  chomped_string = chomped_string.split("lon:")[1]
  lon = chomped_string.split("}")[0]

  log_data = { :filename => filename,
               :status => status,
               :summary => summary.to_s,
               :time => time,
               :lat => lat,
               :lon => lon }


  description = "Region (" + lat + ", " + lon + ")<br>" +
                "Started time:" + log_data[:time] + "<br><br>" +
                log_data[:summary] + "<br><br>" +
                "Detailed logs: <a href='http://gravitystorm.dev.openstreetmap.org/redactions/logs-live/" +
                log_data[:filename].to_s + "'>" + log_data[:filename].to_s + "</a>"


  if status=="UNKNOWN" and log_data[:summary]==""
    status="CURRENT"
    description = "No summary yet. This region appears to be 'in progress'.<br><br>" + description
  end 

  latlon = lat + "," + lon
  puts "line_no " + line_no.to_s + " latlon=" + latlon  if line_no % 100 ==0

  #foundIndex = -1;
  #regions.each_with_index do |on_region, searchpos|
  #  foundIndex=searchpos if on_region[0]==lat and on_region[1]==lon 
  #end

  if regions[latlon].nil?
    #We didn't encounter these coords before. New region
    regions[latlon] = [lat,lon,status, description]; 
  else 
    #We encountered these coords before. Modify this region data
    regions[latlon][3] = description + "<br><hr>...and an earlier run:<br>" + regions[latlon][3];
    regions[latlon][2] = status
    regions[latlon][2] = "RERUNS" if status=="SUCCESS" or status=="ZEROS"
  end    
     
end

puts "writing output file"
File.open('botprocessing2.json', 'w') do |f|
  f.write("regions=[")
  regions.values.each_with_index do |region, i|
    region[2] = '"' + region[2] + '"'
    region[3] = '"' + region[3] + '"'
    f.write("[" + region.join(",") + "]")
    f.write(",\n") unless i==regions.size
  end
  f.write("];")
end
