#!/usr/bin/perl
# The -T flag forces checking of tainted values, for security.

# FILENAME    : findentry.pl
# TYPE        : main
# DESCRIPTION : main search script for untranslated Suda text.
#               CGI-bin script to accept keywords programs and find SUDA 
#               things that match.


use CGI;
use CGI::Carp;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw/:standard/;

#use diagnostics;

use lib "./";

# paths we depend on
$ENV{PATH} = '';
use strict;

# Global variables used in this file
use vars qw (
              $REAL_PATH
	      			$CGI_PREFIX
              $DEFAULT_PAGE_NUM
              $DEFAULT_NUM_PER_PAGE
              $DEFAULT_FILTER
              $FIND_ENTRY_CGI
              $LIST_BY_TR_ED_CGI
              $MENU_GIF_PATH
	      			$QEDIT
	      			$SEARCH_CGI
              %input
              $key
            );

require "Constant_main.pl";
require "Util_main.pl";

# Getting CGI input
local (%input);
local ($key);
foreach $key (param()) {
    $input{$key} = param($key);
}

# preparing HTTP header
&print_header('', $input{'filter'});

# -- search method
if ( !defined($input{'search_method'}) or ($input{'search_method'} eq '') ) {
  $input{'search_method'} = "QUERY";
}

# -- search result page number
if ( (!defined($input{'page_num'})) or
     ($input{'page_num'} eq '')     or
     ($input{'page_num'} < 1)     ) {
  $input{'page_num'} = $DEFAULT_PAGE_NUM;
}

# -- search result - number of entries per page
if ( (!defined($input{'num_per_page'})) or
     ($input{'num_per_page'} eq '')   ) {
  $input{'num_per_page'} = $DEFAULT_NUM_PER_PAGE;
}

# Defining 'my' variables local to this file
my ($ENTRY_DONE_REC_FILE) = $REAL_PATH . 'trans_done.dat';
my ($ENTRIES_DONE) = $REAL_PATH . 'done.dat';
my ($limit) = 750; # how many entries we are willing to show
my ($title_str, $keywords, $list, $entry, $originalAdler, $key, $adler);
my ($MACHTYPE, $headword, $content, $URLstart, $file, $fileName, $line);
my ($letter) = '';
my ($start) = 0;
my ($stop) = 0;
my ($value) = 0;
my ($AnswerCount) = 0;
my ($found) = 1;
my (@fullList) = ();
my (%status) = ();
my ($cur_entry_num) = 0;
my ($number_range, $end);

# ******************** MAJOR PROGRAM STARTS HERE ********************

$title_str = "Matching SUDA entries";

&print_HtmlTop($title_str, '', $input{'login'}, $input{'enlogin'});

# -- machine type
if (defined($ENV{'MACHTYPE'})) {
	$MACHTYPE = $ENV{'MACHTYPE'};
} else {
	$MACHTYPE = "sparc"
}
$MACHTYPE =~ s/\W//g; # remove suspicious characters
$MACHTYPE =~ /(.*)/;  $MACHTYPE = $1;  # untaint

#validate searchstring (keywords)
$keywords = join (" ", param('keywords'));
if ($keywords eq "") {
  print "The search string is empty<br/>\n";
  print "Search file invoked<br/>\n";
} # if (searchstring eq "")
else { # searchstring not empty ($keywords)

  $keywords =~ s/[^\w.*\+\s\|-]//g; # remove suspicious characters
  $keywords =~ /(.*)/; # untaint
  $keywords = $1;

  print "Entries matching <strong><font color=red>$keywords</font></strong>:\n";

  $keywords =~ s/-/ -! /g;
  $keywords =~ s/(^|\s|\b)(\w*[\+\.\*][\w\+\.\*]*)/$1-r $2/g;
  $keywords =~ s/\|/ -o /g;

# print "(I have converted your query to the internal form <strong>$keywords</strong>)\n";

  $list = `$QEDIT l text $keywords`;
  @fullList = split /\n{2,}/, $list;
  $AnswerCount = $#fullList + 1;
  if ($AnswerCount <= 0) { # if no results found
    $found = 0;
    print "There are no entries that match your query.<br/>\n";
    print "<hr noshade='noshade'/>";

print <<SEARCH_MENU_END;
    <center>
    <form action="$FIND_ENTRY_CGI" method="get">
    <table border="0" width="100%" cellspacing="5" cellpadding="5" bgcolor="#F0E2B9" align="center">
      <tr align="center">
      <td bgcolor="#F0E2B9" align="center">
      <input type="hidden" name="search_method" value="QUERY"/>
      <input type="hidden" name="login" value="$input{'login'}"/>
      <input type="hidden" name="enlogin" value="$input{'enlogin'}"/>
      <input type="hidden" name="page_num" value="$DEFAULT_PAGE_NUM"/>
      <input type="hidden" name="user_list" value="LIST"/>
      <p>Find <input type="text" name="keywords" value=""  size="40"/>&nbsp;
      <select name="num_per_page">
        <option value="10"> 10 </option>
        <option value="25" selected="selected"> 25 per page </option>
        <option value="50"> 50 </option>
        <option value="100"> 100 </option>
        <option value="200"> 200 </option>
      </select>
      &nbsp;<input type="submit" value="Find"/>
      <p align='center'>(Try these <a href="/sol/findentry_tips.shtml"
	  target="_blank">tips for more productive searches</a>.)</p>
      </td>
      </tr>
    </table>
    </form>
    </center>

SEARCH_MENU_END
  } # if no results found

  if ($found) { # if results found

    my ($page_min) = ($input{'page_num'}-1) * $input{'num_per_page'} + 1 ;
    my ($page_max) = $input{'page_num'} * $input{'num_per_page'};
    my ($count) = 0;
    my ($show_next_button) = 0; # FLAG - Do not show next button
  $count = 0;
  foreach $entry (sort {cmpentry($a,$b)} split /\n{2,}/, $list) { # foreach
    $count++;
    if ($count >  $page_max) {
      $show_next_button = 1;
    }
    if (($count >= $page_min) and
        ($count <= $page_max) ) { # within display range

      $entry =~ /adler = "([^"]+)"/;
      $originalAdler = $1; # we may modify later by changing fonts
      foreach $key (split /\s+/, $keywords) {
        next if (substr($key,0,1) eq "-");
        next if ($key =~ /^\s*$/);
        # print "<font color=blue> key: $key </font>";
        $key =~ s/[.]/\\w/g;
        $entry =~ s/\b($key)\b/<font color=red>$1<\/font>/ig;
      }
      # print "<font color=orange> Entry is $entry. </font>";
      $entry =~ /adler = "([^"]+)"/;
      $adler = $1;
      $entry =~ /headword = "([^"]+)"/;
      $headword = $1;
      $entry =~ /content = "([^"]+)"/;
      $content = $1;
      $URLstart = "<a href=$CGI_PREFIX/$SEARCH_CGI?search_method=QUERY" .
        "&amp;login=$input{'login'}&amp;enlogin=$input{'enlogin'}" .
        "&amp;searchstr=${originalAdler}&amp;field=adlerhw_gr&amp;db=REAL>" ;
      print "\n<p> $adler <br/> <strong> $headword: </strong>\n$content\n";
      print "<br/>";
      # print "Adler is [$originalAdler]\n";
			my $result;
			$result = `$QEDIT l realtranslations -f adlerhw_gr $originalAdler`;
			if ($result eq "") {
				print "<font color=purple> This entry has not been translated. ";
			} else {
				print "<font color=green> This entry has been $URLstart translated</a>. ";
			}
      print "</font>\n\n";
      print "<hr noshade='noshade'/>\n";
    } # within display range
  } # foreach entry

print <<SEARCH_MENU_END;
    <center>
    <form action="$FIND_ENTRY_CGI" method="get">
    <table border="0" width="100%" cellspacing="5" cellpadding="5" bgcolor="#F0E2B9" align="center">
      <tr align="center">
      <td bgcolor="#F0E2B9" align="center">
      <input type="hidden" name="search_method" value="QUERY"/>
      <input type="hidden" name="login" value="$input{'login'}"/>
      <input type="hidden" name="enlogin" value="$input{'enlogin'}"/>
      <input type="hidden" name="page_num" value="$DEFAULT_PAGE_NUM"/>
      <input type="hidden" name="user_list" value="LIST"/>
      <p>Find (in the source text) <input type="text" name="keywords" value=""  size="20"/>&nbsp;
      <select name="num_per_page">
        <option value="10"> 10 </option>
        <option value="25" selected="selected"> 25 per page </option>
        <option value="50"> 50 </option>
        <option value="100"> 100 </option>
        <option value="200"> 200 </option>
      </select>
      &nbsp;<input type="submit" value="Find"/>
      <p align='center'>(Try these <a href="/sol/findentry_tips.shtml"
	  target="_blank">tips for more productive searches</a>.)</p>
      </td>
      </tr>
    </table>
    </form>
    </center>

SEARCH_MENU_END

  print "<div align=\"center\" STYLE=\"color: black\">" ,   
        "No. of records found: <span
		style='font-weight:bold;'>$count</span>\n";    
  print "&nbsp;&nbsp;&nbsp;Page <span
  style='font-weight:bold;'>$input{'page_num'}</span></div><br/>\n";   
                                                                                
  my ($prev_page_num) = ($input{'page_num'} - 1);           
  my ($next_page_num) = ($input{'page_num'} + 1);

  if ($prev_page_num > 0) {
    print "<center>\n";
    print "<table border=\"0\">\n";
    print "<tr>\n";                                         
                                                                                    
    print "                                                 
      <td>                                                          
      <form action=\"$FIND_ENTRY_CGI\" method=\"post\">                 
      <input type=\"hidden\" name=\"search_method\" value=\"QUERY\"/>
      <input type=\"hidden\" name=\"login\" value=\"$input{'login'}\"/>
      <input type=\"hidden\" name=\"enlogin\" value=\"$input{'enlogin'}\"/>       
      <input type=\"hidden\" name=\"keywords\" value=\"$input{'keywords'}\"/>   
      <input type=\"hidden\" name=\"num_per_page\" value=\"$input{'num_per_page'}\"/>
      <input type=\"hidden\" name=\"page_num\" value=\"$prev_page_num\"/>         
      <input type=\"image\"  name=\"Previous\" 
	  src=\"$MENU_GIF_PATH/previous.jpg\" align='absmiddle' alt=\"Previous\"/>
      </form>                                                       
      </td>                                                         
";                                                            
  } # if ($prev_page_num > 0)

  if (!$show_next_button) { # if next button is not to be displayed       
    # print closing tag                                     
    print "</tr>\n";                                        
    print "</table>\n";                                     
    print "</center>\n";                                    
    print "<center><font color=\"red\">End of search</font></center><br/>\n";
  } # if (!$show_next_button)                               
  else { # showing next button                              
    if (!($prev_page_num > 0)) {                            
      print "<center>\n";
      print "<table border=\"0\">\n";                       
      print "<tr>\n";                                       
    }                                                       
    if (!defined($input{'login'})) { # anonymous search; use guest ids
      $input{'login'} = "guest";                                
    }                                                           
                                                                        
    print "                                                  
      <td>                                                          
      <form action=\"$FIND_ENTRY_CGI\" method=\"post\">                 
      <input type=\"hidden\" name=\"search_method\" value=\"QUERY\"/>
      <input type=\"hidden\" name=\"login\" value=\"$input{'login'}\"/>
      <input type=\"hidden\" name=\"enlogin\" value=\"$input{'enlogin'}\"/>       
      <input type=\"hidden\" name=\"keywords\" value=\"$input{'keywords'}\"/>   
      <input type=\"hidden\" name=\"num_per_page\" value=\"$input{'num_per_page'}\"/>
      <input type=\"hidden\" name=\"page_num\" value=\"$next_page_num\"/>
      <input type=\"image\"  name=\"Next\" src=\"$MENU_GIF_PATH/next.jpg\"
	  align='absmiddle' alt=\"Next\"/>
      </form>
      </td>
      </tr>
      </table>
      </center>
";

  } # else  # showing next button
  } # if results found
  &print_HtmlBot;
} # searchstring not empty ($keywords)
#exit(0);


#==============================================================================
# SUB_NAME : cmpentry
# PARAMS   : unsorted array list containing entries from the untranslated Suda
#            text
# DO WHAT  : sort the given unsorted array list
# RETURN   : return the sorted array list
#==============================================================================
sub cmpentry { # compare two entries: use the qddb $NUMBER$ field
	my ($a, $b) = @_;
	$a =~ /.NUMBER. = "([^"]+)"/;
	$a = $1;
	$b =~ /.NUMBER. = "([^"]+)"/;
	$b = $1;
	return($a <=> $b);
} # cmpentry


#==============================================================================
# SUB_NAME : entries_done
# PARAMS   : FILE -- trans_done.dat
# DO WHAT  : Write the entries that have so far been translated by translators
#            into "done.dat" file, which is used to print out the entries
#            translated so far.
# RETURN   : NIL
#==============================================================================
sub entries_done {
  if(!open (TDONE, "<$ENTRY_DONE_REC_FILE")) {
    print "Cannot open $ENTRY_DONE_REC_FILE<br/>\n";
    die "Can't open $ENTRY_DONE_REC_FILE";
  }
  my ($LINE) = '';
  open(DONE, ">$ENTRIES_DONE");
  foreach $LINE (<TDONE>) {
    if ($LINE !~ /<done>/) {next;}
    $LINE =~ s/<done>\w+=([^<]+)<.*/$1/;
    if (!defined($LINE)) {print "line? $LINE<p>"; next;}
    my (@done_range) = split(/\|\|/, $LINE);
    my ($range) = 0;
    foreach $range (@done_range) {
      print DONE "$range\n";
      ($adler, $number_range) = split(/:/, $range);
      ($start, $end) = split(/-/, $number_range);
      my ($subres) = $end - $start + 1;
    }
  }
  close(DONE);
  close(TDONE);
} # end of entries_done()
