#!/usr/bin/env ruby
#
# post something to an annotea server...
#
# this lives at http://www.w3.org/2001/sw/Europe/200209/annodemo/annopost.rb
version = '$Id: annopost.rb,v 1.4 2002/10/18 18:07:49 charles Exp $'
#
# License: This software is covered by the W3C Software License.
#	See http://www.w3.org/Consortium/Legal/copyright-software for details 
#
#
# todo: Hmmm. 3D flythrough view with marauding aliens and cool weapons ?? 
#	building the annotation if it is a file
#	

# set up the network thang...

require 'net/http'
require 'getoptlong'

# fixed data

my_debug = bodydata = authstring = bodyref =  nil
creator = "Anonymous Coward"
type = "Comment"
my_headers = {'Content-type' => 'application/xml' ,
	'User-agent' => 'chaalsToy/0.01a' } 
doit = "true"

usage = "Usage: annopost options <target> [<server>]
  Options:	( [-f <filename>] | [-r <resource>] ) [-c <creator>] [-t <type>] | [-a annotation]
		[--usage|--help|-h]
		[-l|--auth <user:password>]
		[-n|--noexec]
		[-v|--debug]"


# get the options information

opts = GetoptLong.new(
  [ "--creator", 	"-c", 		GetoptLong::REQUIRED_ARGUMENT ],
  [ "--file", 		"-f", 		GetoptLong::REQUIRED_ARGUMENT ],
  [ "--usage", "--help", "-h", 		GetoptLong::NO_ARGUMENT ],
  [ "--auth", 		"-l", 		GetoptLong::REQUIRED_ARGUMENT ],
  [ "--noexec", 	"-n", 		GetoptLong::NO_ARGUMENT ],
  [ "--resource", 	"-r", 		GetoptLong::REQUIRED_ARGUMENT ],
  [ "--type", 		"-t", 		GetoptLong::REQUIRED_ARGUMENT ],
  [ "--debug", 		"-v", 		GetoptLong::NO_ARGUMENT ],
  [ "--version", 			GetoptLong::NO_ARGUMENT ]
)

opts.each do |opt, arg|
  case opt
  when "--debug"
    my_debug = "true"
  when "--file"
    bodydata = `cat #{arg.inspect}`
    # ought to make a full body...
  when "--type"
    type = arg.inspect
  when "--auth"
    my_headers['Authorization'] = 'Basic ' + ["#{arg.inspect}"].pack('m').strip
  when "--resource"
    bodyref = arg.inspect
  when "--usage"
    puts usage
    doit = false
  when "--version"
    puts version
    doit = false
  when "--noexec"
    my_debug = "true"
    doit = false
  end
end

# read the command line
target = ARGV[0]
server = (ARGV[1] || 'http://annotest.w3.org/annotations')

# 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 }

# format the annotation

data = `ruby annomake.rb #{target} #{bodyref} "#{creator}" #{type}` unless (data = bodydata)

# do the work, give some feedback

if (doit)
  annoserver = Net::HTTP.new(uri['server'], uri['port'])
  resp, newdata = annoserver.post(uri['path'], data, my_headers, "")
  print resp.code , "  " , resp.message , "\n"
end

# provide further feedback in debug mode

if (my_debug == 'true')
  puts "debugging mode on"
  if (doit)
    puts "Server response header:"
    resp.each do|a , b| 
      print a , ": " , b , "\n"
    end
  end
  puts
  puts "target: " + target
  puts
  print "server: http://" , uri['server'] , ":" , uri['port'] , uri['path'] , "\n"
  if my_headers['Authorization']
    print "Authorisation hash: " , my_headers['Authorization']
  puts
  end
  puts "Annotation submitted"
  puts data
end

