package PO4Moz::InstallRDF;

use strict;
use PO4Moz::Util;

sub parse {
  my $filename = shift;
  my $contents = shift;

  # This is gonna be easy.
  
  my %entries = ("index" => []);
  my $curentry = {"file" => $filename, "comments" => ""};

  my %info = ("prettyName" => "",
              "locale" => "",
              "version " => "",
              "guid" => "");

  # Remove information we don't need
  $contents =~ s/<em:targetApplication>.*?<\/em:targetApplication>//gs;
  
  # Get the locale name
  if ($contents =~ /<em:file>.*?<Description about="urn:mozilla:extension:file:(.*?)\.jar">.*?<\/em:file>/s) {
    $info{"locale"} = $1;
  }
  
  # Get the pretty name, if possible
  if ($contents =~ /<em:name>(.*?)<\/em:name>/s) {
    $info{"prettyName"} = $1;
  }
  elsif ($contents =~ /em:name="(.*?)"/s) {
    $info{"prettyName"} = $1;
  }
  
  # Get the version
  if ($contents =~ /<em:version>(.*?)<\/em:version>/s) {
    $info{"version"} = $1;
  }
  elsif ($contents =~ /em:version="(.*?)"/s) {
    $info{"version"} = $1;
  }

  # Get the GUID  
  if ($contents =~ /<em:id>(.*?)<\/em:id>/s) {
    $info{"guid"} = $1;
  }
  elsif ($contents =~ /em:id="(.*?)"/s) {
    $info{"guid"} = $1;
  }
  
  foreach my $key ("prettyName", "locale", "version", "guid") {
    $curentry->{"key"} = "$key";
    $curentry->{"text"} = $info{$key};

    $curentry->{"comments"} = " Name of your language pack"
      if ($key eq "prettyName");
    $curentry->{"comments"} = " Name of your language pack's locale"
      if ($key eq "locale");
    $curentry->{"comments"} = " Version of your language pack"
      if ($key eq "version");
    $curentry->{"comments"} = " Enter here a GUID for your language pack"
      if ($key eq "guid");

    $entries{"$filename:$key"} = $curentry;
    push @{$entries{"index"}}, "$filename:$key";
    $curentry = {};
  }

  return %entries;
}

sub translate {
  my $file = shift;
  my $contents = shift;
  my %entries = @_;
  my $out = "";
  
  # easy too?

  $out = $contents;

  # Modify the locale
  if ($entries{"$file:locale"}) {
    my $t = $entries{"$file:locale"}->{"translation"};
    $t = $entries{"$file:locale"}->{"text"} if (!$t);
    $out =~ s/(<em:file>.*?<Description about="urn:mozilla:extension:file:).*?(\.jar">.*?<\/em:file>)/$1$t$2/s;
    $out =~ s/(<em:locale>locale\/)[a-z]{2,3}-[A-Z]{2,3}(\/.*?<\/em:locale>)/$1$t$2/gs;
  }
  
  # Modify the pretty name, if possible
  if ($entries{"$file:prettyName"}) {
    my $t = $entries{"$file:prettyName"}->{"translation"};
    $t = $entries{"$file:prettyName"}->{"text"} if (!$t);
    $out =~ s/(about="urn:mozilla:install-manifest">.*?<em:name>).*?(<\/em:name>)/$1$t$2/s;
    $out =~ s/(about="urn:mozilla:install-manifest"\b.*?\bem:name=").*?(")/$1$t$2/s;
  }
  
  # Modify the version
  if ($entries{"$file:version"}) {
    my $t = $entries{"$file:version"}->{"translation"};
    $t = $entries{"$file:version"}->{"text"} if (!$t);
    $out =~ s/(about="urn:mozilla:install-manifest">.*?<em:version>).*?(<\/em:version>)/$1$t$2/s;
    $out =~ s/(about="urn:mozilla:install-manifest"\b.*?\bem:version=").*?(")/$1$t$2/s;
  }
  
  # Modify the guid  
  if ($entries{"$file:guid"}) {
    my $t = $entries{"$file:guid"}->{"translation"};
    $t = $entries{"$file:guid"}->{"text"} if (!$t);
    $out =~ s/(about="urn:mozilla:install-manifest">.*?<em:id>).*?(<\/em:id>)/$1$t$2/s;
    $out =~ s/(about="urn:mozilla:install-manifest"\b.*?\bem:id=").*?(")/$1$t$2/s;
  }
  
  # a piece of cake

  return $out;
}  

1;
