#!/bin/perl
use strict;

my @preforder = ('ORGANIZATION','YEAR_FOUNDED','DIRECTOR','DIRECTORS','DIRECTOR(S)',
                 'CHAIRPERSON','P.I.','PRESIDENT','CEO/PRESIDENT','VICE PRESIDENT',
                 'CHIEF EXECUTIVE','SECRETARY GENERAL','SECRETARY',
                 'ADDRESS','ADDRESS (HOME)','CITY','STATE','COUNTRY',
                 'CONTACT','TELEPHONE','FAX','FAX ', 'PH/FAX',
                 'E-MAIL','E-MAIL ','EMAIL',
                 'WEBSITE','WEB','WORKAREA','SERVICES','WORK SERVICES',
                 'LASTMOD');

open DATABASE, "/home/webpages/t8/people/rajan/AIDS-india/NGO/ingo.html"
    || errout($?);

my @indianstates = ("Andhra Pradesh", "Arunachal Pradesh", "Assam", "Bihar",
		    "Chattisgarh", "Goa", "Gujarat", "Haryana", 
		    "Himachal Pradesh", "Jammu and Kashmir", "Jharkhand",
		    "Karnataka", "Kerala", "Madhya Pradesh", "Maharashtra",
		    "Manipur", "Meghalaya", "Mizoram", "Nagaland", "Orissa",
		    "Punjab", "Rajasthan", "Sikkim", "TamilNadu", "Tripura",
		    "Uttar Pradesh", "Uttaranchal", "West Bengal");
my @indianuts = ("Andaman and Nicobar", "Chandigarh", 
		 "Dadra and Nagar Haveli", "Daman and Diu", "New Delhi", 
                 "Lakshadweep", "Pondicherry");

my @records = ();

my $state;
my $currentrec;
my $currentfield;

while (<DATABASE>) {
    chomp;
    if (/<U>(.*)<\/U>/i) {
	$state = $1;
	push @records, $currentrec if defined $currentrec;
	undef $currentrec;
	undef $currentfield;
	next;
    }
    if (/^\s*</) {
	undef $currentfield;
	next;
    }
    if (! /^([^ ][^:]+):\s+(.*)/) {
	next unless defined $currentfield;
	$$currentfield .= "$_";
	next;
    }
    if (uc "$1" eq "ORGANIZATION") {
	push @records, $currentrec if defined $currentrec;
	$currentrec = { SUBMIT => "search" };
	if (grep uc($_) eq uc($state), (@indianstates, @indianuts)) {
	    $currentrec->{COUNTRY} = "INDIA";
	    $currentrec->{STATE} = $state;
	} elsif (uc ($state) ne "INTERNATIONAL") {
            $currentrec->{COUNTRY} = $state;
        }
    } else {
	next unless $currentrec;
    }
    $currentrec->{uc "$1"} = $2;
    $currentfield = \$currentrec->{uc "$1"};
}

push @records, $currentrec if defined $currentrec;

my $searchpat = $ENV{QUERY_STRING};
$searchpat =~ s/([^&=]+=[^&]*)(&?)(.*)/$3$2$1/;
$searchpat =~ s/[^=&]+=\s*\&//g;
$searchpat =~ s/(\&|^)[^=&]+=\s*$//g;
$searchpat =~ s/([^=&]+)=([^=&]+)/\$_->{\U$1\E} =~ \/\Q$2\E\/i/g;
$searchpat =~ s/\&/ \&\& /g;
$searchpat =~ s/\+/ /g;
$searchpat =~ s/\%([\da-z]{2})/pack('H2',$1)/ieg;


# OVERRIDING SECURITY
if ($searchpat =~ /^(.*)$/) {
   $searchpat = $1;
} else {
   errout("Invalid pattern '$searchpat'");
}

my @result = grep eval $searchpat, @records;

print "Status: 200 OK\nContent-Type: text/html; charset=iso-8859-1\n\n";
print <<FILE_HEAD;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
            "http://www.w3.org/TR/REC-html40/strict.dtd">
<HTML>
<HEAD>
 <TITLE>Results of search on NGO database</TITLE>
 <LINK REL="Made" HREF="mailto:rajan\@lanl.gov">
 <STYLE TYPE="text/css">
    BODY { background: white; color: black }
    .Red { color: red }
 </STYLE>
</HEAD>
<BODY>
FILE_HEAD

print '<H1>', scalar(@result), ' of ', scalar(@records), " match</H1>\n";
my $display = $searchpat;
$display =~ s/\$_->{([^}]*)}\s*=~\s*\/([^\/]*)\/i/'$2' in '$1'/g;
$display =~ s/\&\&/ AND /g;
$display =~ s/(\s*AND\s*)'Search' in 'SUBMIT'//;
$display =~ s/'Search' in 'SUBMIT'(\s*AND\s*)?//;
print "<p>SEARCH PATTERNS WERE: $display</p>\n";
foreach $currentrec (@result) {
    print "<HR><TABLE>\n";
    foreach (sort {
                   foreach (@preforder) {
                      if ($_ eq $a) { return -1; }
                      if ($_ eq $b) { return 1; }
                   };
                   return 0;}
                  keys %{$currentrec}) {
	next if /^SUBMIT$/;
	s/</\&lt/g; s/>/\&gt;/g; s/\&/\&amp;/g;
	print "<TR><TD CLASS=\"Red\">$_</TD>\n";
        if ($_ eq "E-MAIL" || $_ eq "EMAIL" || $_ eq "E-MAIL ") {
            $_ = $currentrec->{$_};
	    s/^\s+//g;
            s/<br>\s*$//;
            if (!/^<A/) {
	    $_ = join ', ', map($_?"<A HREF=\"mailto:$_\">$_</A>":(),
				split /[,;\s]/);
	    }
        } elsif ($_ eq "WEBSITE" || $_ eq "WEB") {
            $_ = $currentrec->{$_};
	    s/^\s+//g;
            s/<br>\s*$//;
            if (!/^<A/) {
            if (! /:\/\// ) {
	      $_ = "<A HREF=\"http://$_\">$_</A>";
            } else {
	      $_ = "<A HREF=\"$_\">$_</A>";
            }
            }
        } else {
            $_ = $currentrec->{$_};
	    s/^\s+//g;
        }
#	s/<[^>]*>/ /g;
#	s/</\&lt/g; s/>/\&gt;/g; s/\&/\&amp;/g;
	print "<TD>$_</TD></TR>\n";
    }
    print "</TABLE></LI>";
}
print "<HR>\n";

print <<FILE_TAIL;
<P><A HREF="http://www.lanl.gov/Misc/disclaimer.html">Disclaimer</A></P>
</BODY>
</HTML>
FILE_TAIL


sub errout
{
    print "Status: 400 denied\nContent-Type: text/plain\n\n";
    print "Cannot deal with @_\n";
    exit(0);
}
