#!/usr/bin/perl -w

# Convert ISBN to Library of Congress Call Number using their Z39.50 interface.
# This script requires the following modules or apps, either directly or
# indirectly through dependencies:
#  + yaz, libyaz, libyaz-devel
#  + Search-Z3950-0.05
#  + Net-Z3950-0.44
#  + Event-1.00
#  + YAML-0.35
#  + MARC-Record-1.38

# by Steve Bonds
# June 12 2004
# Copyright 2004, all rights reserved.

# This is Free Software.  You may redistribute this script under the terms of
# either the GNU General Public License version 2.0 or the Perl
# Artistic License.

use Search::Z3950;

my ($isbn) = shift;

unless ($isbn =~ /\S+/) { die "Please provide ISBN" };

# I could do some data sanity checks on the ISBN, but if it's not valid, the
# DB query will probably fail later.

$z = Search::Z3950->new(
			'delay' => 0.5,
			'preferredRecordSyntax' => 19,
			'port' => 7090,
			'host' => 'z3950.loc.gov',
			'databaseName' => 'Voyager',
			'search_types' => [
					   {
					    'name' => 'isbn',
					    'multiple' => 0,
					    'templates' => {
							    'prefix' => '@attr 1=7 @attr 4=1 "%s"'
							   }
					   },
					  ],
			'querytype' => 'prefix',
		       );

my $results = $z->search('isbn' => $isbn);
my $MARC = $results->record(1);
#my $title = $MARC->field('245')->subfield("a");
#print "Title: $title\n";
#my $author = $MARC->field('100')->subfield("a");
#print "Author: $author\n";
my $lccn = $MARC->field('050')->subfield("a");
print "$lccn\n";
