#!/usr/local/bin/perl

# gateway from gopher to archie
# this is the "brute force" kind of approach; a tidier solution
# would speak the prospero protocols directly.

$archiehost = mudhoney.micro.umn.edu
$archieport = 7999

$archie = "archie.sura.net";
$type = "exact";
$query = <>;
chop($query);
chop($query);
$test = $query;

@DaytoMon  = ("", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");


if ($test =~ m/000000(.*)\`(.*)\`(.*)\`(.*)$/) {
	$time = $4;
	$size = $3;
	$host = $1;
	$file = $2;

	$year = substr($time, 0, 4);
	$month = substr($time, 4, 2);
	$mon  = @DaytoMon[$month];
	$day   = substr($time, 6, 2);
	
	print "Your archie query on $archie found the following matching\r\n";
	print "file or directory:\r\n\r\n";
	print "   Host: $host\r\n";
	print "   Name: $file\r\n";
	print "   Size: $size  (in bytes)\r\n";
	print "   Date: $mon $day, $year\r\n";
	print "\r\n";
	print "To retrieve this file you would use your local ftp client\r\n";
	print "to connect to host $host.  \r\n";
	print "\r\n";
	print ".\r\n";
	exit;
}
	
if ($type eq 'exact') {
	$arg = " -e ";
} else {
	$arg = " -s ";
}

$archcmd = "/usr/local/bin/archie -l -t " . $arg . " -h " . $archie . " " . $query;
@result = `$archcmd`;
#print $archcmd;
foreach (@result) {
	($time, $size, $host, $file) = split;
	print "0$host\t000000$host`$file`$size`$time\t$archiehost\t$archieport\r\n";
}
	
print ".\r\n";
exit;

