#!/usr/bin/perl
#Convert Gnome file associations into Firefox file associations

use strict;

#Globals
my $appdir="/usr/share/applications/";
my $mimeinfo=$appdir."mimeinfo.cache";
my $globs="/usr/share/mime/globs";

#Make RDF header
sub header() {
  print "<?xml version=\"1.0\"?>\n";
  print "<RDF:RDF xmlns:NC=\"http://home.netscape.com/NC-rdf#\"\n";
  print "         xmlns:RDF=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n";
}

#Add RDF entry for MIME type
sub entry() {
  my ($mime,$ext,$prog,$name)=@_;

  print "  <RDF:Description RDF:about=\"urn:mimetype:handler:".$mime."\"\n";
  print "                   NC:alwaysAsk=\"true\"\n";
  print "                   NC:saveToDisk=\"false\"\n";
  print "                   NC:useSystemDefault=\"false\"\n";
  print "                   NC:handleInternal=\"false\">\n";
  print "    <NC:externalApplication RDF:resource=\"urn:mimetype:externalApplication:".$mime."\"/>\n";
  print "  </RDF:Description>\n";
  print "  <RDF:Description RDF:about=\"urn:mimetype:externalApplication:".$mime."\"\n";
  print "                   NC:path=\"".$prog."\"\n";

  if ($name ne "") {
    print "                   NC:prettyName=\"".$name."\" />\n";
  }

  print "  <RDF:Seq RDF:about=\"urn:mimetypes:root\">\n";
  print "    <RDF:li RDF:resource=\"urn:mimetype:".$mime."\"/>\n";
  print "  </RDF:Seq>\n";
  print "  <RDF:Description RDF:about=\"urn:mimetype:".$mime."\"\n";
  print "                   NC:value=\"".$mime."\"\n";
  print "                   NC:editable=\"true\"\n";

  if ($ext ne "") {
    print "                   NC:fileExtensions=\"".$ext."\"\n";
  }

  print "                   NC:description=\"\">\n";
  print "    <NC:handlerProp RDF:resource=\"urn:mimetype:handler:".$mime."\"/>\n";
  print "  </RDF:Description>\n";
}

#Make RDF footer
sub footer() {
  print "  <RDF:Description RDF:about=\"urn:mimetypes\">\n";
  print "    <NC:MIME-types RDF:resource=\"urn:mimetypes:root\"/>\n";
  print "  </RDF:Description>\n";
  print "</RDF:RDF>\n";
}

#Read desktop file
sub getdesktop() {
  my ($deskfile)=@_;
  my $prog="";
  my $name="";

  open(IN2,"<",$appdir.$deskfile);

  while(my $line=<IN2>)
  {
    chomp $line;
    if ($line =~ m/Exec=(.*)/)
    {
      $prog=$1;
    }
    if ($line =~ m/Name=(.*)/)
    {
      $name=$1;
  } }

  close(IN2);
  $prog =~ s/ .*//;
  return ($prog,$name);
}

#Read mimeinfo.cache to find out which desktop file for each MIME type
sub readmimeprog() {
  my %mp;

  open(IN,"<",$mimeinfo);

  while(my $line=<IN>)
  {
    chomp $line;
    if ($line =~ m/([^=]*)=([^;]*)/)
    {
      my $mime=$1;
      my $desk=$2;
      my @prog=&getdesktop($desk);
      $mp{$mime}=\@prog;
  } }

  close(IN);
  return %mp;
}

#Read globs to find out which extensions for each MIME type
sub readmimeext() {
  my %me;

  open(IN,"<",$globs);

  while(my $line=<IN>)
  {
    chomp $line;

    if ($line =~ m/([^:]*):\*\.(.*)/)
    {
      my $mime=$1;
      my $ext=$2;
      $me{$mime}=$ext;
  } }

  close(IN);
  return %me;
}

#Read MIME info from files
sub getentries() {
  my %mimeprog=&readmimeprog();
  my %mimeext=&readmimeext();

  foreach my $k (sort keys %mimeprog) {
     my $mime=$k;
     my ($prog,$name)=@{$mimeprog{$mime}};
     my $ext=$mimeext{$mime};
     if (!(defined $ext)) {$ext="";}
     &entry($mime,$ext,$prog,$name);
} }

sub main() {
  &header();
  &getentries();
  &footer();
}

&main();
