#! /usr/bin/perl

sub error {
    print STDERR "error on $ARGV:$. - $_\n";
}

sub tail_error {
    local($linenum) = @_;

    print STDERR "error on $ARGV:$linenum - $InputLine[$linenum]\n";
}


$_ = <>;	m:^<HTML>$: || &error;
print $_;
$_ = <>;	m:^<HEAD>$: || &error;
print $_;
$_ = <>;	(($title) = m:^<TITLE> *(.*) *</TITLE>$:) || &error;
print $_;
$_ = <>;	m:^</HEAD>$: || &error;
print $_;
$_ = <>;	m:^<BODY>$: || &error;
print $_;
$_ = <>;	m!^<B>Connected: An Internet Encyclopedia</B>$! || &error;
print $_;
$_ = <>;	m:^<BR>$: || &error;
print $_;
$_ = <>;	(($title2) = m:^<EM> *(.*) *</EM>$:) || &error;
($title2 eq $title) || &error;
print $_;
$_ = <>;	m:^<HR>$: || &error;
print $_;
$_ = <>;	m:^<CENTER>$: || &error;
print $_;
$_ = <>;	m!^<B>Top:</B> <A HREF="/Connected/index.html">Connected: An Internet Encyclopedia</A>$! || &error;
print $_;
$_ = <>;

$up_count = 0;
while (($up_url, $up_name) = m!^<BR><B>Up:</B> *<A HREF="([^"]*)">(.*)</A>$!) {
    $up_url[$up_count] = $up_url;
    $up_name[$up_count] = $up_name;
    $up_count ++;
    print $_;
    $_ = <>;
}

		m:^</CENTER>$: || &error;

print $_;
$_ = <>;

if ($title =~ m/^RFC [0-9]+$/) {
  if (m:^<FORM METHOD="POST" ACTION="/Connected/RFC/search.cgi">$:) {
    $searchForm = "yes";
    print $_;
    $_ = <>;	m:^<TABLE>$: || &error;
    print $_;
    $_ = <>;	(($searchRFC) = m!^<TR><TD><B>Search:</B> <TD><INPUT NAME="SEARCH_STRING"> <INPUT TYPE=HIDDEN NAME="RFC" VALUE="([0-9]+)">$!) || &error;
    "RFC $searchRFC" eq $title || &error;
    print $_;
    $_ = <>;	m!^<TD><B>Search Depth:</B> <TD><SELECT NAME="DEPTH"><OPTION SELECT>1</SELECT>$! || &error;
    print $_;
    $_ = <>;	$_ eq "<TR><TD><B>Search Type:</B><TD><INPUT TYPE=\"RADIO\" NAME=\"TYPE\" VALUE=\"STRING\" CHECKED>String <INPUT TYPE=\"RADIO\" NAME=\"TYPE\" VALUE=\"REGEX\">RegEx\n" || &error;
    print $_;
    $_ = <>;	$_ eq "<TD><B>Search Options:</B> <TD><INPUT TYPE=\"CHECKBOX\" NAME=\"CASE_INDEPENDANT\" CHECKED>NoCase <INPUT TYPE=\"CHECKBOX\" NAME=\"WHOLE_WORD\" CHECKED>WholeWord\n" || &error;
    print $_;
    $_ = <>;	m:^</TABLE>$: || &error;
    print $_;
    $_ = <>;	m:^</FORM>$: || &error;
    print $_;
    $_ = <>;
  } else {
    ($rfc) = ($title =~ m/^RFC ([0-9]+)$/);
    print "<FORM METHOD=\"POST\" ACTION=\"/Connected/cgi-bin/search.cgi\">
<TABLE>
<TR><TD><B>Search:</B> <TD><INPUT NAME=\"SEARCH_STRING\"> <INPUT TYPE=HIDDEN NAME=\"BASE_URL\" VALUE=\"/Connected/RFC/$rfc/index.html\">
<TD><B>Search Depth:</B> <TD><SELECT NAME=\"DEPTH\"><OPTION>0<OPTION SELECTED>1<OPTION>2</SELECT>
<TR><TD><B>Search Type:</B><TD><INPUT TYPE=\"RADIO\" NAME=\"TYPE\" VALUE=\"STRING\" CHECKED>String <INPUT TYPE=\"RADIO\" NAME=\"TYPE\" VALUE=\"REGEX\">RegEx
<TD><B>Search Options:</B> <TD><INPUT TYPE=\"CHECKBOX\" NAME=\"CASE_INDEPENDANT\" CHECKED>NoCase <INPUT TYPE=\"CHECKBOX\" NAME=\"WHOLE_WORD\" CHECKED>WholeWord
</TABLE>
</FORM>\n";
  }
}


# These conditional constructs are for handling the various combinations
# of links that can appear after "Up".  We can have:
#   1. nothing
#   2. PREV
#   3. NEXT
#   4. PREV BR NEXT
# In any case, we have already read a line ahead, and end having read a line
# ahead, so the <HR><P> match doesn't need a $_=<> paired with it

if (($prev_url, $prev_name)=m!^<B>Prev:</B> *<A HREF="([^"]*)"> *(.*) *</A>$!){
    print $_;
    $_ = <>;
    goto end_of_conditionals if (! m:^<BR>$:);
    print $_;
    $_ = <>;
}

if (($next_url, $next_name)=m!^<B>Next:</B> *<A HREF="([^"]*)"> *(.*) *</A>$!){
    print $_;
    $_ = <>;
}

end_of_conditionals:
m:^<HR><P>$: || &error;
print $_;



# Read the rest of the file, storing the lines, so we can check the ending
# by going backwards from EOF

while (<>) {
    print $_;
    $InputLine[$.] = $_;
}


# Check the ending

$InputLine[$.]   =~ m:^</HTML>$: || &tail_error($.);
$InputLine[$.-1] =~ m:^</BODY>$: || &tail_error($.-1);
(($title3) = ($InputLine[$.-2] =~ m:^<EM> *(.*) *</EM>$:)) ||
    &tail_error($.-2);
($title3 eq $title) || &tail_error($.-2);
$InputLine[$.-3] =~ m:^<BR>$: || &tail_error($.-3);
$InputLine[$.-4] =~ m!^<B>Connected: An Internet Encyclopedia</B>$! ||
    &tail_error($.-4);

# The trailer is also conditional, but simpler than the header.
# All it can have is a NEXT

if ($InputLine[$.-5] =~ m:^<HR>$:) {
    $InputLine[$.-6] =~ m:^</CENTER>: || &tail_error($.-6);
    (($next2_url, $next2_name) = ($InputLine[$.-7] =~ m!^<B>Next:</B> *<A HREF="([^"]*)"> *(.*)</A>$!)) || &tail_error($.-7);
    $InputLine[$.-8] =~ m:^<CENTER>$: || &tail_error($.-8);
    $InputLine[$.-9] =~ m:^<P><HR>$: || &tail_error($.-9);
} else {
    $InputLine[$.-5] =~ m:^<P><HR>$: || &tail_error($.-5);
}

($next2_url eq $next_url) || &tail_error($.-7);
($next2_name eq $next_name) || &tail_error($.-7);

# print "$ARGV	Title	$title\n";
# if ($searchForm eq "yes") {
#     print "$ARGV	SearchForm\n";
# }
# for ($i=0; $i < $up_count; $i ++) {
#     print "$ARGV	Up	$up_name[$i]	$up_url[$i]\n";
# }
# print "$ARGV	Prev	$prev_name	$prev_url\n" if ($prev_url ne "");
# print "$ARGV	Next	$next_name	$next_url\n" if ($next_url ne "");
# print "\n";
