#!/usr/bin/env ruby
#
# get annotations
#
# this lives at http://www.w3.org/2001/sw/Europe/200209/annodemo/annoget.rb
# 	$Id: annoget.rb,v 1.7 2002/10/18 18:08:08 charles Exp $
#
# usage: annoget.rb <annotated document> [<annotation server> [<username> [<password>]]]
#
# todo: make it accept options for username/password insteaed of strict order
#	make it useful by having something to feed the output to
#	turn all these pieces into a class
#
# License: This software is covered by the W3C Software License.
#	See http://www.w3.org/Consortium/Legal/copyright-software for details 


# set up the network thang...

require 'net/http'

# read the command line

target = ARGV[0]
server = (ARGV[1] || 'http://annotest.w3.org:80/annotations')

username = (ARGV[2] || '')
password = (ARGV[3] || '')

# interpret the server information

if server =~ /:\/\/([^:\/]+):([^\/]+)(\/.*)$/
  host = $1
  port = $2
  res = $3
  else 
  server =~ /:\/\/([^\/]+)(\/.*)$/
  host = $1
  res = $2
  port = '80'
end

uri = { 'server' => host , 'path' => res , 'port' => port.to_i }

authstring = 'Basic ' + ["#{username}:#{password}"].pack('m').strip

# add the magic query string to the path.

path = uri['path'] + '?w3c_annotates=' + target

# some things that are fixed data

my_headers = {'Accept' => 'application/xml' ,
	'Authorization' => authstring ,
        'User-agent' => 'chaalsToy/0.01a' } 
#'application/rdf+xml'  is what it should be

# Do the work: make connection, get the data, and print it out

annoserver = Net::HTTP.new(uri['server'], uri['port'])

resp, data = annoserver.get(path, my_headers)
puts data
# if debug puts uri['server'] , uri['path'] , uri['port']