#!/usr/bin/perl
# Vyakarana/Dhatupatha/mdhvindx/disp/mdhv-html.pl Mar 4, 2009 
#reads filter option from command line.
# reads txt lines from STDIN, writes output to STDOUT
# Input should be like 'mdhv-sandhi-sort.txt' (only 1st fld used)
#Output file name should end in '.html'
# also reads 'filter' argument, which determines how to 
# construct the output. filter should be one of these:
# X='SktRomanUnicode SktDevaUnicode SLP2HK' or
# SLP2SLP if no transliteration desired.
use lib qw(/Volumes/Sanskrit-RAID/jfunderburk/app/cvtskt);
use Sanskrit::EmbedUni;
#use Sanskrit::Embed;
my $index = $ARGV[0];
my $filter= $ARGV[1];
if (not($index)) {$index="index";}
if (!$filter) {$filter="SLP2SLP";}
print STDERR "index = $index, filter = $filter\n";
my $FILTER_DIR = "/Volumes/Sanskrit-Raid/jfunderburk/util/translit_filter";
my @FILTERS = qw(SktRomanUnicode SktDevaUnicode SLP2HK SLP2SLP SLP2ITRANS SktRomanManjushreeCSX SktRomanCSX);
{my $n;
 my $found = 0;
 foreach(@FILTERS) {
     if ($_ eq $filter) {$found = 1};
 }
 if ($found eq 0) {die "invalid filter. valid are @FILTERS\n";}
}

# print header material for this file
my $HEADER="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n";


print "$HEADER\n";
print "<html>\n";
print "<head>\n";
print "<title>Madhaviya index: $filter</title>\n";
print '<link href="http://www.language.brown.edu/Sanskrit/styles/main.css" rel="stylesheet"  type="text/css" media="screen" />';
print "<style type='text/css'>\n";
print "td { background-color:#e0e0e0; font-size:16pt; padding-bottom:5px; padding-top:5px;padding-left:3px}\n";
print "th { text-align: left}\n";
print "td.title {text-align: center}\n";
print "h1.title {text-align: center; font-size: 18pt;}\n";

#print "#optdiv {position:absolute;  bottom: 5px; left: 5px; right: 5px; text-align: center}\n";
print "#datadiv {overflow: auto; position:absolute; top:210px; bottom: 30px;}\n";
print "#copyright {position:absolute;  bottom: 5px; left: 5px; right: 5px; text-align: center}\n";
print "a.letterlink {text-decoration: none;}\n";
print  ".romanUniItalic {font-style: italic; font-weight: normal;}" .
    ".devaUni {font-size: 18pt;}";

print "</style>\n";
print "<script type='text/javascript'>\n";
print " function optdivoutput(){\n";
print "  var word = document.getElementById('optdivoutput').value;\n";
print "  var htmlpfx='';\n";
print "  var htmlsfx = 'mdhv_$index';\n";
print "  if (word != 'SktDevaUnicode') {\n";
print "   htmlsfx = htmlsfx + '_' + word;\n";
print "  }\n";
print "  htmlsfx = htmlsfx + '.html';\n";
print "  var html = htmlpfx + htmlsfx;\n";
print "  window.document.location.href = html;\n";
print "  }\n";
print "</script>\n";

if (($filter =~ /UTF8|Unicode/) or ($filter eq "SLP2HK") or
    ($filter eq "SLP2SLP") or ($filter eq "SLP2ITRANS")){
    print '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">' . "\n";
}else {
    print '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">' . "\n";
}
print "</head>\n";
print "<body>\n";

my $table = "";
$table .= '<div class="segment">';
$table .= '<h1><a href="/cgi-skt/redirect?get=catalog.html">The Sanskrit Library</a> | Vyakarana | Dhatupatha</h1>';
$table .= "</div>\n"; # segment div

$table .= "<div id='titlediv'>\n";
$table .= '<table  width="100%"><tr><td class="title"><img id="unilogo" src="/Sanskrit/Vyakarana/Dhatupatha/images/brown_seal.jpg" alt="Brown University" width="70" height="97" /></td>';
my ($htitle,$h);
if ($filter eq "SktDevaUnicode"){
#    $h="{sp dvArikAdAsaSAstriRaH} {st mADavIyA-DAtuvftti-anukramaRI}";
    my %titles = (
	index => "{sd dvArikAdAsaSAstriRaH mADavIyA-DAtuvftti-anukramaRI}",
	sOtraDAtu => "{sd dvArikAdAsaSAstriRaH mADavIyanAmaDAtuvfttisTasOtraDAtvanukramaRI}",
	kaRqvAdi => "{sd dvArikAdAsaSAstriRaH mADavIyanAmaDAtuvfttisTakaRqvAdiDAtvanukramaRI}",
	nAmaDAtu => "{sd dvArikAdAsaSAstriRaH mADavIyanAmaDAtuvfttisTanAmaDAtvanukramaRI}",
	Rijanta => "{sd dvArikAdAsaSAstriRaH mADavIyanAmaDAtuvfttisTaRijantaDAtvanukramaRI}"
	);
    $h=$titles{$index};
#    print STDERR "title $index = $h\n";
    $htitle = embed_Uni($h);
#    print STDERR "back from embed_Uni\n";
    $htitle =~ s/\{ci (.*?)\}/<span class="romanUniItalic">$1<\/span>/g;
    $htitle =~ s/\{c (.*?)\}/$1/g;
    $htitle =~ s/\{v (.*?)\}/$1/g;
}elsif (($filter eq "SLP2HK") or ($filter eq "SktRomanUnicode") or
	($filter eq "SLP2SLP") or ($filter eq "SLP2ITRANS")) {
    my %titles = (
	index => "Dwarikadas Shastri's {st mADavIyA} {st DAtuvftti} Index",
	sOtraDAtu => "Dwarikadas Shastri's Index of {se sOtraDAtu}s <br/>in {sp sAyaRa}'s {st mADavIya} {st nAmaDAtuvftti}",
	kaRqvAdi => "Dwarikadas Shastri's Index of {se kaRqvAdiDAtu}s <br/>in {sp sAyaRa}'s {st mADavIya} {st nAmaDAtuvftti}",
	nAmaDAtu => "Dwarikadas Shastri's Index of {se nAmaDAtu}s <br/>in {sp sAyaRa}'s {st mADavIya} {st nAmaDAtuvftti}",
	Rijanta => "Dwarikadas Shastri's Index of {se RijantaDAtu}s <br/>in {sp sAyaRa}'s {st mADavIya} {st nAmaDAtuvftti}"
	);
    $h=$titles{$index};
#    print STDERR "title $index = $h\n";
    $htitle = embed_Uni($h);
#    print STDERR "back from embed_Uni\n";
    $htitle =~ s/\{ci (.*?)\}/<span class="romanUniItalic">$1<\/span>/g;
    $htitle =~ s/\{c (.*?)\}/$1/g;
    $htitle =~ s/\{v (.*?)\}/$1/g;
}

#else { # SktRomanManjushreeCSX SktRomanCSX
#    $h = "Dwarikadas Shastri's {st mADavIyA} {st DAtuvftti} Index";
#    $htitle = embed_X($h);
#    $htitle =~ s/\{ci (.*?)\}/<span class="romanUniItalic">$1<\/span>/g;
#}

$table .= "<td><H1 class='title'>$htitle</H1></td>\n";
$table .= '<td><table><tr><td class="title"><a href="help.html">help</a></td></tr><tr><td class="title"><a href="IndexAbout.pdf">about (pdf)</a></td></tr> <tr><td class="title"><a href="IndexAbout.html">about (html)</a></td></tr></table></table>' . "\n";
$table .= "</div>\n";

$table .= "  <div id='optdiv'>\n";
$table .= "  <select id='optdivoutput' onChange='optdivoutput()'>\n";
my ($i,$f,$opt);
my @FILTEROPTIONS = ("Roman Unicode","Devanagari Unicode" ,"Kyoto Harvard",
   "SLP","ITRANS"); #,"ManjushreeCSX","CSX");
for ($i=0; $i <= $#FILTEROPTIONS;  $i++) {
  my $f = $FILTERS[$i];
  $table .= "  <option value='$f'";
  if ($f eq $filter) {
   $table .= " selected='selected'";
  }
  $table .= ">$FILTEROPTIONS[$i]</option>\n";
}
$table .= "  </select>\n";
my @first_chars = qw(a A i I u U f F e o k K g G N c C j J w q R t T d D n p P b B m y r l v S z s h);
my %first_chars_used;
my $num_first_chars = scalar @first_chars;
foreach (@first_chars) {
    $first_chars_used{$_} = "no";
}
my $ifirst_char;
if ($index ne "sOtraDAtu") {
for ($ifirst_char=0;$ifirst_char < $num_first_chars; $ifirst_char++) {
    $table .= "&nbsp;&nbsp;<a class='letterlink' href='#begin_char_$ifirst_char'><SA>$first_chars[$ifirst_char]</SA></a>";
}
}
$table .= "  </div>\n";

$table .= "<div id='datadiv'>\n";

$table .= "<table class='disp' width='100%'>\n";

my $n = 0; # number of lines read
my ($line,$s,$s1);
    $line = "<tr>\n";
    $line .= "<th class='disphd'>full root</th>\n";
    $line .= "<th class='disphd'>root</th>\n";
    $line .= "<th class='disphd'>marker</th>\n";
    $line .= "<th class='disphd' width='20%'>sense or specifier</th>\n";
    $line .= "<th class='disphd'>class</th>\n";
    $line .= "<th class='disphd'>class name</th>\n";
    $line .= "<th class='disphd'>sutra</th>\n";
    $line .= "<th class='disphd'>page</th>\n";
    $line .= "<th class='disphd'>variant</th>\n";
    $line .= "</tr>\n";
$table .= $line;
$ifirst_char = 0;
#$table .= "<div id='datadiv'>\n";
my $nout = 0;
while (<STDIN>) {
    $n++;
    $s = $_;
#    print "$n read: $s\n";
    chomp($s);
    ($s1) = split(' : ',$s); # get first field.
    my ($fullDAtu,$root,$marker,$sense,$gaRaNum,$gaRa_name,$sUtraNum,$page,$pa) = split(' ; ',$s1);
    if ( (($index eq "index") and (1 <= $gaRaNum) and ($gaRaNum <= 10)) or
	 ($index eq $gaRa_name)) {
	$line = process($s);
	$table .= $line;
	$nout++;
    }
#    die "debug\n";
}
$table .= "</table>\n";
$table .= "</div>\n";
$table .= "<div id='copyright'>\n";	
$table .= "<span class='copy'>&copy; 2009 The Sanskrit Library</span>\n";
$table .= "</div>\n";
print STDERR "$nout lines written from $n lines processed.\n";
if ($filter ne "SLP2SLP") {
    open FILTER, "| $FILTER_DIR/$filter";
    print FILTER $table;
    close FILTER;
}else {
    $table =~ s/<\/?SA>//g;
    print $table;
}


print  "</body>\n";
print  "</html>\n";
exit;

sub process {
    my $s = shift;
    ($s1) = split(' : ',$s); # get first field.
    my ($fullDAtu,$root,$marker,$sense,$gaRaNum,$gaRa_name,$sUtraNum,$page,$pa) = split(' ; ',$s1);
    if ($index ne "index") {
	$gaRaNum = "";
    }
    my $char = substr($root,0,1);
    my $line = "";
    $line .= "<tr>\n";
    my $line0 = "<SA>$fullDAtu</SA>";
    if($first_chars_used{$char} eq "no") {
	$first_chars_used{$char}="yes";
#	print STDERR "Found first char $char at root $root, $num_first_chars\n";
	$ifirst_char=0;
	while ($ifirst_char < $num_first_chars) {
#	    print STDERR "$ifirst_char, $num_first_chars\n";
	    if ($char eq $first_chars[$ifirst_char]) {
		$line0 = "<a name='begin_char_$ifirst_char' />" . $line0;
#		print STDERR "found $char @ position $ifirst_char\n";
		$ifirst_char=$num_first_chars;
	    }else {
#		print STDERR "$char ne " . $first_chars[$ifirst_char] . " , $ifirst_char\n";
		$ifirst_char++;
	    }
	}
    }
#    if (($ifirst_char < $num_first_chars) and
#	($char eq $first_chars[$ifirst_char])) {
#	$line0 = "<a name='begin_char_$ifirst_char' />" . $line0;
#	$ifirst_char++;
#    }
    $line .= "<td class='disp'>$line0</td>\n";
    $line .= "<td class='disp'><SA>$root</SA></td>\n";
    $line .= "<td class='disp'><SA>$marker</SA></td>\n";
    # a few senses are very long, though most are not.
    # trim the size of $sense
    if (length ($sense) > 30) { # tried 40 first
	$sense = substr($sense,0,30);
	$sense =~ s/[kKgGNcCjJYwWqQRtTdDnpPbBmyrlvSzsh]+$//;
	$sense =~ s/\(/ (/;
	$sense =~ s/\[/ [/;		      
	if (length($sense) <= 28) {
	    $line .= "<td class='disp'><SA>$sense</SA> &deg;</td>\n";
	}else {
	    $line .= "<td class='disp'><SA>$sense</SA>&deg;</td>\n";
	}
	print STDERR " Trimming sense for $fullDAtu\n";
    }else {
	$sense =~ s/\(/ (/;
	$sense =~ s/\[/ [/;		      
	$line .= "<td class='disp'><SA>$sense</SA></td>\n";
    }
    $line .= "<td class='disp'>$gaRaNum</td>\n";
    $line .= "<td class='disp'><SA>$gaRa_name</SA></td>\n";
    $line .= "<td class='disp'>$sUtraNum</td>\n";
    my $pagenum = $page;
    $pagenum =~ s/-.*//;
    $pagenum += 54;
    $line .= "<td class='disp'>";
    $line .="<a href='http://www.sanskritlibrary.org/Vyakarana/Dhatupatha/mdhvscan/disp/index.php?page=$pagenum'";
    $line .= " target='mdhvscan'" . ">" . "$page</a>" . "</td>\n";
    if ($pa ne "") {
	$line .= "<td class='disp'><SA>pA</SA>&deg;</td>\n";
    }else {
	$line .= "<td class='disp'>$pa</td>\n";
    }
    $line .= "</tr>\n";
			 
}
