#!/usr/bin/perl -w
#
# Durchsucht die Briefe (pcl->pdf->html, siehe Script zur Umwandlung)
# und die E-Mails (in einzelnen Dateien, siehe Script zur Umwandlung)
# nach Suchworten. Zeigt die Ergebnisse als Links an
#
# michael.schaarwaechter@ub.uni-dortmund.de, 9/07
#

use CGI;
our $query=new CGI;

our $briefedir="/var/spool/sisis/avserver/batch/alist/briefe4apache";
our $link2show="https://meinsisis.server/Briefe";

# -----------------------------------
sub datum {

   my @time;
   @time=localtime;

   my $y=$time[5]+1900;
   my $m=$time[4]+1;
   my $d=$time[3];
   my $hour=$time[2];
   my $min=$time[1];
   my $sec=$time[0];
   $sec="0".$sec if (length($sec)==1);
   $min="0".$min if (length($min)==1);
   $hour="0".$hour if (length($hour)==1);
   $d="0".$d if (length($d)==1);
   $m="0".$m if (length($m)==1);

   return $d.".".$m.".".$y." ".$hour.":".$min.":$sec";
}
# -----------------------------------
sub fehler {
   return qq#<p class="error">#,@_,qq#</p>#;
}
# -----------------------------------
sub suche {
   my $suchstring=$query->param("s");
   return &fehler("Suchbegriff fehlt!") if (not $suchstring);
   print qq#<p><span class="bggelb">Gesucht wurde $suchstring</span></p>#;
   my @suchen=split / +/,$suchstring;

   undef $/;
   my $content;
   my $count=0;
   print "<p>&nbsp;<br>";
   foreach my $d(glob("$briefedir/20??.??.??")) {
      foreach my $f(glob("$d/*.html"),glob("$d/*.txt")) {
         open (FILE,$f); $content=<FILE>; close FILE;
         $found=1;
         foreach my $s(@suchen) {
            if ($content!~/$s/si) {
               $found=0;
               last;
            }
         }
         $f=~s/$briefedir\///;
         if ($found) {
            my $where="E-Mail";
            $where="Brief" if ($f=~/.html$/);
            print qq#Fundstelle $where: <a href="$link2show/$f">$link2show/$f</a><br>#;
            $count++;
         }
      }
   }
   print qq#<br><span class="bggelb">$count Fundstellen</span><p>#;
   return;
}
# -----------------------------------

$| = 1;   # Keine Ausgabepufferung

print $query->header;

print << "EOT";
   <!DOCTYPE  HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
   <html>
   <head>
      <TITLE>Suchbriefe-Ausgabe</TITLE>
      <META NAME="author" CONTENT="$0, Michael Schaarwaechter">
      <META NAME="copyright" CONTENT="Michael Schaarwaechter">
      <META CONTENT="text/html; charset=iso-8859-1" HTTP-EQUIV="Content-Type">
      <style type="text/css"> <!--
         /*  spezielle CSS-Formate */
         body {
               font-family:Helvetica,Arial,sans-serif;
               font-size:medium;
               background-color:#FFFFFF;
              }
         h1 {
               font-size:x-large;
               font-size:small;
               border-bottom-width:3px;
               border-bottom-style:solid;
               border-bottom-color:#6699CC;
            }
         .error {
               color:red;
            }
         .bggelb {
               background-color:yellow;
            }
         .signatur {
               text-align: right;
               font-size:small;
               border-top-width:3px;
               border-top-style:solid;
               border-top-color:#6699CC;
               margin-left:30%;
              }
       --> </style>
   </head>
   <body>

   <h1>Ergebnis der Suche</h1>
EOT
print &suche;
my $prog=$0;
print qq#<p class="signatur">Erzeugt von $prog am #.&datum.qq#</p># ;
print $query->end_html;


