# phone2.pl
#!/bin/perl

$PHONEBOOK="/export/home/httpd/cgi-bin/Phone.txt";

$CGIPATH="http://www.csnet.net/cgi-bin/";
eval `$CGIPATH/test/cgiparse -form -prefix $`;

# Determine the age of the phone book
$mod_date=int(-M $PHONEBOOK);	

#Display the age of the phone book and generate the search form
print <<EOM;
Content-type: text/html

<TITLE>Phone Book Search</TITLE>
<BODY>
<H1>Phone Book Search</H1>
The phone book was updated $mod_date days ago.<p>	
<FORM ACTION="/cgi-bin/phone2.pl" METHOD="POST">
Search for: <INPUT TYPE="TEXT" NAME="QUERY"> 
<INPUT TYPE="SUBMIT" VALUE="SEARCH">
</FORM>
<p><hr><p>
EOM

# Do the search only if a query was entered
if (length($query)>0) {	
  print <<EOM;
Search for <B>$query</B> yields these entries:
<PRE>
EOM

#Inform user if search is unsuccessful
$answer = `grep -i $query $PHONEBOOK`;
if (!$answer) { print "Search was unsuccessful\n" ;}
else { print $answer\n" ; }

print <<EOM;
</PRE>
</BODY>
EOM
}
