#!/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/crunchflights.rb
# sample data file is at http://www.w3.org/2002/08/flight/flights
# sample output at http://www.w3.org/2002/08/flight/out.rdf
# another sample output at http://www.w3.org/2002/08/flight/out.rdf
#
#
# convert flights data in format
#
#  9. UA  863 H  12AUG SFOMEL HK1  2305  *0930  O*       E MO
#
#	to RDF 
#
# Usage:
#    > ruby crunchflights.rb foo
# where foo is the filename of the data. outputs to stdout
# 
# output can be fed to script at http://www.w3.org/2002/08/flight/mapflights.rb to make a map
#
# todo: error checking on the input data - right now it must be correctly formatted
#	match the output to DanCon's
#	use ruby date module
#	watch JibberJim reimplement it cleaner and nicer in about 25 minutes (mostly done ;-)



# convert months to numbers. This should be replaced by using ruby date module
monthNo = {
  'JAN' => '01',
  'FEB' => '02',
  'MAR' => '03',
  'APR' => '04',
  'MAY' => '05',
  'JUN' => '06',
  'JUL' => '07',
  'AUG' => '08',
  'SEP' => '09',
  'OCT' => '10',
  'NOV' => '11',
  'DEC' => '12'
}

file = ARGV[0]

data = `cat #{file}` #err

puts "<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
	xmlns='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";

# split the data and process each line

flights = data.split(/\n/)
 
flights.each do |f|

  f.chomp!
  i = f.split /\s+/

  i[6] =~  /(...)(...)/
  from = $1
  to = $2

  puts "<Flight>\n"
  puts "<carrier>" + i[2] + "</carrier>  <flightNumber>"+ i[3] +"</flightNumber>\n"
  puts "<cyc:fromLocation><airport:Airport rdf:resource='http://www.daml.org/cgi-bin/airport?" + from + "' /></cyc:fromLocation>\n"
  puts "<cyc:toLocation><airport:Airport rdf:resource='http://www.daml.org/cgi-bin/airport?" + to + "'/></cyc:toLocation>"

  # split up dates. Re-do this with date module

  i[5] =~ /(..)(...)/
  date = $1
  month = $2

  i[8] =~ /(..)(..)/
  depH = $1
  depM = $2

  # check if arrival is a day or two after departure
  # this is rough - it will accept the idea of arriving on 33 december...

  if i[9] =~ /(.)(..)(..)/
    if $1 == '*'
      arrD = date.to_i + 2
    else
      arrD = date.to_i + 1
    end
    arrD = arrD.to_s
    arrH = $2
    arrM = $3
  else i[9] =~/(..)(..)/
    arrD = date
    arrH = $1
    arrM = $2
  end

  print "<departureTime><dcterms:W3CDTF><rdf:value>2002-" + monthNo[month] + "-" + date
  print "T" + depH + ":" + depM + "</rdf:value></dcterms:W3CDTF></departureTime>\n"
  print "<arrivalTime><dcterms:W3CDTF><rdf:value>2002-" + monthNo[month] + "-" + arrD
  print "T" + arrH + ":" + arrM + "</rdf:value></dcterms:W3CDTF></arrivalTime>\n"
  puts "</Flight>\n\n"

end
print "</rdf:RDF>\n\n"


