#!/usr/bin/perl -w

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

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

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

undef $/;

sub analyze
{
  return "" if(! defined $_[1]);
  # print "$_[1]\n";

  my $typ=$_[1]->getNodeType($_[0]);
  # print "Typ: $typ\n";

  return $_[1]->getNodeValue($_[0]) if($typ eq TEXT_NODE);

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

  my $erg="";

  my $array = $_[1]->getChildNodes($_[0]);
  foreach my $node (@$array)
  {
    $erg.=analyze($_[0],$node,$_[2],$_[3]);
  }
  if ($type eq "lowercase")
  {
    $erg=lc $erg;
  }
  elsif ($type eq "soundex")
  {
    $erg=soundex($erg);
  }
  elsif ($type eq "source" || $type eq "destination" || $type eq "parameter")
  {
    # $erg=$erg;
  }
  elsif ($type eq "index")
  {
    my $source=$_[3]."/".analyze($_[0],$_[1]->getFirstChild($_[0]),"","");
    my $destination=analyze($_[0],$_[1]->getLastChild($_[0]),"","");

    print "$_[2] $source -> $destination\n";
    symlink $destination,$source if($_[2] eq "symlink");
    unlink $source if($_[2] eq "unlink");
  }
  elsif ($type eq "root")
  {
    # do nothing
  }
  elsif ($type eq "#text")
  {
    $erg=$_[1]->getNodeValue($_[0]);
  }
  else
  {
    print STDERR "Typ nicht erkannt: $type\n";
  }
  $erg;
}



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 "Processing file $file\n";

    #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";
  
    if ($result)
    { 
      my $sit = new XML::Sablotron::Situation();
      my $doc = XML::Sablotron::DOM::parseBuffer($sit, $result);
      analyze ($sit,$doc->getFirstChild($sit),"symlink",$_[0]);
    }
  }
}

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");

