JAL-3247 sign the AppleScript uninstaller
[jalview.git] / utils / install4j / auto_file_associations.pl
1 #!/usr/bin/env perl
2
3 use strict;
4
5 my $fileformats = $ARGV[0];
6 $fileformats = "../../src/jalview/io/FileFormat.java" unless $fileformats;
7
8 # default mimetype will be text/x-$shortname
9 # TODO: find an actual extension for mat, see JAL-Xxxxx for outstanding issues too
10 # TODO: look up standard mime type used for BLASTfmt matrices, etc
11 my $mimetypes = {
12   rnaml => "application/rnaml+xml",
13   biojson => "application/x-jalview-biojson+json",
14   jnet => "application/x-jalview-jnet+text",
15   features => "application/x-jalview-features+text",
16   scorematrix => "application/x-jalview-scorematrix+text",
17   pdb => "chemical/x-pdb",
18   mmcif => "chemical/x-cif",
19   mmcif2 => "chemical/x-mcif",
20   jalview => "application/x-jalview+xml+zip",
21   jvl => "application/x-jalview-jvl+text",
22   annotations => "application/x-jalview-annotations+text",
23 };
24
25 my @dontaddshortname = qw(features json);
26 my @dontaddextension = qw(html xml json jar mfa fastq);
27 my $add_associations = {
28   biojson => {shortname=>"biojson",name=>"BioJSON",extensions=>["biojson"]},
29   gff2 => {shortname=>"gff2",name=>"Generic Features Format v2",extensions=>["gff2"]},
30   gff3 => {shortname=>"gff3",name=>"Generic Features Format v3",extensions=>["gff3"]},
31   features => {shortname=>"features",name=>"Jalview Features",extensions=>["features","jvfeatures"]},
32   annotations => {shortname=>"annotations",name=>"Jalview Annotations",extensions=>["annotations","jvannotations"]},
33   mmcif2 => {shortname=>"mmcif2",name=>"mmCIF",extensions=>["mcif","mmcif"]},
34   jvl => {shortname=>"jvl",name=>"Jalview Version Locator",extensions=>["jvl"],iconfile=>"Jalview-Version-Locator"},
35   jnet => {shortname=>"jnet",name=>"JnetFile",extensions=>["concise","jnet"]},
36   scorematrix => {shortname=>"scorematrix",name=>"Substitution Matrix",extensions=>["mat"]},
37 };
38 my $add_extensions = {
39   blc => ["blc"],
40 };
41 my @put_first = qw(jalview jvl);
42
43 my $mactemplatefile = "file_associations_template-Info_plist.xml";
44 my $i4jtemplatefile = "file_associations_template-install4j.xml";
45 my $mactemplate;
46 my $i4jtemplate;
47 open(MT,"<$mactemplatefile") or dir("Could not open '$mactemplatefile' for reading");
48 while(<MT>){
49   $mactemplate .= $_;
50 }
51 close(MT);
52 open(IT,"<$i4jtemplatefile") or dir("Could not open '$i4jtemplatefile' for reading");
53 while(<IT>){
54   $i4jtemplate .= $_;
55 }
56 close(IT);
57 my $macauto;
58 my $i4jauto;
59
60 my $macautofile = $mactemplatefile;
61 $macautofile =~ s/template/auto$1/;
62 my $i4jautofile = $i4jtemplatefile;
63 $i4jautofile =~ s/template/auto$1/;
64
65 open(MA,">$macautofile") or die ("Could not open '$macautofile' for writing");
66 print MA "<key>CFBundleDocumentTypes</key>\n<array>\n\n";
67 open(IA,">$i4jautofile") or die ("Could not open '$i4jautofile' for writing");
68
69 open(IN, "<$fileformats") or die ("Could not open '$fileformats' for reading");
70 my $id = 10000;
71 my $file_associations = {};
72 while(my $line = <IN>) {
73   $line =~ s/\s+/ /g;
74   $line =~ s/(^ | $)//g;
75   if ($line =~ m/^(\w+) ?\( ?"([^"]*)" ?, ?"([^"]*)" ?, ?(true|false) ?, ?(true|false) ?\)$/i) {
76     my $shortname = lc($1);
77     next if (grep($_ eq $shortname, @dontaddshortname));
78     my $name = $2;
79     my $extensions = $3;
80     $extensions =~ s/\s+//g;
81     my @possextensions = split(m/,/,$extensions);
82     my @extensions;
83     my $addext = $add_extensions->{$shortname};
84     if (ref($addext) eq "ARRAY") {
85       push(@possextensions, @$addext);
86     }
87     for my $possext (@possextensions) {
88       next if grep($_ eq $possext, @extensions);
89       next if grep($_ eq $possext, @dontaddextension);
90       push(@extensions,$possext);
91     }
92     next unless scalar(@extensions);
93     $file_associations->{$shortname} = {
94       shortname => $shortname,
95       name => $name,
96       extensions => \@extensions
97     };
98     warn("Adding file association for $shortname\n");
99   }
100 }
101 close(IN);
102
103 my %all_associations = (%$file_associations, %$add_associations);
104
105 for my $shortname (@put_first, sort keys %all_associations) {
106   my $a = $all_associations{$shortname};
107   if (ref($a) ne "HASH") {
108     next;
109   }
110
111   my $name = $a->{name};
112   my $extensions = $a->{extensions};
113   my $mimetype = $mimetypes->{$shortname};
114   $mimetype = "text/x-$shortname" unless $mimetype;
115
116   my $iconfile = $a->{iconfile};
117   $iconfile = "Jalview-File" unless $iconfile;
118
119   my @extensions = @$extensions;
120   #warn("LINE: $line\nFound extensions '".join("', '", @extensions)."' for $name Files ($shortname)'n");
121
122   my $macentry = $mactemplate;
123   my $i4jentry = $i4jtemplate;
124
125   $macentry =~ s/\$\$NAME\$\$/$name/g;
126   $macentry =~ s/\$\$SHORTNAME\$\$/$shortname/g;
127   $macentry =~ s/\$\$MIMETYPE\$\$/$mimetype/g;
128   $macentry =~ s/\$\$ICONFILE\$\$/$iconfile/g;
129
130   $i4jentry =~ s/\$\$NAME\$\$/$name/g;
131   $i4jentry =~ s/\$\$SHORTNAME\$\$/$shortname/g;
132   $i4jentry =~ s/\$\$MIMETYPE\$\$/$mimetype/g;
133   $i4jentry =~ s/\$\$ICONFILE\$\$/$iconfile/g;
134
135   while ($macentry =~ m/\$\$([^\$]*)EXTENSIONS([^\$]*)\$\$/) {
136     my $pre = $1;
137     my $post = $2;
138     my $macextensions;
139     for my $ext (@extensions) {
140       $macextensions .= $pre.$ext.$post;
141     }
142     $macentry =~ s/\$\$${pre}EXTENSIONS${post}\$\$/$macextensions/g;
143   }
144   print MA $macentry;
145
146   for my $ext (@extensions) {
147     my $i4jextentry = $i4jentry;
148     $i4jextentry =~ s/\$\$EXTENSION\$\$/$ext/g;
149     $i4jextentry =~ s/\$\$ID\$\$/$id/g;
150     $id++;
151
152     print IA $i4jextentry;
153   }
154
155   delete $all_associations{$shortname};
156 }
157
158 close(IA);
159 print MA "</array>\n";
160 close(MA);