#!/usr/bin/perl

use CGI qw(:standard);
use URI::Escape;
require 'w3csearchprinting.lib';
require 'vocab.lib';
use strict;

use TAP::SOAP;
use TAP::Client;

my $client = new TAP::Client('http://localhost/data/');
setClient($client);

my $query = param("q");
if (not $query) {
    $query = "web services";
}
my $equery = uri_escape($query);

my @tapans = $client->GetResourcesNamed($query)->asStrings();

my $kbHomePage = "http://www.w3.org/2002/05/tap/sembrowse/";
setKB($kbHomePage);

my $semsearchPage = "http://www.w3.org/2002/05/tap/semsearch/";

my $searchPage = "http://www.google.com/";

# HTML constructs

my $SearchHeader = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta name="generator"
    content="HTML Tidy for Linux/x86 (vers 1st March 2002), see www.w3.org" />

    <title>Semantic Search:' . $query . '</title>
    <base href="' . $searchPage . '" />

<style type="text/css">
/*<![CDATA[*/

body,td,div,.p,a { 
  font-family:arial,sans-serif
}

div,td { 
  color:black 
}

.f,.fl:link { 
  color:#6f6f6f 
  } 

a:link,.w,a.w:link,.w a:link { 
  color:#000099
  }

a:visited,.fl:visited { 
  color:#660066 
  }

a:active,.fl:active { 
  color:#ff3300
  }

.t a:link,.t a:active,.t a:visited,.t { 
  color:#ffffff
  }

.t {
    background-color:#000099
    }

.h { 
  color:#000; font-size:14px
  }

.i,.i:link { 
  color:#a90a08
  }

.a,.a:link { 
  color:#008000
  }

.z { 
  display:none
  }

div.n { 
    margin-top: 1ex
    }

.n a { 
    font-size:10pt; 
  color:black
  }

.n .i {
    font-size:10pt; 
    font-weight:bold
    }

.q a:visited,.q a:link,.q a:active,.q {
    text-decoration: none; 
  color: #000099;
} 

.b {
    font-size: 12pt; 
  color:#000099; 
    font-weight:bold
    }

/*]]>*/
    </style>
    </head>

    <body bgcolor="white" text="black" link="#000099" vlink="#660066"
    alink="#FF3300">
    <div align="left">
    <img alt="Web Search" border="0"
    src="http://www.w3.org/Icons/w3c_home" height="48"
    width="72" /> 

    <table summary="wrapper table" border="0">
    <tr bgcolor="#ffffff">
    <td>
    <table summary="inner table" border="0" cellpadding="0"
    cellspacing="0">
    <tr>
    <td rowspan="3" valign="top"><a href="/"><img
    border="0"
    src="http://www.google.com/images/Logo_60wht.gif"
    width="176" height="77" alt="Go to Google Home" />
    </a> &nbsp;&nbsp;&nbsp;</td>

    <td nowrap="nowrap"><font size="-1"><a
    href="/help/">Search&nbsp;Tips</a>&nbsp;</font></td>
    </tr>

    <tr>
    <td valign="middle">
    <form name="gs" method="get"
    action="' . $semsearchPage . '">
    <input type="text" name="q" size="20"
    maxlength="204" value="' . $query . '" /> <font
    size="-1"><input type="submit" name="btnG"
    value="Search" /></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b><font size="+2"><em>Semantics by <a
    href="http://tap.stanford.edu/">TAP</a></em></font></b>
    </form>
    </td>
    </tr>

    <tr>
    <td><font size="-1"><input type="radio"
    name="sitesearch" value="" />Search&nbsp;WWW <input
    type="radio" name="sitesearch" value="w3.org"
    checked="checked" />Search&nbsp;w3.org&nbsp;</font></td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    </div>

    <table summary="results" width="100%" border="0"
    cellpadding="2" cellspacing="0">
    <tr>
    <td bgcolor="#000099" nowrap="nowrap"><font size="-1"
    color="#ffffff">Searched pages from <b>w3.org</b> 
    ';


my $adBlockStart = "\<table border=0 cellpadding=0 cellspacing=4 align=right width=20%\>";
#    my $adBlockStart = "\<div\>";
my $adBlockEnd = "here...\<\/a\>\<\/font\>\<\/td\>\<\/tr\>\<\/table\>";

my $alltappre = "<table border=0 cellpadding=0 cellspacing=4 align=right width=20%><tr><td align=center><br><span class=f><font size=-1></font></span><tr><td>";
my $alltappost = "<tr><td align=center><font size=-1></font></td></tr></table>";

my $tappre = "<table bgcolor=#DDB7BA border=0 cellspacing=0 width=100%><tr><td><table border=0 cellpadding=3 cellspacing=0 width=100%><tr><td bgcolor=#FFF5F6><font size=-1>";
my $tappost = "</td></tr></table></td></tr></table>";


my $SearchFooter = '
    <br clear="all" />
    </font>

    <div class="n">
    <center>
    <br clear="all" />
    <br />

    <table border="0" cellpadding="0" width="1%"
    cellspacing="0" align="center">
    <tr align="center" valign="top">
    <td valign="bottom" nowrap="nowrap"><font
    size="-1">Result&nbsp;Page:&nbsp;</font></td>

    <td><img src="/nav_first.gif" width="18"
    height="26" alt="" /><br />
    </td>

    <td><img src="/nav_current.gif" width="16"
    height="26" alt="" /><br />
    <span class="i">1</span></td>

    <td><a
    href="' . $semsearchPage . "q=?" . $equery . '">
    <img src="/nav_page.gif" width="16" height="26"
    alt="" border="0" /><br />
    2</a></td>

    <td><a
    href="/custom?q=' . $equery . '&amp;hl=en&amp;lr=&amp;ie=UTF-8&amp;cof=L:http://www.w3.org/Icons/w3c_home;LH:48;LW:72;BGC:white;T:black;LC:%23000099;VLC:%23660066;ALC:%23ff3300;AH:left;&amp;domains=w3.org&amp;sitesearch=w3.org&amp;start=20&amp;sa=N">
    <img src="/nav_page.gif" width="16" height="26"
    alt="" border="0" /><br />
    3</a></td>

    <td><a
    href="/custom?q=' . $equery . '&amp;hl=en&amp;lr=&amp;ie=UTF-8&amp;cof=L:http://www.w3.org/Icons/w3c_home;LH:48;LW:72;BGC:white;T:black;LC:%23000099;VLC:%23660066;ALC:%23ff3300;AH:left;&amp;domains=w3.org&amp;sitesearch=w3.org&amp;start=30&amp;sa=N">
    <img src="/nav_page.gif" width="16" height="26"
    alt="" border="0" /><br />
    4</a></td>

    <td><a
    href="/custom?q=' . $equery . '&amp;hl=en&amp;lr=&amp;ie=UTF-8&amp;cof=L:http://www.w3.org/Icons/w3c_home;LH:48;LW:72;BGC:white;T:black;LC:%23000099;VLC:%23660066;ALC:%23ff3300;AH:left;&amp;domains=w3.org&amp;sitesearch=w3.org&amp;start=40&amp;sa=N">
    <img src="/nav_page.gif" width="16" height="26"
    alt="" border="0" /><br />
    5</a></td>

    <td><a
    href="/custom?q=' . $equery . '&amp;hl=en&amp;lr=&amp;ie=UTF-8&amp;cof=L:http://www.w3.org/Icons/w3c_home;LH:48;LW:72;BGC:white;T:black;LC:%23000099;VLC:%23660066;ALC:%23ff3300;AH:left;&amp;domains=w3.org&amp;sitesearch=w3.org&amp;start=50&amp;sa=N">
    <img src="/nav_page.gif" width="16" height="26"
    alt="" border="0" /><br />
    6</a></td>

    <td><a
    href="/custom?q=' . $equery . '&amp;hl=en&amp;lr=&amp;ie=UTF-8&amp;cof=L:http://www.w3.org/Icons/w3c_home;LH:48;LW:72;BGC:white;T:black;LC:%23000099;VLC:%23660066;ALC:%23ff3300;AH:left;&amp;domains=w3.org&amp;sitesearch=w3.org&amp;start=60&amp;sa=N">
    <img src="/nav_page.gif" width="16" height="26"
    alt="" border="0" /><br />
    7</a></td>

    <td><a
    href="/custom?q=' . $equery . '&amp;hl=en&amp;lr=&amp;ie=UTF-8&amp;cof=L:http://www.w3.org/Icons/w3c_home;LH:48;LW:72;BGC:white;T:black;LC:%23000099;VLC:%23660066;ALC:%23ff3300;AH:left;&amp;domains=w3.org&amp;sitesearch=w3.org&amp;start=70&amp;sa=N">
    <img src="/nav_page.gif" width="16" height="26"
    alt="" border="0" /><br />
    8</a></td>

    <td><a
    href="/custom?q=' . $equery . '&amp;hl=en&amp;lr=&amp;ie=UTF-8&amp;cof=L:http://www.w3.org/Icons/w3c_home;LH:48;LW:72;BGC:white;T:black;LC:%23000099;VLC:%23660066;ALC:%23ff3300;AH:left;&amp;domains=w3.org&amp;sitesearch=w3.org&amp;start=80&amp;sa=N">
    <img src="/nav_page.gif" width="16" height="26"
    alt="" border="0" /><br />
    9</a></td>

    <td><a
    href="/custom?q=' . $equery . '&amp;hl=en&amp;lr=&amp;ie=UTF-8&amp;cof=L:http://www.w3.org/Icons/w3c_home;LH:48;LW:72;BGC:white;T:black;LC:%23000099;VLC:%23660066;ALC:%23ff3300;AH:left;&amp;domains=w3.org&amp;sitesearch=w3.org&amp;start=90&amp;sa=N">
    <img src="/nav_page.gif" width="16" height="26"
    alt="" border="0" /><br />
    10</a></td>

    <td nowrap="nowrap"><a
    href="/custom?q=' . $equery . '&amp;hl=en&amp;lr=&amp;ie=UTF-8&amp;cof=L:http://www.w3.org/Icons/w3c_home;LH:48;LW:72;BGC:white;T:black;LC:%23000099;VLC:%23660066;ALC:%23ff3300;AH:left;&amp;domains=w3.org&amp;sitesearch=w3.org&amp;start=10&amp;sa=N">
    <img src="/nav_next.gif" width="100" height="26"
    alt="" border="0" /><br />
    <span class="b">Next</span></a></td>
    </tr>
    </table>

    <table border="0" cellpadding="3" cellspacing="0"
    align="center">
    <tr>
    <td nowrap="nowrap">

    <form method="get" action="/custom">
    <font size="-1"><input type="text" name="q"
    size="31" maxlength="2048"
    value="' . $query . '" /> <input type="submit"
    name="btnG" value="Google Search" /><input
    type="hidden" name="hl" value="en" /><input
    type="hidden" name="lr" value="" /><input
    type="hidden" name="ie"
    value="ISO-8859-1" /><input type="hidden"
    name="cof"
    value="L:http://www.w3.org/Icons/w3c_home;LH:48;LW:72;BGC:white;T:black;LC:#000099;VLC:#660066;ALC:#ff3300;AH:left;" />
    <input type="hidden" name="domains"
    value="w3.org" /><input type="hidden"
    name="sitesearch" value="w3.org" /> <a
    href="http://www.google.com/swr?q=' . $equery . '&amp;hl=en&amp;lr=&amp;ie=UTF-8&amp;cof=L:http://www.w3.org/Icons/w3c_home;LH:48;LW:72;BGC:white;T:black;LC:%23000099;VLC:%23660066;ALC:%23ff3300;AH:left;&amp;domains=w3.org&amp;sitesearch=w3.org&amp;search=custom&amp;swrnum=2590">
    Search&nbsp;within&nbsp;results</a></font>
    </form>

    </td>
    </tr>
    </table>


    <table width="100%" cellpadding="2" cellspacing="0"
    border="0">
    <tr>
    <td class="t" align="center"><font size="-1"><a
    href="/">Google&nbsp;Home</a> - <a
    href="/ads/">Advertise&nbsp;with&nbsp;Us</a> -
    <a href="/services/">Search&nbsp;Solutions</a>
    - <a href="/news/">News and Resources</a> - <a
    href="/language_tools?hl=en">Language Tools</a>
    - <a
    href="/about.html">Jobs,&nbsp;Press,&nbsp;Cool&nbsp;Stuff...</a></font></td>
    </tr>
    </table>

    <hr class="z" />
    <br />
    <font size="-1" class="p">&copy;2002 Google</font>
    </center>


    </div>
    </td>
    </tr>
    </table>
    </body>
    </html>

    ';


my $adBlockStart = "\<table border=0 cellpadding=0 cellspacing=4 align=right width=20%\>";
my $adBlockEnd = "here...\<\/a\>\<\/font\>\<\/td\>\<\/tr\>\<\/table\>";

my $alltappre = '
    <table summary="TAP pre" border=0 cellpadding=0 cellspacing=4 align=right width=20%>
    <tr>
    <td align=center><br /><span class=f><font size=-1></font></span>
    <tr>
    <td>';

my $alltappost = '
    <tr>
    <td align=center><font size="-1"></font>
    </td>
    </tr>
    </table>';

my $tappre = '
    <table summary="TAP pre" bgcolor="#DDB7BA" border=0 cellspacing=0 width=100%>
    <tr>
    <td>
    <table summary="TAP pre inner" border=0 cellpadding=3 cellspacing=0 width=100%>
    <tr>
    <td bgcolor=#FFF5F6><font size=-1>';

my $tappost = '
             </td>
           </tr>
         </table>
       </td>
     </tr>
   </table>';

my $pagepre = "";
my $pagepost = "";

sub getGoogleAns {
    my $q = uri_escape($query);
    my $buf = TAP::SOAP::Get("http://www.google.com/search?q=$q&as_sitesearch=w3.org");
    my ($pre, $ans) = split(/Searched pages from \<b\>w3\.org\<\/b\>/, $buf);
    my ($aans, $post) = split(/\<\/blockquote\>/, $ans);
    if ($aans =~ "See your message here") {
	my ($pp, $rr) = split(/$adBlockStart/, $aans);
	my ($ig, $en) = split(/$adBlockEnd/, $rr);
	$pagepre = $pp;
	$pagepost = $en;
    } else {
	my ($pp, $rr) = split(/\<div\>/, $aans);
	$pagepre = $pp;
	$pagepost = $rr;
    }
}

getGoogleAns($query);

print header;
print $SearchHeader;
print $pagepre;

my %taphash = ();
for my $item (@tapans) {
    $taphash{$item} = 1;
}

my @SpecialTypes = ("Person", 
		    "ActivityStatement", 
		    "Activity", 
		    "WorkingGroup", 
		    "InterestGroup", 
		    "CoordinationGroup", 
		    "Service", 
		    "Note", 
		    "WD", 
		    "PR", 
		    "REC", 
		    "MailingList", 
		    "News");

my %typePrefix = ("Person" => " ",
		  "Activity" => "<b> Activities:</b><br />",
		  "Service" => "<b> Services:</b><br />",
		  "WorkingGroup" => "<b> Groups:</b><br />",
#		  "CoordinationGroup" => "<b> Groups:</b><br />",
#		  "InterestGroup" => "<b> Groups:</b><br />",
		  "REC" => "<b> Recommendation:</b><br />",
		  "PR" => "<b> Proposed Recommendations:</b><br />",
		  "Note" => "<b> Notes:</b><br />",
		  "WD" => "<b> Working Drafts:</b><br />",
		  "MailingList" => "<b>Mailing Lists:</b><br />",
		  "News" => "<b>News / Events:</b><br />");

my %relatedTypePrefix = ("Person" => "<b>Related People</b><br />",
			 "Activity" => "<b>Related Activities:</b><br />",
			 "Service" => "<b>Related  Services:</b><br />",
			 "WorkingGroup" => "<b> Related Groups:</b><br />",
#			 "CoordinationGroup" => "<b> Related Groups:</b><br />",
#			 "InterestGroup" => "<b> Related Groups:</b><br />",
			 "REC" => "<b> Related<br />Recommendations:</b><br />",
			 "PR" => "<b> Related<br />Proposed Recommendations:</b><br />",
			 "Note" => "<b> Related Notes:</b><br />",
			 "WD" => "<b> Related Drafts:</b><br />",
			 "MailingList" => "<b>Related Mailing Lists:</b><br />",
			 "News" => "<b>Related News:</b><br />");


my %relans = ();

#for my $ans (@tapans) {
#    print "$ans<br/>";
#}

if ($#tapans eq -1) {
    print $pagepost;
} else {
    print $alltappre;

    for my $type (@SpecialTypes) {
	   my $preprinted = 0;

	   for my $ans (@tapans) {

	       if ($client->IsInstanceOf($ans, v($type))) {
		   if ($preprinted eq 0 and $typePrefix{$type}) {
		       print $tappre;
		       print $typePrefix{$type};
		       $preprinted = 1;
		   }

		   if ($type eq "Person") {
		       for my $it (formatPerson($ans, 1)) {
			   if (not ($taphash{$it})) {  $relans{$it} = 1; }
		       }
		   } elsif ($type eq "Activity") {
		       for my $it (formatActivity($ans, 1)) {
			   if (not ($taphash{$it})) {  $relans{$it} = 1; } 
		       }
		   } elsif ($type eq "ActivityStatement") {
		       for my $it (formatActivityStatement($ans, 1)) {
			   if (not ($taphash{$it})) {  $relans{$it} = 1; } 
		       }
		   } elsif ($type eq "WorkingGroup" or 
			    $type eq "CoordinationGroup" or
			    $type eq "InterestGroup") {
		       for my $it (formatGroup($ans, 1)) {
			   if (not ($taphash{$it})) {  $relans{$it} = 1; } 
		       }
		   } elsif ($type eq "Service") {
		       for my $it (formatService($ans, 1)) {
			   if (not ($taphash{$it})) {  $relans{$it} = 1; } 
		       }
		   } elsif ($type eq "Note") {
		       for my $it (formatSpecification($ans, 1)) {
			   if (not ($taphash{$it})) { $relans{$it} = 1; }
		       }
		   } elsif ($type eq "WD" ) {
		       for my $it (formatSpecification($ans, 1)) {
			   if (not ($taphash{$it})) { $relans{$it} = 1; }
		       }
		   } elsif ($type eq "PR" ) {
		       for my $it (formatSpecification($ans, 1)) {
			   if (not ($taphash{$it})) { $relans{$it} = 1; }
		       }
		   } elsif ($type eq "REC" ) {
		       for my $it (formatSpecification($ans, 1)) {
			   if (not ($taphash{$it})) { $relans{$it} = 1; }
		       }
		   } elsif ($type eq "MailingList") {
		       for my $it (formatMailingList($ans, 1)) {
			   if (not ($taphash{$it})) { $relans{$it} = 1; }
		       }
		   } elsif ($type eq "News") {
		       for my $it (formatNews($ans, 1)) {
			   if (not ($taphash{$it})) { $relans{$it} = 1; }
		       }
		   }
	       }
	   }
	   
	   if ($preprinted eq 1) {
	       print $tappost;
	       print "<p>\n";
	   }
       }

    for my $type (@SpecialTypes) {

	my $preprinted = 0;

	   for my $ans (keys %relans) {

	       if ($client->IsInstanceOf($ans, v($type))) {
		   if ($preprinted eq 0 and $relatedTypePrefix{$type}) {
		       print $tappre;
		       print $relatedTypePrefix{$type};
		       $preprinted = 1;
		   }
		   
		   if ($type eq "Person") {
		       formatPerson($ans, 0);
		   } elsif ($type eq "Activity") {
		       formatActivity($ans, 0);
		   } elsif ($type eq "WorkingGroup" or
			    $type eq "CoordinationGroup" or
			    $type eq "InterestGroup") {
		       formatGroup($ans, 0);
		   } elsif ($type eq "Service") {
		       formatService($ans, 0);
		   } elsif ($type eq "NOTE") {
		       formatSpecification($ans, 0);
		   } elsif ($type eq "REC") {
		       formatSpecification($ans, 0);
		   } elsif ($type eq "PR") {
		       formatSpecification($ans, 0);
		   } elsif ($type eq "WD") {
		       formatSpecification($ans, 0);
		   } elsif ($type eq "Note") {
		       formatSpecification($ans, 0);
		   } elsif ($type eq "MailingList") {
		       formatMailingList($ans, 0);
		   } elsif ($type eq "News") {
		       formatNews($ans, 0);
		   }
	       }
	   }
	   
	   if ($preprinted eq 1) {
	       print $tappost;
	       print "<p>\n";
	   }
       }


    print $alltappost;
    print $pagepost;
}

print $SearchFooter;
