#!/usr/bin/perl

# leita
# perl leita.pl ord ord ...

#   fremja sql , fá lista [skrá,orð,fjöldi] raðað eftir skrá
#   fyrir hverja skra:
#     gera grep með byteoffsets
#     fyrir hvert result:
#       lesa línu með extra context
#       finna aftur orðin í línunni
#       fyrir hvert þeirra:
#          build [undan,orð,eftir]
#   prenta listann

#our $version='test';
$ENV{LANG}='is_IS.utf8';
#use utf8;
use locale;
#use open IN=>':utf8';
#use open ':utf8',':std';
#use open ':utf8';
#use open OUT=>':utf8',':std';
#use open STDOUT=>':utf8';
#binmode STDOUT,':utf8';
#binmode STDIN,':utf8';
#binmode STDOUT,':bytes';
binmode STDERR,':bytes';
our $version='live';

our ($root,$haus1,$haus2,$TITILL,$hmynd,$hmyndv,$webdir,$dbuser,$dbname);

if ($version eq 'live') {
    $root='/var/www/corpus-data/textar';
    $haus2="Íslenskt textasafn";
    $haus1="Stofnun Árna Magnússonar í íslenskum fræðum";
    $TITILL=qq(Textasafn SÁM);
    $hmynd="corpus.png";
    $hmyndv="logo-80.png";
    $webdir='/var/www/corpus';
    $dbuser='postgres';
    $dbname='corpus2;host=mimisbrunnur.rhi.hi.is';
} else {
    $root='/home/textar';
    $haus2="test";
    $haus1="Textasafn";
    $TITILL=qq(Textasafn);
    $hmynd="textamynd1.png";
    $hmyndv="textamynd2.png";
    $webdir='/var/www/corpus';
    $dbuser='postgres';
    $dbname='corpus';
}

our $apache=1;

our $UNDAN=62;
our $EFTIR=82;
our $MAXLIN=250;
our $MAXFILES=50;
our $MAXTIME=4;

our $self='leit.pl';
#our $iscgi=1;

use vars qw($disort $Disort %disort);
{
    use utf8;
    $disort=" 0123456789aábcdðeéfghiíjklmnoópqrstuúvwxyýzþæö";
    $Disort=" 0123456789AÁBCDÐEÉFGHIÍJKLMNOÓPQRSTUÚVWXYÝZÞÆÖ";
}
{
  my $c="64";
  for (split //,$disort) {
    $disort{$_}=chr($c++);
  }
  $c="64";
  for (split //,$Disort) {
    $disort{$_}=chr($c++);
  }
}

use DBI;

our $dbh = DBI->connect("dbi:Pg:dbname=$dbname",$dbuser)
           or die 'gat ekki opnað gagnagrunn';
                                                                                
use CGI;
our $cgi=new CGI;
our %vars;
%vars=$cgi->Vars();
#{
#    for my $k (keys %vars) {
#	print STDERR "param $k: [$vars{$k}] (".length($vars{$k}).")\n";
#    }
#}


# params
# lemma1,lemma2
# m\d\d*
#ser1,ser2
# flokkar
# leita
# saekja1,saekja2

$vars{lemma1}=~s/^ +//;
$vars{lemma2}=~s/^ +//;

our $forbidden=0;
our $allt=0;
our $clientip=$ENV{HTTP_X_FORWARDED_FOR} || $ENV{REMOTE_ADDR};

if ($ENV{REDIRECT_ALLT}==1) {
#    for my $k (keys %ENV) {print enc("$k=$ENV{$k}")."<br/>\n";}
    $self='leitallt.pl';
    $forbidden=1;
    if (open IP,"< $root/.ipnumer") {
	while (<IP>) {
	    /^(\S+)/g;
	    if ($1 eq $clientip) {
		$forbidden=0;
		last;
	    }
	}
    }
    $allt=1;
}

#do_vidhald();exit(0);

if ($forbidden) {
    do_forbidden();
} elsif ($vars{saekja1}) {
    do_lemma(1,$vars{lemma1});
} elsif ($vars{saekja2}) {
    do_lemma(2,$vars{lemma2});
} elsif ($vars{info}) {
    doinfo($vars{info})
} elsif ("$vars{hreinsa1}$vars{hreinsa2}") {
    do_leitform(%vars)
} elsif ($vars{hreinsaform}) {
    do_leitform()
} elsif ($vars{hreinsaform}) {
    do_leitform()
} elsif ($vars{leita}) {
    do_leita()
} elsif ($vars{heim}) {
    do_heim($vars{heim})
} elsif ($allt && $vars{hs}) {
    do_heimlist()
} elsif ($allt && $vars{heimleit}) {
    do_heimlist()
} elsif ($allt && $vars{heimed}) {
    do_heimed($vars{heimed})
} elsif ($allt && $vars{delheim}) {
    do_delheim()
} elsif ($allt && $vars{geymheim}) {
    do_geymheim()
} else {
    do_leitform()
}

exit(0);

sub do_vidhald {
    head('Corpus',0);
    print qq(<br/><br/>);
    print qq(Þessi síða er lokuð vegna viðhalds); 
    print qq(<br/><br/>);
    print qq(Reyndu aftur fljótlega<br/>);
}

sub do_forbidden {
    head('Corpus',0);
    for my $k (keys %ENV) {
#	print qq(env: $k=[$ENV{$k}]<br/>\n);
    }
    print qq(<br/><br/>);
    print qq(Aðgangur að þessari útgáfu er ekki leyfður frá IP-tölu $clientip); 
    print qq(<br/><br/>);
    print qq(Opin útgáfa <a href="leit.pl">er hér</a><br/>);
}

sub do_leitform {
    my %v=@_;
    head('Leit',1);
    print qq(<form action="$self" accept-charset="UTF-8">\n);
    
    print qq(<table class="linkar">);
    print qq(<tr><td>
	<a href="$self?info=4">Help</a>);
    print qq(</td><td align="right">
	<a href="$self?info=3">Leiðbeiningar</a>
	&nbsp;&nbsp;
	<a href="$self?info=1">Um textasafnið</a>
	&nbsp;&nbsp;
	<a href="$self?info=2">Ritaskrá</a>
	&nbsp;&nbsp;
	<a href="http://www.arnastofnun.is/page/hofundarrettur_tilvitnun" target="_new">Tilvitnun í efni Textasafns</a>
	</td></tr></table><br style="clear:both">\n);

    print qq(<div class="ordval">);
    for my $num (1,2) {
	if ($v{"hreinsa$num"}) {
	    $v{"ser$num"}='';
	    $v{'lemma'.$num}='';
	}
	print qq(<div class="lemma">Orð $num: &nbsp;);
	print qq(<input name="lemma$num" value="$v{'lemma'.$num}"/>);
	print qq(&nbsp;<input type="submit" name="saekja$num" value="Sækja orðmyndir"/>);
	print qq(&nbsp;<input type="submit" name="hreinsa$num" value="Hreinsa"/><br/>);
	if ($v{"ser$num"}) {
	    print qq(<input type="hidden" name="ser$num" value="$v{'ser'.$num}"/>);
	    my $hc=0;
	    for my $omr (split/~/,$v{"ser$num"}) {
		$hc++;
		print qq(<div class="homonym">);
		my @omr=split/:/,$omr; # " (rnum ord, ofl];mynd;mynd...)"
		my $hh=shift @omr;
		my $ch=(($v{sethom}eq $num) || $v{"m$num$hc"})?' checked':'';
		print qq(<div class="homolemm"><input id="m$num$hc" name="m$num$hc" type="checkbox" value="1"$ch onchange="toggleh('$num$hc')">$hh</div>);
		my $c=0;
		for my $om (@omr) {
		    $c++;
		    my $id="m$num$hc$c";
		    my $ch=(($v{sethom} eq $num) || $v{$id})?' checked':'';
		    print qq(<div class="ordmynd"><input name="$id" id="$id" type="checkbox" value="1"$ch >$om</div>);
		}
		print qq(<div></div><br style="clear:both"/>);
		print qq(</div>);
	    }
	} else {
	    if ($v{sethom} eq $num) {
		print qq(<div class="homonym">Engar orðmyndir fundust.<br/>Leitin mun takmarkast við );
		if ($v{"lemma$num"}=~/ /) {print qq(þessi orð.</div>);}
		else {print qq(þetta orð.</div>);}
	    }
	}
	print qq(</div>);
    }
    print qq(</div>);


    print qq(<table width="100%">);
    print qq(</table>\n);

    
    print qq(<input class="leittakki" type="submit" name="leita" value="Leita"/><br/>);

    print qq(Efnisflokkar:&nbsp;<br/>);
    print qq(<input type="checkbox" id="f" value="0" onchange="toggle()"><span class="flokkY">Allir flokkar</span><br/>\n);

    print qq(<script type="text/javascript">\n);
    print qq( var state=0;document.getElementById('f').checked=0;);
    print qq(</script>\n);
    my %vfl=map {$_=>1} split/\x00/,$v{flokkar};
    my $flfile="$root/.".(($allt==1)?"flokkar":"opnirflokkar");
    #print STDERR "flfile: $flfile\n";
    open (FL,"< $flfile") or print STDERR "arg:$!\n";
    binmode FL,':bytes';
    my $pl='';
    my $cnt=0;
    my $id=1;
    print q(<div class="flokkar">);
    
    while (<FL>) {
	chomp;
	next if /^#/;
	next if /^\s*$/;
	my ($lev,$fl,$att,$txt,$hover)=split/:/;
        $hover=qq( title=").enc($hover).qq(") if $hover;
	$txt ||= $fl;
	# $lev: F=flokkur Y=Yfirflokkur U=Undirflokkur
	if (($cnt==4) || (($cnt !=0) && ($lev eq 'Y'))) {
	    print qq(<br/>);
	    $cnt=0;
	}
	if (($pl eq 'Y') && ($lev ne 'U')) {
	    print qq(</div>);
	    print qq(<script type="text/javascript">\n);
	    print qq( flarr['y$py']=$id;\n);
	    print qq(</script>\n);
	    $cnt=0;
	}
	my $spancl="flokk$lev";
	if ($lev eq 'Y') {
	    $py=$id;
	    print qq(<input id="$id" class="yfirflokkur" type="checkbox" value="1" onchange="togglef($id)" >);
	    print qq(<span$hover class="$spancl">$txt</span>);
	    print qq(&nbsp;<span class="fl">$att</span>) if $att;
	    print qq(<br/><div class="yfirflokkur">);
	    $cnt=-1;
	} else {
	    my $s=" checked" if $vfl{$fl} && $lev==0;
	    my $cl=($pl eq 'F')?'flokkur':'undirflokkur';
	    print qq(<input id="$id" type="checkbox" name="flokkar" value="$fl"$s><span$hover class="$spancl">$txt</span>&nbsp;);
	    print qq(<span class="fl">$att</span>) if $att;
	}
        $id++;
        $cnt++;
	$pl=$lev if $lev ne 'U';
#	$cnt=0 unless $lev; 
    }
    if ($pl eq 'Y'){
	print qq(</div>);
	print qq(<script type="text/javascript">\n);
	print qq( flarr['y$py']=$id;\n);
	print qq(</script>\n);
    }
    
    print qq(</div>\n);
    close FL;
    print qq(<br><input class="leittakki" type="submit" name="leita" value="Leita"/><br/>);
#    print qq(</td></tr>\n);



#    print qq(</table>);
    print qq(</form><br>\n);
#      print qq(<img src="bordi.jpg" alt="jpg">);

#    print qx(cat $webdir/leidbeiningar.txt);
    
#    print qq(<img src="bordi.png" width="100%" alt="png">);
    print qq(<div id="bordi"></div>);

    print qq(</body>);
    print qq(</html>);
}

sub httpheader {
    if ($apache) {
#	$req->content_type('text/html;charset=iso-8859-1');
	$req->content_type('text/html;charset=utf-8');
#        $req->headers_out->set('Set-Cookie' => $user{cookie}) if $user{cookie};
	$req->send_http_header;
    } else {
	print $cgi->header();#('-cookie'=>$user{cookie});
    }
}

sub head {
      my $titill=shift;
      my $script=shift;
      # html haus, sameiginlegur
      print $cgi->header('text/html;charset=utf-8');
      print qq(<html><head>);
      print qq(<meta http-equiv="Cache-Control" content="max-age=0"/>);
      print qq(<title>$TITILL - $titill</title>);
      print qq(<link rel="stylesheet" href="corpus.css"/>\n);
      if ($script) {
	  print qq(<script type="text/javascript">\n);
	  print qq(var flarr=[];\n);
          print qq(function toggle() {var num=1;state=1-state;while ( document.getElementById(num)) {document.getElementById(num).checked=state;num++; }\n
}\n);
          print qq(function toggleh(i) {var num=1;var n="m"+i;var st=document.getElementById(n).checked;while ( document.getElementById(n+num)) {document.getElementById(n+num).checked=st;num++; }\n
}\n);
          print qq(function togglef(i) {var num=i+1;var n="m"+i;var st=document.getElementById(i).checked;while ( num<flarr['y'+i]) {document.getElementById(num).checked=st;num++; }\n
}\n);
          print qq(</script>\n);
      }
      print qq(</head>\n);
    
      print qq(<body>\n);
print qq(
<div class="belti">
<img src="$hmynd"/>
<h1>$haus2</h1>
<h2>$haus1</h2>
</div>
);
    
#      print qq(<table border="0" cellpadding="5" align="center" width="100%"><tr>);
#      print qq(<td hewight="100" wiadth="150" align="right"><img src="textfin2.png" width="120" alt=""></td>);
#      print qq(<td align="center"><font size="+2">Orðabók Háskólans</font><br><font size="+3">Textasafn</font><br></td>);
#      print qq(<td heiwght="100" widath="150" align="left"><img src="textfin2.png" width="120" alt=""></td>);
#      print qq(</tr></table><br>\n);
    }

sub do_lemma {
    my ($num,$lemm)=@_;
    $lemm=~s/ .*//;
    $lemm=ilc($lemm);
    my $m=getmyndir($lemm,'',0);
    my @m=@$m;
    $ser=join '~', map {join ':',@$_} @$m;
#    $m=[[$lemm]] unless (@$m);
    
#    $vars{om}=$m;
#    do_leitform((%vars,"ser$num"=>$ser,"om$num"=>$m));
    do_leitform((%vars,'sethom'=>$num,"ser$num"=>$ser));
}

sub do_leita {
    my $r;
    my %myndir1;
    my %myndir2;
    my %m;
    for my $num (1,2) {
	$m{$num}={};
	if ($vars{"lemma$num"}) {
	    my @lemm=split/ +/,ilc($vars{"lemma$num"});
	    if ($vars{"ser$num"}) {
		my $hc=0;
		for my $omr (split/~/,$vars{"ser$num"}) {
		    $hc++;
		    my @omr=split/:/,$omr; # " (rnum ord, ofl];mynd;mynd...)"
		    my $hh=shift @omr;
		    my $c=0;
		    for my $om (@omr) {
			$c++;
			my $id="m$num$hc$c";
#			print STDERR " test om=$om id=$id\n";
			if ($vars{$id}) {
			    $m{$num}{$om}=1;
#			    print STDERR " selectecd om=$om\n";
			}
		    }
		}
		shift @lemm;
	    }
	    $m{$num}{$_}=1 for @lemm;
	}
    }
#    print STDERR qq(c=$r m$r=$vars{"m$r"}\n);
    do_leita_([keys %{$m{1}}],[keys %{$m{2}}],$vars{offs},$vars{sort});
}

# nýtt:
# do_leita(\@myndir1,\@myndir2,$offs,$sort)
sub do_leita_ {
    my $myndir1=shift;
    my $myndir2=shift;
    my $offs=shift;
    my $sort=shift;
#    print STDERR "do_leit offs=$offs sort=$sort\n";
    print STDERR " myndir1=[".join(' ',@$myndir1)."]\n";
    print STDERR " myndir2=[".join(' ',@$myndir2)."]\n";

    my $nyleit=qq(\n<form action="$self"><input style="font-size: 120%" type="submit" name="hreinsaform" value="Ný leit"/></form>\n);

#    print qq(<html><body bgcolor="#fffdea">\n);
    head('Niðurstöðu leitar');
    my @fl=$cgi->param(flokkar);
    my $startt=time();
    
    my $skrar=getrows(\@fl,$myndir1,$myndir2); #[$verk,$ord,$pos,$ord2,$pos2] 
    print STDERR "selecttime:".(time()-$startt)."\n";
#    print STDERR "num:".scalar(keys(%$skrar))."\n";
    my $fc;
    my $lc;
    my @list;
    my @skrar=keys %$skrar;
    my $tfc=@skrar;
    my $startt=time();
#    $UNDAN=30;
#    $EFTIR=40;
    # create list of [$skra,$before,$key,$after,$verk]
    for my $skra (sort @skrar) { # [$verk,$ord,$pos,$ord2,$pos2]
	next if $fc++<$offs;
#	print STDERR "  formattera $skra:\n";
	my @items=@{$skrar->{$skra}};
	open F,"< $root$skra" or die "gat ekki lesið '$skra': $!";
	for my $item (@items) {
	    my ($undan,$eftir,$key,$keybytes,$c,$p,$p0);
	    my ($verk,$ord,$pos,$ord2,$pos2)=@$item;
#	    print STDERR "     formattera $skra:($verk,$ord,$pos,$ord2,$pos2)\n";
	    seek F,$pos,0;
	    if (@$myndir2) {
		$keybytes=$pos2-$pos+length($ord2);
	    } else {
		$keybytes=length($ord);
	    }
	    binmode F,':bytes';
	    read F,$key,$keybytes; # $key is bytes
	    utf8::decode($key); # $key is utf8
	    $key=~s/\s+/ /g;
	    binmode F,':utf8';
	    $c=' ';
	    while (($c ne '') && (length("$key $eftir")<$EFTIR)) {
		read F,$c,$EFTIR-length("$key $eftir")+10;
		$eftir.=$c;
		$eftir=~s/\s+/ /g;
	    }
	    $eftir=~s/===.*//;
	    $eftir=substr($eftir.(' 'x$EFTIR),0,$EFTIR-length($key)-1);
	    $p=$pos;
	    while (length($undan)<$UNDAN-1) {
		seek F,$p-($UNDAN-length($undan))-2,0;
		read F,$c,1;
		$p0=tell(F);
		my $u;
		while (tell(F)<$p) {
		    read F,$c,1;
		    $u.=$c;
		}
		$p=$p0;
		$undan="$u$undan";
		$undan=~s/\s+/ /g;
	    }
#	    $undan=reverse $undan;
            my @x=split/===/,$undan;
	    $undan=pop @x;
	    $undan=substr((' 'x$UNDAN).$undan,-$UNDAN+1);
	    # gera sort key. sort=0=verk 1=fyrri, 2 key 3=eftir
	    $fyrri=reverse $undan;
	    my $naesta;
	    {
		use utf8;
		$fyrri=~s/([-a-zA-ZþæðöÞÆÐÖáéýúíóÁÉÝÚÍÓ]+).*/$1/;
		$fyrri=reverse $fyrri;
		($naesta)=$eftir=~/([-a-zA-ZþæðöÞÆÐÖáéýúíóÁÉÝÚÍÓ].*)/;
	    }
	    my $sortkey="$verk $key $eftir";
	    if ($sort==1) {$sortkey="$fyrri $key $naesta";}
	    elsif ($sort==2) {$sortkey="$key $naesta";}
	    elsif ($sort==3) {$sortkey="$eftir";}
#	    utf8::encode($undan);
#	    utf8::encode($key);
#	    utf8::encode($eftir);
#	    print STDERR "lina:[$undan][$key][$eftir] fyrri=[$fyrri]\n";
#	    print STDERR " sortkey:[$sortkey]\n";
	    push @list,[$sortkey,$undan,$key,$eftir,$verk,$skra];
	    $lc+=1;
	}
	close F;
	last if ($lc>=$MAXLIN) || ($fc-$offs>$MAXFILES) || (time()-$startt>$MAXTIME);
    }
#    die "test!";
#    print STDERR "greptime ($lc/$fc-$offs):".(time()-$startt)."\n";
    my $next=($fc<$tfc)?$fc:'';
    my $sortl; 
    my $tot;
    my $out;
    my $qs;
#    print STDERR "bp0\n";
    if (scalar @list) {
#    my @list=sort {$a->[0] cmp $b->[0]} @list;
	@list=map {$_->[0]}
	sort {$a->[1] cmp $b->[1]}
	map {[$_,ditrans($_->[0])]} @list;


	%ht=('&'=>'&amp;','<'=>'&lt;','>'=>'&gt;','"'=>'&quot;',"'"=>'&apos;',"\t"=>' ');
	for my $res (@list) {
	    my $b=$res->[1];
	    my $o=$res->[2];
	    my $a=$res->[3];
	    $v=$res->[4];
	    $b=~s/([\&<>\"\'\t])/$ht{$1}/g;
	    $a=~s/([\&<>\"\'\t])/$ht{$1}/g;
	    $l=qq[$b <font color="red">$o</font> $a];
	    utf8::encode($l);
	    $l.=qq[ (<a href="$self?heim=].urlenc($v).qq[">$v</a>)];
	    $out.="$l\n";
	}
#    my $of=" af $tfc" if $fc-$offs<$tfc;
	my $of=" af $tfc";
#    my $tot= "\n".scalar(@list)." orð fundin í ".($fc-$offs)."$of textum\n\n";
	$tot= "\n".scalar(@list)." orð fundin í textum ".($offs+1)."-".($fc)."$of\n\n";
	$qs="leita=1\&lmax=1\&l1=1";
	for my $k (keys %vars) {
	    $qs.="\&$k=".urlenc($vars{$k}) if $k=~/^m\d\d/;
	    $qs.="\&$k=".urlenc($vars{$k}) if ($k=~/^ser\d/) || ($k=~/^lemma\d/);
	}
	
	$qs.="\&flokkar=".urlenc($_) for (@fl);
	$sortl="Raða þessari síðu eftir: &nbsp;";
	$sortl.=qq(<a href="$self?offs=$offs\&sort=2\&$qs">Orðmynd</a>&nbsp;&nbsp;&nbsp;);
	$sortl.=qq(<a href="$self?offs=$offs\&sort=1\&$qs">Undanfarandi orði</a>&nbsp;&nbsp;&nbsp;);
	$sortl.=qq(<a href="$self?offs=$offs\&sort=3\&$qs">Eftirfarandi orði</a>&nbsp;&nbsp;&nbsp;);
	$sortl.=qq(<a href="$self?offs=$offs\&sort=0\&$qs">Textaskrá</a> (sjálfgefið));
    } else {
	$sortl='';
	$tot="\n".scalar(@list)." orð fundin\n\n";
	$out='';
    }
    print qq(<table align="right"><tr>
        <td valign="top" style="padding-right: 50px">
	<a href="http://www.arnastofnun.is/page/hofundarrettur_tilvitnun" target="_new">Tilvitnun í efni Textasafns</a>
        </td><td>$nyleit</td></tr></table>);
    print qq(<br>);
    print qq($tot<br><br>\n$sortl<br>\n);
    print qq(<pre>\n);
    print qq($out\n);
#    print STDERR "bp3\n";

    print qq(</pre>\n);
    print qq($tot<br><br>$sortl<br><br>\n) if @list;
    if ($next) {
	print qq(<a href="$self?offs=$next\&$qs\&sort=$sort">Næsta síða</a><br/><br/>);
    }
    print qq($nyleit);
    print qq(</body>);
    print qq(</html>\n);
}

sub doinfo {
    my ($i)=@_;
    my $r={
	    1=>['um.txt','Um textasafnið'],
	    2=>['ritaskra.txt','Ritaskrá'],
	    3=>['leidbeiningar.txt','Leiðbeiningar'],
	    4=>['help.txt','Help'],
          }->{$i};
    my $texti = qx(cat $webdir/$r->[0]);
    if ($r->[0]=~/\.html$/) {
	$cgi->header('text/html;charset=utf-8');
	#httpheader();
	print $texti;
    } else {
	head($r->[1]);
	print qq(<table class="linkar">);
	print qq(<tr><td align="right">
        <a href="$self">Til baka í leit</a>
        </td></tr></table><br style="clear:both">\n);

	print qq(<center><table class="info"><tr><td>$texti</td></tr></table></center>);
	print qq(</body>);
	print qq(</html>);
    }
}

sub dogrep {
    my ($skra,$verk,@ords)=@_;

    my ($isgz,$isht);
    $isgz=1 if $skra=~/\.gz$/;
    $isht=1 if $skra=~/\.htm[^\/]*$/;

    my $ords=join '\|',@ords;
    my $tmp="$root$skra";
    if ($isgz) {
	$tmp="/tmp/corpus.$$";
	system("zcat -f $root$skra > $tmp");
    }
    my $grep=q[LANG=is_IS.UTF-8 grep -i -b -o '\b\(].(join'\|',@ords).q[\)\b' '].$tmp.q['];
    print STDERR "þetta grepcommand=[$grep]\n";
    my @grep=qx($grep);
    chomp @grep;
    print STDERR "[$skra] grep=",join("\n+",@grep)."\n-----\n";
    open SKRA,"< $tmp" or die "gat ekki lesið skrá $tmp:$!";
    my @list;
    for my $line (@grep) {
	my ($b,$t)=split/:/,$line,2;
        my $len=length($t);
	print STDERR "grepline b=$b t=$t l=$len\n";
	my $r=(join'|',@ords);
	my $start=$b-$UNDAN*2;
	$start=0 if $start<0;
	my $end=$b+length($t)+$EFTIR*2;
	my $buf;
	seek SKRA,$start,0;
	my $len=read SKRA,$buf,$end-$start;
        
	$buf=~s/[\n\r]/ /g;
	# print STDERR "line=[$line]\nt=[$t]\nr=[$r]\n";
        $t=~s/[^a-zA-Z\xc0-\xff]/ /g;
	$t=~tr/A-Z\xc0-\xdf/a-z\xe0-\xff/;
	$t=" $t ";
#print STDERR "split [$t]/[$r]\n";
	while ($t=~/ ($r) /g) {
	    my $o=$-[0];
#	    print STDERR "  match:[$1][".substr($buf,($b-$start)+$o,length($1))."]\n";
	    push @list,[$skra,
                        substr($buf,0,($b-$start)+$o),
                        substr($buf,($b-$start)+$o,length($1)),
                        substr($buf,($b-$start)+$o+length($1)),
                        $verk,$1];
	}
    }
    close(SKRA);
    unlink $tmp if $isgz;
    @list;
}

sub ilc {
    my ($o)=@_;
    $o=~tr/A-ZÁÉÝÚÍÓÞÆÐÖ/a-záéýúíóþæðö/;
    $o;
}

sub getrows {
    my ($fl,$ord1,$ord2)=@_;
    # $fl er arrayref flokka
    # $ord1 og $ord2 eru arrayref orðmynda
    my $qm1=(join ',',map {'?'} @$ord1) || "'__'";
    my $qm2=(join ',',map {'?'} @$ord3);
    
    my %skrar;
    my @fl=@$fl;
# print STDERR "fl[]=[@fl]\n";
    my %fl=map {$_ => 1} @fl?@fl:();
    my $wfl;
    my $ffl;
# print STDERR "fl-=($fl{'-'})\n";
    $flp=(join ',', map {'?'} @fl) || '__';

    $dbh->do("set enable_seqscan = 0");

    my $sql=qq{select * from (
                 select distinct skra,verk,ord,pos,naesta,pos2 from ord join textar using (skrid) join flokkar using (skrid) 
	           where naesta IN ($qm1)};
    $sql.=           " and ord IN (".(join ',',map {'?'} @$ord2).")\n" if @$ord2;
    $sql.=        qq{  and flokkur IN ($flp) limit 2000
                 ) as foo  
	           order by verk;
	       };
    print STDERR qq(getrows sql=[$sql] \() . join(' , ',@$ord1,@$ord2,@fl) .qq[)\n];
    my $sth=$dbh->prepare($sql);
    $sth->execute(@$ord1,@$ord2,@fl);
    my $c=0;
    while (my ($skra,$verk,$ord2,$pos2,$ord,$pos)=$sth->fetchrow_array) {
	push @{$skrar{$skra}},[$verk,$ord,$pos,$ord2,$pos2];
	$c++;
#	print STDERR "fann [$verk,$ord,$pos,$ord2,$pos2]\n";
    }
    $sth->finish();
    print STDERR "fann $c items, ".scalar(keys %skrar)." skrár\n";
#    die "test";
    \%skrar;
}

sub getflokka {
    my @fl;

    my $sql=qq{select distinct flokkur from flokkar order by flokkur};
#print "sql=[$sql]<br>\n";
    my $sth=$dbh->prepare($sql);
    $sth->execute();

    while (my ($f)=$sth->fetchrow_array) {
	push @fl,$f;
    }
    $sth->finish();
    @fl;
}
    
sub getmyndir {
    my ($ord,$ofl,$allar)=@_;
    my @m;
    # retunr [[ord,ofl,num,mynd,mynd...],...]
    my $sql=qq{select ord,ofl,rnum,ordmynd from myndir2 where ord=?};
    $sql .=qq( and ofl like ?) if $ofl ne '';
    $sql .=qq( and teg is NULL) unless $allar;
    $sql.=" order by ord,ofl,rnum,ordmynd";   
 print STDERR "getmyndir sql=$sql<br/>\nord=[$ord] ofl=[$ofl]<br/>\n";
    my $sth=$dbh->prepare($sql);
    $sth->execute($ord,($ofl ne '')?("$ofl%"):());
    my $pn='-';
    while (my ($o,$of,$r,$f)=$sth->fetchrow_array) {
	my $n="$o, $of";
	$n="$r $n" if $r;
#        print STDERR "mynd: [$f][$o][$of][$r][$n]\n";
	push @m,[$n] if $n ne $pn;
	push @{$m[-1]},$f;
	$pn=$n;
    }
    $sth->finish();
    \@m;
}

sub do_heim {
    my ($verk)=@_;
    my $sql=qq{select * from heim where verk=?};
    my $sth=$dbh->prepare($sql);
    $sth->execute($verk);
    my $res=$sth->fetchrow_hashref();
    head('Heimildir');
    print qq(<br/><br/>&nbsp;&nbsp;);
    print qq(<table style="margin-left: 4em" border="1" bgcolor="#ffffff" cellpadding="3">);
    print qq(<tr><td style="background-color: #e4ffe0">&nbsp;Titill&nbsp;</td><td>&nbsp;).enc($res->{titill}).qq(&nbsp;</td></tr>);
    print qq(<tr><td style="background-color: #e4ffe0">&nbsp;Höfundur&nbsp;</td><td>&nbsp;).enc($res->{hof}).qq(&nbsp;</td></tr>);
    print qq(<tr><td style="background-color: #e4ffe0">&nbsp;Þýðandi&nbsp;</td><td>&nbsp;).enc($res->{trans}).qq(&nbsp;</td></tr>);
    print qq(<tr><td style="background-color: #e4ffe0">&nbsp;Útgáfuár&nbsp;</td><td>&nbsp;).enc($res->{artal}).qq(&nbsp;</td></tr>);
    print qq(<tr><td style="background-color: #e4ffe0">&nbsp;Útgefandi&nbsp;</td><td>&nbsp;).enc($res->{utgefandi}).qq(&nbsp;</td></tr>);
    print qq(<tr><td style="background-color: #e4ffe0">&nbsp;Efnisflokkur&nbsp;</td><td>&nbsp;).enc($res->{flokkur}).qq(&nbsp;</td></tr>);
    if ($res->{ath}) {
    print qq(<tr><td style="background-color: #e4ffe0">&nbsp;Athugasemd&nbsp;</td><td>&nbsp;).enc($res->{ath}).qq(&nbsp;</td></tr>);
}
    print qq(</table>);
    print qq(</body>);
    print qq(</html>);
    $sth->finish();
}

sub do_heimed {
    my ($verk)=@_;
    my $sql=qq{select * from heim where verk=?};
    my $sth=$dbh->prepare($sql);
    $sth->execute($verk);
    my $res=$sth->fetchrow_hashref();
    head('Heimildir');
    print qq(<br/><br/>);
    print qq(<form action="$self">);
    print qq(<input type="hidden" name="verk" value=\").enc($verk).qq(\">);
    print qq(<input type="hidden" name="f" value=\").enc($vars{f}).qq(\">);
    print qq(<input type="hidden" name="o" value=\").enc($vars{o}).qq(\">);
    print qq(<input type="hidden" name="r" value=\").enc($vars{r}).qq(\">);
    print qq(<table class="heimed">);
    print qq(<tr><td class="pr">Titill</td><td><input class="i" type="text" name="titill" value=\").enc($res->{titill}).qq(\"></td></tr>);
    print qq(<tr><td class="pr">Höfundur</td><td><input class="i" type="text" name="hof" value=\").enc($res->{hof}).qq(\"></td></tr>);
    print qq(<tr><td class="pr">Þýðandi</td><td><input class="i" type="text" name="trans" value=\").enc($res->{trans}).qq(\"></td></tr>);
    print qq(<tr><td class="pr">Útgáfuár</td><td><input class="i" type="text" name="artal" value=\").enc($res->{artal}).qq(\"></td></tr>);
    print qq(<tr><td class="pr">Útgefandi</td><td><input class="i" type="text" name="utgefandi" value=\").enc($res->{utgefandi}).qq(\"></td></tr>);
    print qq(<tr><td class="pr">Efnisflokkur</td><td><input class="i" type="text" name="flokkur" value=\").enc($res->{flokkur}).qq(\"></td></tr>);
    print qq(<tr><td class="pr">Athugasemd</td><td><input class="i" type="text" name="ath" value=\").enc($res->{ath}).qq(\"></td></tr>);
    print qq(<tr><td>&nbsp;</td><td><input style="float:right" type="submit" name="delheim" value="Eyða">);
    print qq(<input type="submit" name="geymheim" value="Geyma"></td></tr>);
    print qq(</table>);
    print qq(</form>);
    print qq(</body>);
    print qq(</html>);
    $sth->finish();
}

sub do_geymheim {
    if ($vars{verk} eq '') {
	$vars{err}='Vantar verk';
    } else {
	if ($vars{nytt} ==1) {
	    $dbh->do("insert into heim (titill,hof,trans,artal,utgefandi,flokkur,verk,ath) values (?,?,?,?,?,?,?)",{},$vars{titill},$vars{hof},$vars{trans},$vars{artal},$vars{utgefandi},$vars{flokkur},$vars{verk},$vars{ath});
	} else {
	    $dbh->do("update heim set titill=?,hof=?,trans=?,artal=?,utgefandi=?,flokkur=?,ath=? where verk=?",{},$vars{titill},$vars{hof},$vars{trans},$vars{artal},$vars{utgefandi},$vars{flokkur},$vars{ath},$vars{verk});
	}
    }
    do_heimlist();
}
   
sub do_delheim {
    $dbh->do("delete from heim where verk=?",{},$vars{verk});
    do_heimlist();
}
   
sub do_heimlist {
    # notar o=xx
    my %of=(''=>'verk',v=>'verk',undef=>'verk',h=>'hof',t=>'titill',u=>'artal',f=>'flokkur',th=>'trans',U=>'utgefandi',A=>'ath');
    my $desc='';
    $desc='DESC' if $vars{r};
    %r=($vars{o}=>1);
    my $sql;
    my $where='';
    my @wh=();
    my @par=();
    my $qs="hs=1";
    my $full=1;
    for my $c (qw(verk hof titill artal flokkur trans utgefandi ath)) {
	if ((my $v=$vars{$c}) ne '') {
            $full=0;
	    my $t;
	    if ($v=~s/\*/%/g) {
		$t="$c like ?";
	    } else {
		$t="$c=?";
	    }
	    push @wh,$t;
	    push @par,$v;
	    $qs.="\&$c=".urlenc($vars{$c});
	}
    }
    $where=join(' and ',@wh);
    $where="where $where" if $where;

    my $f=0+$vars{f};
    $sql=qq[select * from heim $where order by $of{$vars{o}} $desc NULLS LAST, verk limit 51 offset $f];
    print STDERR "sql=[$sql](@par)\n";
    my $res=$dbh->selectall_arrayref($sql,{Slice=>{}},@par);
    head('Heimildir');
    if ($f>0) {
	my $p=($f>50)?$f-50:0;
	print qq(<a href="$self?$qs&f=$p&r=$vars{r}&o=$vars{o}">Til baka</a>&nbsp;&nbsp;);
    }
    if (@$res>50) {
	my $n=$f+50;
	print qq(<a href="$self?$qs&f=$n&r=$vars{r}&o=$vars{o}">Áfram</a>);
    }
    print qq(<br>);
    print qq(<table class="heimildaskra">);
    print qq(<tr>);
    print qq(<th><a href="$self?$qs&o=v&r=$r{v}">Verk</a></th>);
    print qq(<th><a href="$self?$qs&o=t&r=$r{t}">Titill</a></th>);
    print qq(<th><a href="$self?$qs&o=h&r=$r{h}">Höfundur</a></th>);
    print qq(<th><a href="$self?$qs&o=th&r=$r{th}">Þýðandi</a></th>);
    print qq(<th><a href="$self?$qs&o=u&r=$r{u}">Útgáfuár</a></th>);
    print qq(<th><a href="$self?$qs&o=U&r=$r{U}">Útgefandi</a></th>);
    print qq(<th><a href="$self?$qs&o=f&r=$r{f}">Efnisflokkur</a></th>);
    print qq(<th><a href="$self?$qs&o=A&r=$r{A}">Athugasemd</a></th>);

    print qq(</tr>\n);
    my $n=@$res;
    $n=50 if $n==51;
    for my $l (0..$n-1) {
	my $r=$res->[$l];
	my $ed="$self?o=$vars{o}&f=$vars{f}&heimed=".urlenc($r->{verk});
	print qq(<tr>);
	print qq(<td><a href="$ed">).enc($r->{verk})."</a></td>";
	print qq(<td>).enc($r->{titill})."</td>";
	print qq(<td>).enc($r->{hof})."</td>";
	print qq(<td>).enc($r->{trans})."</td>";
	print qq(<td>).enc($r->{artal})."</td>";
	print qq(<td>).enc($r->{utgefandi})."</td>";
	print qq(<td>).enc($r->{flokkur})."</td>";
	print qq(<td>).enc($r->{ath})."</td>";
	print qq(</tr>\n);
    }
    print qq(</table>);
    if ($f>0) {
	my $p=($f>50)?$f-50:0;
	print qq(<a href="$self?$qs&f=$p&r=$vars{r}&o=$vars{o}">Til baka</a>&nbsp;&nbsp;);
    }
    if ($f+50<@$res) {
	my $n=$f+50;
	print qq(<a href="$self?$qs&f=$n&r=$vars{r}&o=$vars{o}">Áfram</a>);
    }
    if ($full ==0) {
	print qq(&nbsp;&nbsp;<a href="$self?hs=1">Til baka í allan listann</a>);
    }
    if ($vars{err}) {
	print qq(<br><br>Villa: ).enc($vars{err});
	print qq(<br><br>);
    }
    print qq(<br><br>Ný færsla eða leit:<br>);
    print qq(<form action="$self">);
    print qq(<input type="hidden" name="nytt" value="1">);
    print qq(<input type="hidden" name="f" value=\").enc($vars{f}).qq(\">);
    print qq(<input type="hidden" name="o" value=\").enc($vars{o}).qq(\">);
    print qq(<input type="hidden" name="r" value=\").enc($vars{r}).qq(\">);
    print qq(<table class="heimed">);
    print qq(<tr><td class="pr">Verk</td><td><input class="i" type="text" name="verk" value="$vars{verk}"></td></tr>);
    print qq(<tr><td class="pr">Titill</td><td><input class="i" type="text" name="titill" value="$vars{titill}"></td></tr>);
    print qq(<tr><td class="pr">Höfundur</td><td><input class="i" type="text" name="hof" value="$vars{hof}"></td></tr>);
    print qq(<tr><td class="pr">Þýðandi</td><td><input class="i" type="text" name="trans" value="$vars{trans}"></td></tr>);
    print qq(<tr><td class="pr">Útgáfuár</td><td><input class="i" type="text" name="artal" value="$vars{artal}"></td></tr>);
    print qq(<tr><td class="pr">Útgefandi</td><td><input class="i" type="text" name="utgefandi" value="$vars{utgefandi}"></td></tr>);
    print qq(<tr><td class="pr">Efnisflokkur</td><td><input class="i" type="text" name="flokkur" value="$vars{flokkur}"></td></tr>);
    print qq(<tr><td class="pr">Athugasemd</td><td><input class="i" type="text" name="ath" value="$vars{ath}"></td></tr>);
    print qq(<tr><td>&nbsp;</td><td>);
    print qq(<input style="float:right" type="submit" name="heimleit" value="Leita">);
    print qq(<input type="submit" name="geymheim" value="Geyma"></td></tr>);
    print qq(</table>);
    print qq(</form>);
    print qq(</body>);
    print qq(</html>);
}

sub enc {
    my $t=shift;
    $t=~s/\&/\&amp;/g;
    $t=~s/\</\&lt;/g;
    $t=~s/\>/\&gt;/g;
    $t=~s/\"/\&quot;/g; # dummy comment to fool emacs syntax-highlighting
    $t;
} 

sub unenc {
    my $t=shift;
    # vantar, ónotað
}

sub urlenc {
    my $t=shift;
    $t=~s/([^a-zA-Z0-9_\.-])/uc sprintf("%%%02x",ord($1))/eg; # urlencode
    $t;
}

sub urldec {
    my $t=shift;
    $t=~s/%(..)/chr(hex($1))/eg;
    $t;
}

sub ditrans {
  my $o=shift;
  $o=~s/(.)/$disort{$1}/g;
  $o;
}


