JAL-3247 Script to produce auto file association files for mac and windows installer...
[jalview.git] / utils / install4j / auto_file_associations.pl
diff --git a/utils/install4j/auto_file_associations.pl b/utils/install4j/auto_file_associations.pl
new file mode 100755 (executable)
index 0000000..b46e274
--- /dev/null
@@ -0,0 +1,81 @@
+#!/usr/bin/env perl
+
+use strict;
+
+my $fileformats = $ARGV[0];
+$fileformats = "./src/jalview/io/FileFormats.java" unless $fileformats;
+
+my $mactemplatefile = "file_associations_template-Info_plist.xml";
+my $i4jtemplatefile = "file_associations_template-install4j.xml";
+my $mactemplate;
+my $i4jtemplate;
+open(MT,"<$mactemplatefile") or dir("Could not open '$mactemplatefile' for reading");
+while(<MT>){
+  $mactemplate .= $_;
+}
+close(MT);
+open(IT,"<$i4jtemplatefile") or dir("Could not open '$i4jtemplatefile' for reading");
+while(<IT>){
+  $i4jtemplate .= $_;
+}
+close(IT);
+my $macauto;
+my $i4jauto;
+
+my $macautofile = $mactemplatefile;
+$macautofile =~ s/template/auto$1/;
+my $i4jautofile = $i4jtemplatefile;
+$i4jautofile =~ s/template/auto$1/;
+
+open(MA,">$macautofile") or die ("Could not open '$macautofile' for writing");
+print MA "<key>CFBundleDocumentTypes</key>\n<array>\n\n";
+open(IA,">$i4jautofile") or die ("Could not open '$i4jautofile' for writing");
+
+open(IN, "<$fileformats") or die ("Could not open '$fileformats' for reading");
+my $id = 10000;
+while(my $line = <IN>) {
+  $line =~ s/\s+/ /g;
+  $line =~ s/(^ | $)//g;
+  if ($line =~ m/^(\w+) ?\( ?"([^"]*)" ?, ?"([^"]*)" ?, ?(true|false) ?, ?(true|false) ?\)$/i) {
+    my $shortname = lc($1);
+    my $name = $2;
+    my $extensions = $3;
+    $extensions =~ s/\s+//g;
+    my @extensions = split(m/,/,$extensions);
+    #warn("LINE: $line\nFound extensions '".join("', '", @extensions)."' for $name Files ($shortname)'n");
+
+    my $macentry = $mactemplate;
+    my $i4jentry = $i4jtemplate;
+
+    $macentry =~ s/\$\$NAME\$\$/$name/g;
+    $macentry =~ s/\$\$SHORTNAME\$\$/$shortname/g;
+
+    $i4jentry =~ s/\$\$NAME\$\$/$name/g;
+    $i4jentry =~ s/\$\$SHORTNAME\$\$/$shortname/g;
+
+    while ($macentry =~ m/\$\$([^\$]*)EXTENSIONS([^\$]*)\$\$/) {
+      my $pre = $1;
+      my $post = $2;
+      my $macextensions;
+      for my $ext (@extensions) {
+        $macextensions .= $pre.$ext.$post;
+      }
+      $macentry =~ s/\$\$${pre}EXTENSIONS${post}\$\$/$macextensions/g;
+    }
+    print MA $macentry;
+
+    for my $ext (@extensions) {
+      my $i4jextentry = $i4jentry;
+      $i4jextentry =~ s/\$\$EXTENSION\$\$/$ext/g;
+      $i4jextentry =~ s/\$\$ID\$\$/$id/g;
+      $id++;
+
+      print IA $i4jextentry;
+    }
+
+  }
+}
+close(IN);
+close(IA);
+print MA "</array>\n";
+close(MA);