#!/usr/bin/perl -w

use XML::Sablotron::DOM qw( :constants :functions );
use XML::Sablotron;
use CGI qw(:standard :cgi-lib);
use strict;

open LOG,">log";

my $count=0;
my $f; my $content;


my $res="";
my $result="";
my %in;
CGI::ReadParse(\%in);

undef $/;


sub processindex
{
  print "Processing Index $_[0] ...\n";
  my $name=$_[0]; $name=~s{/*$}{}; $name=~s{^.*/}{};
  my $base=$_[0]; $base=~s{/*$}{}; $base=~s{\..*?$}{};

  my $style= <<EOF
<?xml version='1.0' encoding='iso-8859-1'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes"/>
  <xsl:param name="id"/>
<xsl:template match="/root">
EOF
;

  open IN,"<$_[0]/.index.xml";
  $style.=<IN>;
  close IN;
  $style.="</xsl:template></xsl:stylesheet>";  

  print "Style:\n$style\n\n\n";


  foreach my $file (<$base/*>)
  {
    my $id=$file; $id=~s{$base/}{};
    my @parameter;
    @parameter=%in;
    push @parameter,"id",$id;

    my $content="<root>";
    open IN,"<$file";
    $content.=<IN>;
    close IN;
    $content.="</root>";

#    print "Vorher:\n$content\n\n\n";

    $res = XML::Sablotron::Process("arg:/style", "arg:/data", "arg:/result", \@parameter, [data=>$content,style=>$style],$result);

    print "Nachher:\n$result\n\n\n";
   
    my $sit = new XML::Sablotron::Situation();
    my $doc = XML::Sablotron::DOM::parseBuffer($sit, $result);

    my $myresult= $doc->xql("//index");

    foreach my $key (@$myresult)
    {
      print $key->getNodeName()." -> ".$key->getFirstChild()->getNodeValue()."\n";
    }


#    my %erg=analyze ($sit,$doc->getFirstChild($sit));
#    print "Gefunden: ";
#      foreach my $d (keys %erg)
#      {
#        print "$d ";
#        symlink "../../ie/$d","db/$user/ind_folder/$folder/$d";
#      }
#      print "\n";

#my $root=$doc->getFirstChild($sit);
#my $type=$root->getNodeName($root);
#my $node=$root->getFirstChild($sit);
#$root->xql($sit,"/ITEM_LIST/ITEM");
#getNodeListLength, getNodeListItem and disposeNodeList
#foreach ($root->xql($sit,"/*"))





  }


}


sub searchforindexdir
{
  if (-d $_[0])
  {
    my $name=$_[0]; $name=~s{/*$}{}; $name=~s{^.*/}{};
    if ($name =~ m/\./)
    {
      if(-r "$_[0]/.index.xml")
      {
        processindex($_[0]); 
      }
      else
      {
        print "No rule found for $_[0]!\n";
      }
    }
    else
    {
      foreach my $sub (<$_[0]/*>)
      {
        searchforindexdir($sub);
      }
    }
  }
}

searchforindexdir("db");

exit;


my $user="";
my $db="";
my $id="";

#mkdir "db/$user/ind_creator",0777;
#symlink "../ie/$id","db/$user/ind_creator/$in{viewerid}_$id";
#mkdir "db/$user/ind_type",0777;
#symlink "../ie/$id","db/$user/ind_type/$type\_$id";


#if (! -w "db/$user/ind_link")
#{
#  mkdir "db/$user/ind_link",0777;
#}



# symlink "../ie/$id1","db/$user/ind_link/$id2\_$id1";
#  symlink "../ie/$id2","db/$user/ind_link/$id1\_$id2";






opendir(DIR,$db) or die "Konnte Verzeichnis $db nicht ffnen: $!";
while (defined($f=readdir(DIR)))
{
  next if $f =~ /^\.\.?$/;

  print LOG "$f\n";
  my $b=$f; my $a="$db/$f";

  open IN,"<$a" or die "$!";
  undef $/;
  $content=<IN>;
  close IN;
  if ($content =~ m/<ie([^>]*)>(.*?)<\/ie>/s)
  {
    my ($arg,$akt)=($1,$2);

    symlink "../ie/$b", "db/$user/ind_type/$1\_$b" if ($arg =~ m/type=\"(\w+)\"/);
    symlink "../ie/$b", "db/$user/ind_creator/$1\_$b" if ($akt =~ m/<creator[^>]+id=\"(\d+)"/);
  }
}

closedir DIR;

foreach my $file (<db/$user/link/*>)
{
  $file =~ m{db/$user/link/(\d+)_(\d+)};
  symlink "../ie/$2", "db/$user/ind_link/$1\_$2";
  symlink "../ie/$1", "db/$user/ind_link/$2\_$1";
}


sub analyze
{
  my $type=$_[1]->getNodeName($_[0]);
  print "Analysiere: $type:\n";

  my %erg;

  if ($type eq "and")
  {
    my %erg1=analyze ($_[0],$_[1]->getFirstChild($_[0]));
    my %erg2=analyze ($_[0],$_[1]->getLastChild($_[0]));
    foreach (keys %erg1)
    {
      $erg{$_}=1 if exists $erg2{$_};
    }
  }
  elsif ($type eq "or")
  {
    my %erg1=analyze ($_[0],$_[1]->getFirstChild($_[0]));
    my %erg2=analyze ($_[0],$_[1]->getLastChild($_[0]));
    %erg=(%erg1,%erg2);
  }
  elsif ($type eq "xor")
  {
    my %erg1=analyze ($_[0],$_[1]->getFirstChild($_[0]));
    my %erg2=analyze ($_[0],$_[1]->getLastChild($_[0]));

  }
  elsif ($type eq "not")
  {
    my @erg1=analyze ($_[0],$_[1]->getFirstChild($_[0]));

  }
  {
    my $fieldname= $_[1]->getAttribute("fieldname",$_[0]);
    my $value= $_[1]->getAttribute("value",$_[0]);

    foreach my $file(<db/$user/ie/*>)
    {
      if (open IN,$file)
      {
        my $content=<IN>;
        close IN;
        $file =~ s{^db/$user/ie/}{};

        my $field;
        $field=$1 if ($fieldname eq "title" and $content=~ m/<title>(.*?)<\/title>/);
        $field=$1 if ($fieldname eq "creatorabbr" and $content=~ m/<creator abbr="([^"]*?)"/);
        $field=$1 if ($fieldname eq "modificationdate" and $content=~ m/<lastmodificationdate>(.*?)<\/lastmodificationdate>/);
        $field=$1 if ($fieldname eq "creationdate" and $content=~ m/<creationdate>(.*?)<\/creationdate>/);

#        if($comp eq "cn" and index($field,$value)>(-1))
        {
          $erg{$file}=1;
        }
#        if($comp eq "eq" and $field eq $value)
        {
          $erg{$file}=1;
        }
#        if($comp eq "lt" and $field lt $value)
        {
          $erg{$file}=1;
        }
#        if($comp eq "gt" and $field gt $value)
        {
          $erg{$file}=1;
        }
      }
    }
  }
#  elsif ($type eq "linkcondition")
  {
    #<linkcondition id="000100000018" type="topic"/>
    my $id= $_[1]->getAttribute("id",$_[0]);
    #print "Suche nach ID $id\n";
    foreach (<db/$user/ind_link/$id\_*>)
    {
      s{^db/$user/ind_link/$id\_}{};
      $erg{$_}=1;
    }
  }
#  elsif ($type eq "typecondition")
  {
    # <typecondition type="group"/>
    my $type= $_[1]->getAttribute("type",$_[0]);
    print "Suche nach Typ $type\n";
    foreach (<db/$user/ind_type/$type\_*>)
    {
      s{^db/$user/ind_type/$type\_}{};
      $erg{$_}=1;
    }

  }
#  else
  {
    print STDERR "Typ nicht erkannt: $type\n";
  }
  %erg;
}


foreach my $folder (<db/$user/ind_type/searchfolder_*>)
{
  $folder =~ s{^db/$user/ind_type/searchfolder_}{};
  mkdir "db/$user/ind_folder/$folder",0777;
  if (open IN,"db/$user/ie/$folder")
  {
    undef $/;
    my $folderfile=<IN>;
    close IN;
    if ($folderfile =~ m{<query>\s*(.*?)\s*</query>}s)
    {
      my $text=$1; $text=~ s/>\s+/>/gs;
      my $sit = new XML::Sablotron::Situation();
      my $doc = XML::Sablotron::DOM::parseBuffer($sit, $text);
      my %erg=analyze ($sit,$doc->getFirstChild($sit));
      print "Gefunden: ";
      foreach my $d (keys %erg)
      {
        print "$d ";
        symlink "../../ie/$d","db/$user/ind_folder/$folder/$d";
      }
      print "\n";
    }
  }
  print "\n";
}

#   symlink "../ie/$id1","ind_link/$id2\_$id1";
#            symlink "../ie/$id2","ind_link/$id1\_$id2";


