#!/usr/bin/env ruby
#
# This software copyright W3C 2002 - for copyright license see 
# W3C software license -  http://www.w3.org/Consortium/Legal/copyright-software
# Charles McCN - http://www.w3.org/People/Charles
#
# original version is at http://www.w3.org/2002/08/flight/mapflights.rb
#
# map data produced by http://www.w3.org/2002/08/flight/crunchflights.rb
# and http://www.daml.org/cgi-bin/airport?
# sample data is at http://www.w3.org/2002/08/flight/out.rdf
# generated by crunchflights script from data at http://www.w3.org/2002/08/flight/flights
#
# sample result is at http://www.w3.org/2002/08/flight/map.svg !!!!WARNING - may not be valid SVG
#
# Usage: ruby mapflights.rb foo
# (outputs data to stdout)
# 
# todo: don't re-fetch airport data
#	convert the flight information to metadata + path + desc/title
#	factor out the SVG bit to be mapping things according to lat/long
#	realise that jibberJim makes better lat/long mapping and leave that bit to him
#	add 'who' information for the flight

# set up the network thang...

require 'net/http'
airports = Net::HTTP.new('www.daml.org', 80)

person = 'chaals'

# read the file

file = ARGV[0]

data = `cat #{file}` #err

# start the SVG output

puts '<?xml version="1.0"  encoding="UTF-8" standalone="no"?>'
puts '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
   "http://www.w3.org/TR/SVG/DTD/svg10.dtd" [
   <!ATTLIST svg
    xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink">
   ]>';
puts "<svg xmlns='http://www.w3.org/2000/svg'
	xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
	xmlns:travel='http://www.w3.org/2000/10/swap/pim/travelTerms#'
        xmlns:airport='http://www.daml.org/2001/10/html/airport-ont#'
        xmlns:dcterms='http://purl.org/dc/terms/'
	xmlns:cyc='http://opencyc.sourceforge.net/daml/cyc.daml#' >\n";

puts "  <title>Flight data for #{person} from #{file}</title>"
puts "  <image xlink:href='http://jibbering.com/imgs/earth.jpg' width='2000' height='1000'/>"
puts '  <g transform="scale(5.555555555) translate(180 90)" viewbox="-180 90 180 -90">'
puts '    <g transform="scale(1,-1)">'



# make all this happen in a comment for now... (remember to close it afterwards)


# split the data and process each line

flights = data.split(/<Flight>/)
 
flights.each do |f|


# factor out this bit (to END)
  f =~ /<cyc:fromLocation><airport:Airport rdf:resource='(.*?)'/
  airport = $1

  if airport
  # make this use the iata code. (adapt crunchflights to use that?)
    airport =~ /airport.(...)/
    from = $1

    iatafrom = '/cgi-bin/airport?' + from

  # get airport info from the Web
  # this should be a seperate module, and should only happen for data that isn't already available
  # also, needs to get the right namespace for lat/long info that it gets

    resp, data = airports.get(iatafrom, nil)
    airportData = data.split(/\n/)
    print '      <g><line  stroke-width="0.2" stroke="red" y1="'
    airportData.each do |x|
      if x =~ /atitude/ 
        x =~ />(.*?)</
	print $1 + '" x1="'
      else if x =~ /ongitude/
        x =~ />(.*?)</
	print $1 + '" y2="'
        end
      end
    end 


  f =~ /<cyc:toLocation><airport:Airport rdf:resource='(.*?)'/
  airport = $1


  airport
    airport =~ /airport.(...)/
    to = $1

    iatato = '/cgi-bin/airport?' + to

  # get airport info from the Web
  # this should be a seperate module, and should only happen for data that isn't already available
  # also, needs to get the right namespace for lat/long info that it gets

    resp, data = airports.get(iatato, nil)
    airportData = data.split(/\n/)
    airportData.each do |x|
      if x =~ /atitude/
        x =~ />(.*?)</
	print $1 + '" x2="'
      else if x =~ /ongitude/
        x =~ />(.*?)</
	print $1 + '"/></g>'
	puts '<!-- more to go here... -->'
        end
      end
    end  
  end
# END

end

puts "    </g>"
puts "  </g>"
puts "</svg>"
