#!/usr/bin/perl

## refdbsru: CGI script providing SRU access to RefDB data
##
## usage: install in the CGI directory of your web server
## todo: how to adapt web server config
##
## Dependencies: perl 5.6.0 or later
##               CGI.pm
##               RefDB::SRU
##               RefDB::Log
##               RefDB::Pref
##
## markus@mhoenicka.de 2007-02-07

##   This program is free software; you can redistribute it and/or modify
##   it under the terms of the GNU General Public License as published by
##   the Free Software Foundation; either version 2 of the License, or
##   (at your option) any later version.
##   
##   This program is distributed in the hope that it will be useful,
##   but WITHOUT ANY WARRANTY; without even the implied warranty of
##   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##   GNU General Public License for more details.
   
##   You should have received a copy of the GNU General Public License
##   along with this program; if not, see <http://www.gnu.org/licenses/>


## some RefDB modules
use RefDB::Prefs;
use RefDB::SRU;

use Sys::Syslog;

use CGI;

## read config file settings
my $confdir = "<sysconfdir>";
my $prefs = RefDB::Prefs::->new("$confdir/refdbsrurc", undef);

# CGI does all the magic related to reading the query
my $query = new CGI;

# this hash receives the parameter-value pairs
my %params = $query->Vars;

# logging options
$params{'logfile'} = (defined($prefs->{"logfile"})) ? $prefs->{"logfile"} : "/var/log/refdbsru.log";
$params{'loglevel'} = (defined($prefs->{"loglevel"})) ? $prefs->{"loglevel"} : 6;
$params{'logdest'} = (defined($prefs->{"logdest"})) ? $prefs->{"logdest"} : 2; ## 0 = stderr, 1 = syslog, 2 = file

# networking options
$params{'server_ip'} = (defined($prefs->{"serverip"})) ? $prefs->{"serverip"} : "127.0.0.1";
$params{'port'} = (defined($prefs->{"port"})) ? $prefs->{"port"} : "9734";
$params{'timeout'} = (defined($prefs->{"timeout"})) ? $prefs->{"timeout"} : "180";

# user authentication
$params{'username'} = (defined($prefs->{"username"})) ? $prefs->{"username"} : "anonymous";
$params{'password'} = (defined($prefs->{"passwd"})) ? $prefs->{"passwd"} : "";


$params{'pdfroot'} = (defined($prefs->{"pdfroot"})) ? $prefs->{"pdfroot"} : "/home/foo/literature";
$params{'xsl_url'} = (defined($prefs->{"xsl_url"})) ? $prefs->{"xsl_url"} : "";
$params{'db_engine'} = (defined($prefs->{"dbserver"})) ? $prefs->{"dbserver"} : undef;

# zeerex parameters
$params{'zeerex_host'} = (defined($prefs->{"zeerex_host"})) ? $prefs->{"zeerex_host"} : "www.change.me";
$params{'zeerex_port'} = (defined($prefs->{"zeerex_port"})) ? $prefs->{"zeerex_port"} : "80";
$params{'zeerex_database'} = (defined($prefs->{"zeerex_database"})) ? $prefs->{"zeerex_database"} : "refs";
$params{'zeerex_databaseInfo_title'} = (defined($prefs->{"zeerex_databaseInfo_title"})) ? $prefs->{"zeerex_databaseInfo_title"} : "Reference Database";
$params{'zeerex_databaseInfo_description'} = (defined($prefs->{"zeerex_databaseInfo_description"})) ? $prefs->{"zeerex_databaseInfo_description"} : "Reference Database";
$params{'zeerex_databaseInfo_author'} = (defined($prefs->{"zeerex_databaseInfo_author"})) ? $prefs->{"zeerex_databaseInfo_author"} : "John Doe";
$params{'zeerex_databaseInfo_contact'} = (defined($prefs->{"zeerex_databaseInfo_contact"})) ? $prefs->{"zeerex_databaseInfo_contact"} : "John\@Doe.org";

# create a new RefDB query object and pass a reference to the
# parameter-value hash 
my $refdbquery = new RefDB::SRU(\%params);

# try to figure out the database name. A default database should be
# provided in the config file. Remote users can override this setting
# by providing an additional path info in the URL. E.g.
# http://myserver.com/cgi-bin/refdbsru/?<query> will use the
# default database, whereas
# http://myserver.com/cgi-bin/refdbsru/foo?<query> will use the
# database "foo" instead
if (defined($query->path_info()) && length($query->path_info()) > 1) {
    $params{'database'} = $query->path_info();
    
    # strip leading slash
    $params{'database'} =~ s/^\///;
}
else {
    $params{'database'} = (defined($prefs->{"defaultdb"})) ? $prefs->{"defaultdb"} : "references";
}

$refdbquery->run();

my $result = $refdbquery->response();

print "Content-type: text/xml\n\n";
print "$result\n";

#$refdbquery->print_vars();



exit 0;
