#!/usr/bin/perl
# run as a CGI script, or offline for testing as
# vulsearch --offline SEARCH
# where SEARCH is the string to search for.

use CGI qw(param);

($basepath,$gnudate)=($ARGV[0] ne "--offline") ? ("../htdocs","date") :
    ("../Website", "c:/gnuwin32/bin/date" );

#initialize our hash of book names
%bib =
(
  Gn => 1,
  Ex => 2,
  Lv => 3,
  Nm => 4,
  Dt => 5,
  Jos => 6,
  Jdc => 7,
  Rt => 8,
  '1Rg' => 9,
  '2Rg' => 10,
  '3Rg' => 11,
  '4Rg' => 12,
  '1Par' => 13,
  '2Par' => 14,
  Esr => 15,
  Neh => 16,
  Tob => 17,
  Jdt => 18,
  Est => 19,
  Job => 20,
  Ps => 21,
  Pr => 22,
  Ecl => 23,
  Ct => 24,
  Sap => 25,
  Sir => 26,
  Is => 27,
  Jr => 28,
  Lam => 29,
  Bar => 30,
  Ez => 31,
  Dn => 32,
  Os => 33,
  Joel => 34,
  Am => 35,
  Abd => 36,
  Jon => 37,
  Mch => 38,
  Nah => 39,
  Hab => 40,
  Soph => 41,
  Agg => 42,
  Zach => 43,
  Mal => 44,
  '1Mcc' => 45,
  '2Mcc' => 46,
  Mt => 47,
  Mc => 48,
  Lc => 49,
  Jo => 50,
  Act => 51,
  Rom => 52,
  '1Cor' => 53,
  '2Cor' => 54,
  Gal => 55,
  Eph => 56,
  Phlp => 57,
  Col => 58,
  '1Thes' => 59,
  '2Thes' => 60,
  '1Tim' => 61,
  '2Tim' => 62,
  Tit => 63,
  Phlm => 64,
  Hbr => 65,
  Jac => 66,
  '1Ptr' => 67,
  '2Ptr' => 68,
  '1Jo' => 69,
  '2Jo' => 70,
  '3Jo' => 71,
  Jud => 72,
  Apc => 73
);

$searchstring=param("searchtext");# if ( $searchstring eq "" );
$searchstring=$ARGV[1] if $ARGV[0] eq "--offline";
$gendate=`$gnudate +%d/%m/%y`;

#write the start of the HTML document
print <<ENDHEADER;
Content-type: text/html; charset=iso-8859-1

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en">
  <head>
    <title>Search the Clementine Vulgate</title>
    <link rel="stylesheet" type="text/css" href="../newstyle.css" />
    <link rel="icon" type="image/png" href="../favicon.png" />
    <link rel="alternate" type="application/rss+xml" title="Clementine text updates" href="../clemtext.rss" />
    <link rel="start" title="Clementine text project" href="../gettext.html" />
    <meta name="author" content="little dot mouth at soon dot com" />
    <meta name="keywords" content="vulgate bible jerome clementine catholic tridentine traditional vulsearch" />
    <meta name="description" content="Search the Clementine Vulgate online." />

  </head>
  <body>
    <div id="root">
      <div id="masthead">
        <a href="../gettext.html"><img src="../vs.png" alt="Clementine project" /></a>
        <h1>VulSearch &amp; the Clementine Vulgate project</h1>
      </div>
      <div id="navbar">
        <h2>Contents</h2>
        <ul>
          <li>
          <a href="../index.html">VulSearch</a>
          </li>
          <li>
          <a href="../gettext.html">Clementine text project</a>
          </li>
          <li>
          <a href="../text.html">Text FAQ</a>
          </li>
          <li>
          <span class="active">Search the text online</span>
          </li>
          <li>
          <a href="../history.html">A history of the project</a>
          </li>
          <li>
          <a href="../technical.html">Behind the scenes</a>
          </li>
          <li>
          <a href="../links.html">Links</a>
          </li>
          <li>
          <a href="../contact.html">Contact me</a>
          </li>
        </ul>
      </div>
      <div id="content">

        <h2>
          <span id="updated">Generated on $gendate</span>
          Search the Clementine Vulgate online
        </h2>
        
ENDHEADER

#the heart of the program
sub cleanup
{
  $s=$_[0];
  $s =~ s/ :/\&nbsp;:/g;
  $s =~ s/ ;/\&nbsp;;/g;
  $s =~ s/ \?/\&nbsp;?/g;
  $s =~ s/ !/\&nbsp;!/g;
  $s =~ s/\xe6/\&aelig;/g;
  $s =~ s/\xeb/\&euml;/g;
  $s =~ s/\xc6/\&AElig;/g;
  $s =~ s/\x9c/\&oelig;/g;
  $s =~ s/\x8c/\&OElig;/g;
  return $s;
}

if ($searchstring ne '')
{
  $results=`grep -iHE "$searchstring" $basepath/txt/*.txt`;
  $results= cleanup($results);

  @verses=split "\n", $results;
  $t=0;
  foreach $verse (@verses)
  {
    ($head, $body) = split " ", $verse, 2;
    ($_, $book) = split /.+\//, $head;
    ($book, $_, $chap, $ver) = split /[\.\:]/, $book;
    $books[$t]=$book;
    $chaps[$t]=$chap;
    $vers[$t]=$ver;
    $body[$t]=$body;
    $ct[$t]=$t;
    $t++;
  }

  print '<h3>Search results for ', "$searchstring ($t)", '</h3>', "\n";


  sub sorting
  {
    $bib{$books[$a]} <=> $bib{$books[$b]} or
    $chaps[$a] <=> $chaps[$b] or
    $vers[$a] <=> $vers[$b]
  }

  if ($t==0)
  {
    print '<p>No results found.</p>', "\n";
  }
  else
  {

    @ct = sort sorting @ct;
    for ($i=0; $i<$t; $i++)
    {
      $href='<a href="' . "../html/$books[$ct[$i]].html#x$chaps[$ct[$i]]_$vers[$ct[$i]]". '">';
      print '<p>', $href, '<span class="ref">',
      "$books[$ct[$i]] $chaps[$ct[$i]]:$vers[$ct[$i]]", '</span></a> ', $body[$ct[$i]], '</p>', "\n\n";
    }
  }
}

#always provide a new search option
print <<ENDFOOTER;
<h3>New search</h3>
<form action="http://vulsearch.sourceforge.net/cgi-bin/vulsearch"
method="post">
<p><input type="text" name="searchtext" value="$searchstring" /> <input
type="submit" name="searchbutton" value="Search" />
</p></form>

<h3>Instructions</h3>
<p>As well as typing in simple phrases to search for, you can perform
sophisticated searches using regular expressions.</p>
<ul>
<li> To search for &aelig; or other ligatures, replace them with periods (for
example, <span class="ref">s.culum s.culi</span>).  </li>
<li> To search for whole words, put \\b before and after the word (for
example, <span class="ref">\\bpatre\\b</span>).  </li>
<li> To search for either a or b, use vertical bars in parentheses (for
example, <span class="ref">virg(o|in)</span>).  </li>
<li> Complete documentation is <a href="../grep.html">here</a>.  </li>
</ul>

          <p class="clearer"></p>

          <div id="footer">
            <div class="centered">
              <div class="leftfloat">
                <a
                  href="http://sourceforge.net"><img style="border: 0"
                  src="http://sourceforge.net/sflogo.php?group_id=53472&amp;type=1"
                  alt="SourceForge.net" id="sf" /></a><br />
                <a
                  href="http://www.digits.com"><img src=
                  "http://counter.digits.com/wc/-r/-f/000000/-b/ffffff/-d/6/VulSearchSearch"
                  alt="(Counter)" style="border: 0" /></a>
              </div>
              <div class="rightfloat">
                <a
                  href="http://validator.w3.org/check?uri=referer"><img
                  style="border: 0"
                  src="../valid-xhtml10.png" alt="Valid XHTML 1.0!" /></a>
              </div>  
              <div class="centered">
                You can email me at<br /><span class="strong">little.mouth@soon.com</span>
              </div>
            </div>
          </div>
        </div>
      </div>
  </body>
</html>

ENDFOOTER