#!/usr/bin/perl -w

# $Id: resolve,v 1.2 2007-05-24 16:16:57 mike Exp $
#
# This is the front end for Keystone Resolver when invoked either as
# an OpenURL 0.1 resolver, or as a Z39.88 (OpenURL 1.0) resolver using
# any of the transports
#	info:ofi/tsp:http:openurl-by-val
#	info:ofi/tsp:http:openurl-by-ref
#	info:ofi/tsp:http:openurl-inline
# or their secure HTTPS equivalents, as defined in Part 1, section
# 15.1 of the standard, and as described in Part 2, section 9.  It
# works using both HTTP GET and HTTP POST.
#
# The classes that do the actual work are designed to work with other
# Z39.88 transports too, but none have been defined yet.

use strict;
use warnings;
use CGI;
use Keystone::Resolver;

my $cgi = new CGI();
if (0) {
    ### Temporary measure.  We will want better logging down the line.
    my $file = "/usr/local/src/cvs/resolver/samples/openurls/samples";
    open F, ">>$file"
	or die "can't write to OpenURL log file '$file': $!";

    print F $cgi->query_string(), "\n";
    close F;
}

binmode STDOUT, ":utf8"; # For proper output
binmode STDERR, ":utf8"; # For any temporary debugging output
my $referer = $ENV{HTTP_REFERER} || "http://made.up.referrer/foo";
my $openURL = Keystone::Resolver::OpenURL->newFromCGI($cgi, $referer);
eval { print $openURL->resolve() };
print "Content-type: text/plain\r\n\r\nUh-oh!\n$@" if $@;
