#!/usr/bin/perl

use CGI::Carp 'fatalsToBrowser';

my $target = lc($ENV{QUERY_STRING});
my (%links, $a, $b);

open( LINKS, "/home/djh/public_html/toldme/links.txt" ) ||
    die "open: /home/djh/public_html/toldme/links.txt: $!\n";

while( <LINKS> ) {
	($a, $b) = split;
	$links{lc($a)} = $b;
}

open( DYNLINKS, "/home/djh/public_html/toldme/dynlinks.txt" ) ||
    die "open: /home/djh/public_html/toldme/dynlinks.txt: $!\n";

while( <DYNLINKS> ) {
	($a, $b) = split;
	$links{lc($a)} = $b;
}

if( $links{$target} ) {
	if( $links{$target} !~ /^(http|ftp|mailto):/ ) {
		$links{$target} =~ s/^/http:\/\/$ENV{HTTP_HOST}/;
	}
	print "Location: $links{$target}\n\n";
	} else {
		die "Dang!  $target not found!";
}
