JAL-4187 JAL-629 JAL-3830 adjustments to Cygwin and WSL parts of bash launcher script...
[jalview.git] / utils / install4j / auto_file_associations-i4j10.pl
1 #!/usr/bin/env perl
2
3 use strict;
4
5 my $i4jversion = 10;
6 if ($ARGV[0] eq "-v") {
7   shift @ARGV;
8   $i4jversion = shift @ARGV;
9   die("-v i4jversion must be an integer [probably 7 or 8]") unless $i4jversion =~ m/^\d+$/;
10 }
11
12 my $fileformats = $ARGV[0];
13 $fileformats = "../../src/jalview/io/FileFormat.java" unless $fileformats;
14
15 # default mimetype will be text/x-$shortname
16 # TODO: find an actual extension for mat, see JAL-Xxxxx for outstanding issues too
17 # TODO: look up standard mime type used for BLASTfmt matrices, etc
18 my $mimetypes = {
19   rnaml => "application/rnaml+xml",
20   biojson => "application/x-jalview-biojson+json",
21   jnet => "application/x-jalview-jnet+text",
22   features => "application/x-jalview-features+text",
23   scorematrix => "application/x-jalview-scorematrix+text",
24   pdb => "chemical/x-pdb",
25   mmcif => "chemical/x-cif",
26   mmcif2 => "chemical/x-mmcif",
27   jalview => "application/x-jalview+xml+zip",
28   jvl => "application/x-jalview-jvl+text",
29   annotations => "application/x-jalview-annotations+text",
30 };
31
32 my @dontaddshortname = qw(features json);
33 my @dontaddextension = qw(html xml json jar mfa fastq);
34 my $add_associations = {
35   biojson => {shortname=>"biojson",name=>"BioJSON",extensions=>["biojson"]},
36   gff2 => {shortname=>"gff2",name=>"Generic Features Format v2",extensions=>["gff2"]},
37   gff3 => {shortname=>"gff3",name=>"Generic Features Format v3",extensions=>["gff3"]},
38   features => {shortname=>"features",name=>"Jalview Features",extensions=>["features","jvfeatures"]},
39   annotations => {shortname=>"annotations",name=>"Jalview Annotations",extensions=>["annotations","jvannotations"]},
40   mmcif => {shortname=>"mmcif",name=>"CIF",extensions=>["cif"]},
41   mmcif2 => {shortname=>"mmcif2",name=>"mmCIF",extensions=>["mcif","mmcif"]},
42   jvl => {shortname=>"jvl",name=>"Jalview Launch",extensions=>["jvl"],iconfile=>"jvl_file"},
43   jnet => {shortname=>"jnet",name=>"JnetFile",extensions=>["concise","jnet"]},
44   scorematrix => {shortname=>"scorematrix",name=>"Substitution Matrix",extensions=>["mat"]},
45 };
46 my $add_extensions = {
47   blc => ["blc"],
48 };
49 my @put_first = qw(jalview jvl);
50 my @owner = @put_first;
51
52 my @non_primary = qw(mmcif mmcif2 pdb);
53
54 my $v = ($i4jversion >= 8)?$i4jversion:"";
55 my $i4jtemplatefile = "file_associations_template-install4j${v}.xml";
56 my $i4jtemplate;
57 my $mactemplatefile = "file_associations_template-Info_plist.xml";
58 my $mactemplate;
59
60 open(MT,"<$mactemplatefile") or die("Could not open '$mactemplatefile' for reading");
61 while(<MT>){
62   $mactemplate .= $_;
63 }
64 close(MT);
65 open(IT,"<$i4jtemplatefile") or die("Could not open '$i4jtemplatefile' for reading");
66 while(<IT>){
67   $i4jtemplate .= $_;
68 }
69 close(IT);
70 my $macauto;
71 my $i4jauto;
72
73 my $macautofile = $mactemplatefile;
74 $macautofile =~ s/template/auto$1/;
75
76 my $i4jautofile = $i4jtemplatefile;
77 $i4jautofile =~ s/template/auto$1/;
78
79 for my $key (sort keys %$add_associations) {
80   my $a = $add_associations->{$key};
81   warn("Known file association for $a->{shortname} (".join(",",@{$a->{extensions}}).")\n");
82 }
83
84 open(MA,">$macautofile") or die ("Could not open '$macautofile' for writing");
85 print MA "<key>CFBundleDocumentTypes</key>\n<array>\n\n";
86
87 open(IA,">$i4jautofile") or die ("Could not open '$i4jautofile' for writing");
88
89 open(IN, "<$fileformats") or die ("Could not open '$fileformats' for reading");
90 my $id = 10000;
91 my $file_associations = {};
92 while(my $line = <IN>) {
93   $line =~ s/\s+/ /g;
94   $line =~ s/(^ | $)//g;
95   if ($line =~ m/^(\w+) ?\( ?"([^"]*)" ?, ?"([^"]*)" ?, ?(true|false) ?, ?(true|false) ?\)$/i) {
96     my $shortname = lc($1);
97     next if (grep($_ eq $shortname, @dontaddshortname));
98     my $name = $2;
99     my $extensions = $3;
100     $extensions =~ s/\s+//g;
101     my @possextensions = map(lc($_),split(m/,/,$extensions));
102     my @extensions;
103     my $addext = $add_extensions->{$shortname};
104     if (ref($addext) eq "ARRAY") {
105       push(@possextensions, @$addext);
106     }
107     for my $possext (@possextensions) {
108       next if grep($_ eq $possext, @extensions);
109       next if grep($_ eq $possext, @dontaddextension);
110       push(@extensions,$possext);
111     }
112     next unless scalar(@extensions);
113     $file_associations->{$shortname} = {
114       shortname => $shortname,
115       name => $name,
116       extensions => \@extensions
117     };
118     warn("Reading file association for $shortname (".join(",",@extensions).")\n");
119   }
120 }
121 close(IN);
122
123 my %all_associations = (%$file_associations, %$add_associations);
124
125 my @ordered = (@put_first, @non_primary);
126 for my $key (sort keys %all_associations) {
127   next if grep($_ eq $key, @ordered);
128   push(@ordered, $key);
129 }
130 my $num = $#ordered + 1;
131
132 warn("--\n");
133
134 my $i4jcount = 0;
135 for my $shortname (@ordered) {
136   my $a = $all_associations{$shortname};
137   next if (ref($a) ne "HASH");
138
139   my $name = $a->{name};
140   my $extensions = $a->{extensions};
141   my $mimetype = $mimetypes->{$shortname};
142   $mimetype = "application/x-$shortname+txt" unless $mimetype;
143
144   my $iconfile = $a->{iconfile};
145   $iconfile = "Jalview-File" unless $iconfile;
146
147   my $owner = grep($_ eq $shortname, @owner);
148   my $primary = (! grep($_ eq $shortname, @non_primary));
149   my $primarystring = $primary?"true":"false";
150   #my $role = $owner?"Owner":($primary?"Editor":"Viewer");
151   my $role = $primary?"Editor":"Viewer";
152   my $rank = $owner?"Owner":($primary?"Default":"Alternate");
153
154   my @extensions = @$extensions;
155
156   my $xname = xml_escape($name);
157   my $xmimetype = xml_escape($mimetype);
158   my $xshortname = xml_escape($shortname);
159   my $xiconfile = xml_escape($iconfile);
160   my $xrole = xml_escape($role);
161   my $xROLE = xml_escape(uc($role));
162   my $xrank = xml_escape($rank);
163   my $xprimarystring = xml_escape($primarystring);
164
165   my $macentry = $mactemplate;
166   $macentry =~ s/\$\$NAME\$\$/$xname/g;
167   $macentry =~ s/\$\$SHORTNAME\$\$/$xshortname/g;
168   $macentry =~ s/\$\$MIMETYPE\$\$/$xmimetype/g;
169   $macentry =~ s/\$\$ICONFILE\$\$/$xiconfile/g;
170   $macentry =~ s/\$\$ROLE\$\$/$xrole/g;
171   $macentry =~ s/\$\$RANK\$\$/$xrank/g;
172   $macentry =~ s/\$\$PRIMARY\$\$/$xprimarystring/g;
173   while ($macentry =~ m/\$\$([^\$]*)EXTENSIONS([^\$]*)\$\$/) {
174     my $pre = $1;
175     my $post = $2;
176     my $macextensions;
177     for my $ext (@extensions) {
178       my $xext = xml_escape($ext);
179       $macextensions .= $pre.$xext.$post;
180     }
181     $macentry =~ s/\$\$${pre}EXTENSIONS${post}\$\$/$macextensions/g;
182   }
183   print MA $macentry;
184
185   my $i4jentry = $i4jtemplate;
186   $i4jentry =~ s/\$\$NAME\$\$/$xname/g;
187   $i4jentry =~ s/\$\$SHORTNAME\$\$/$xshortname/g;
188   $i4jentry =~ s/\$\$MIMETYPE\$\$/$xmimetype/g;
189   $i4jentry =~ s/\$\$ICONFILE\$\$/$xiconfile/g;
190   $i4jentry =~ s/\$\$PRIMARY\$\$/$xprimarystring/g;
191   $i4jentry =~ s/\$\$MACASSOCIATIONROLE\$\$/$xROLE/g;
192
193   my $ext = join(",",sort(@extensions));
194   my $xdisplayext = xml_escape(join(", ", map(".$_",sort(@extensions))));
195   my $progresspercent = int(($i4jcount/$num)*100);
196   $progresspercent = 100 if $progresspercent > 100;
197   $i4jcount++;
198   my $xext = xml_escape($ext);
199   my $addunixextension = "true";
200
201   $i4jentry =~ s/\$\$ADDUNIXEXTENSION\$\$/$addunixextension/g;
202   $i4jentry =~ s/\$\$EXTENSION\$\$/$xext/g;
203   $i4jentry =~ s/\$\$DISPLAYEXTENSION\$\$/$xdisplayext/g;
204   $i4jentry =~ s/\$\$PROGRESSPERCENT\$\$/$progresspercent/g;
205   $i4jentry =~ s/\$\$ID\$\$/$id/g;
206   $id++;
207   $i4jentry =~ s/\$\$ID1\$\$/$id/g;
208   $id++;
209   $i4jentry =~ s/\$\$ID2\$\$/$id/g;
210   $id++;
211
212   print IA $i4jentry;
213
214   delete $all_associations{$shortname};
215   warn("Writing entry for $name (".join(",",@$extensions).": $mimetype)\n");
216 }
217
218 close(IA);
219 print MA "</array>\n";
220 close(MA);
221
222 sub xml_escape {
223   my $x = shift;
224   # stolen from Pod::Simple::XMLOutStream in base distro
225   $x =~ s/([^-\n\t !\#\$\%\(\)\*\+,\.\~\/\:\;=\?\@\[\\\]\^_\`\{\|\}a-zA-Z0-9])/'&#'.(ord($1)).';'/eg;
226   return $x;
227 }