#!/usr/bin/perl

require "cgi-lib.pl";

&ReadParse(*in);

$prefix="http://www.webwitness.org.au/smr/";

#print "Content-type: text/plain\r\n\r\n";

if (!dbmopen(PAGES, "pagesdb", 0644) ||
    !$PAGES{1} || $in{"page"} eq "makedb")
{
 # Makes the dbm index file
 $made=1;
 open(PP, "pages");
 dbmopen(PAGES, "pagesdb", 0644);
 while (<PP>)
 {
  #print;
  chop;
  ($page, $file)= split(/:/);
  #print "<<$page>><<$file>>\n";
  $PAGES{$page}=$file;
 }
 dbmclose(PAGES);
}

#dbmopen (PAGES, "pagesdb", 0644) || die "dbmopen";
if (!dbmopen(PAGES, "pagesdb", 0644))
{
 print "Content-type: text/plain\r\n\r\n";
 print "Couldn't open database: $!\n";
 exit(0);
}

$location=$PAGES{$in{"page"}};

if (!$location)
{
 print <<'EOF';
Content-type: text/html

<HTML>
<HEAD><TITLE>Not found</TITLE></HEAD>
<BODY><H1>Not found</H1>
The page of <B>The Shadow of a Mighty Rock</B> you requested could not be located.
</P>
</HTML>
EOF
}
else
{
 #$location =~ s/html/htm/;
# print "HTTP/1.0 302 Found\r\n";
 print "Content-type: text/html \r\n";
 print "Location: $prefix$location#PAGE$in{page}\r\n\r\n";

 print <<"EOF"
<html>
<HEAD><TITLE>Document Moved</TITLE></HEAD>
<BODY><H1>Document moved</H1>
The document has moved <A HREF="$prefix$location#PAGE$in{page}">here</A>.<P>
</BODY>
EOF
}

