#!/usr/bin/perl

# Copyright (C) 2002 Rodrigo Araujo Real
     
# 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, 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 GNU Emacs; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.    
#
# This file is neither part of XEmacs nor GNU Emacs. 

use HTTP::Request::Common;
use LWP::UserAgent;

$Ua = LWP::UserAgent->new;
$Ua->agent("Mozilla (compatible: LWP $LWP::VERSION)");

$res = $Ua->request(POST 'http://liinwww.ira.uka.de/searchbib/index', [query => $ARGV[0], results => "bibtex"]);
die "Error while getting ", $res->request->uri,
          " -- ", $res->status_line, "\nAborting"
         unless $res->is_success;

$str=$res->content;
# extracts everything before the first <pre>
$str=~s/^.*?<pre>//s;

# extracts everything before the last </pre>
$str=~s/^(.*)<\/pre>.*/\1/s;

# extracts everything between </pre> and <pre>
$str=~s/<\/pre>.*?<pre>//gs;

# extracts any other unwanted HTML tags
$str=~s/<.*?>//gs;

if ($str =~ /Sorry, no references found/) {
    print "------------> No references found for $ARGV[0] <------------\n";
    exit(0);
}

print "------------> Search Expression: $ARGV[0] Results <------------\n";
print "$str";

exit(0);

__END__

=head1 NAME

search-ccsb

=head1 SYNOPSIS

search-ccsb <expression>

=head1 DESCRIPTION

This Perl script helps searching the "The Collection of Computer
Science Bibliographies" provided by Alf-Christian Achilles
<achilles@ira.uka.de>. With it you can perform a query without
browser, directly from the command line.

=head1 EXAMPLE

search-ccsb "operating systems"

=head1 CONTRIBUTORS

Mario Domenech Goulart, Otavio Salvador

=head1 SEE ALSO

The Collection of Computer Science Bibliographies
http://liinwww.ira.uka.de/bibliography/index.html

=head1 COPYRIGHT

Copyright 2002 Rodrigo Araujo Real.

=cut
