http://www.jwz.org/xscreensaver/xscreensaver-5.13.tar.gz
[xscreensaver] / OSX / update-info-plist.pl
1 #!/usr/bin/perl -w
2 # Copyright © 2006-2011 Jamie Zawinski <jwz@jwz.org>
3 #
4 # Permission to use, copy, modify, distribute, and sell this software and its
5 # documentation for any purpose is hereby granted without fee, provided that
6 # the above copyright notice appear in all copies and that both that
7 # copyright notice and this permission notice appear in supporting
8 # documentation.  No representations are made about the suitability of this
9 # software for any purpose.  It is provided "as is" without express or 
10 # implied warranty.
11 #
12 # Updates the NAME.xml file of a .saver bundle to include the current year,
13 # version number, etc.  Also updates the Info.plist file to include the
14 # short documentation, authors, etc. in the Finder "Get Info" properties.
15 #
16 # This is invoked by a final "Shell Script" build action on each of the
17 # .saver targets in the XCode project.
18 #
19 # Created:  8-Mar-2006.
20
21 require 5;
22 #use diagnostics;       # Fails on some MacOS 10.5 systems
23 use strict;
24
25 my $progname = $0; $progname =~ s@.*/@@g;
26 my $version = q{ $Revision: 1.15 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
27
28 $ENV{PATH} = "/usr/local/bin:$ENV{PATH}";   # for seticon
29
30
31 my $verbose = 1;
32
33 sub read_info_plist($) {
34   my ($app_dir) = @_;
35   my $file = "$app_dir/Contents/Info.plist";
36   $file =~ s@/+@/@g;
37   local *IN;
38   my $body = '';
39   error ("$file: $!") unless open (IN, "<$file");
40   while (<IN>) { $body .= $_; }
41   close IN;
42   return ($file, $body);
43 }
44
45
46 sub read_saver_xml($) {
47   my ($app_dir) = @_;
48   error ("$app_dir: no name") 
49     unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
50   my $name = lc($1);
51   my $file = "$app_dir/Contents/Resources/$name.xml";
52   $file =~ s@/+@/@g;
53   local *IN;
54   my $body = '';
55   error ("$file: $!") unless open (IN, "<$file");
56   while (<IN>) { $body .= $_; }
57   close IN;
58   return ($file, $body);
59 }
60
61
62 sub update_saver_xml($$) {
63   my ($app_dir, $vers) = @_;
64   my ($filename, $body) = read_saver_xml ($app_dir);
65   my $obody = $body;
66
67   $body =~ m@<screensaver[^<>]*?[ \t]_label=\"([^\"]+)\"@m ||
68     error ("$filename: no name label");
69   my $name = $1;
70
71   $body =~ m@<_description>(.*?)</_description>@s ||
72     error ("$filename: no description tag");
73   my $desc = $1;
74   $desc =~ s/^([ \t]*\n)+//s;
75   $desc =~ s/\s*$//s;
76
77   # in case it's done already...
78   $desc =~ s@<!--.*?-->@@gs;
79   $desc =~ s/^.* version \d[^\n]*\n//s;
80   $desc =~ s/^From the XScreenSaver.*\n//m;
81   $desc =~ s@^http://www\.jwz\.org/xscreensaver.*\n@@m;
82   $desc =~
83        s/\nCopyright [^ \r\n\t]+ (\d{4})(-\d{4})? (.*)\.$/\nWritten $3; $1./s;
84   $desc =~ s/^\n+//s;
85
86   error ("$filename: description contains bad characters")
87     if ($desc =~ m/([^\t\n -~]|[<>])/);
88
89   error ("$filename: can't extract authors")
90     unless ($desc =~ m@^(.*)\nWritten by[ \t]+(.+)$@s);
91   $desc = $1;
92   my $authors = $2;
93   $desc =~ s/\s*$//s;
94
95   my $year = undef;
96   if ($authors =~ m@^(.*?)\s*[,;]\s+(\d\d\d\d)([-\s,;]+\d\d\d\d)*[.]?$@s) {
97     $authors = $1;
98     $year = $2;
99   }
100
101   error ("$filename: can't extract year") unless $year;
102   my $cyear = 1900 + ((localtime())[5]);
103   $year = "$cyear" unless $year;
104   if ($year && ! ($year =~ m/$cyear/)) {
105     $year = "$year-$cyear";
106   }
107
108   $authors =~ s/[.,;\s]+$//s;
109
110   # List me as a co-author on all of them, since I'm the one who
111   # did the OSX port, packaged it up, and built the executables.
112   #
113   my $curator = "Jamie Zawinski";
114   if (! ($authors =~ m/$curator/si)) {
115     if ($authors =~ m@^(.*?),? and (.*)$@s) {
116       $authors = "$1, $2, and $curator";
117     } else {
118       $authors .= " and $curator";
119     }
120   }
121
122   my $desc1 = ("$name, version $vers.\n\n" .            # savername.xml
123                $desc . "\n" .
124                "\n" . 
125                "From the XScreenSaver collection: " .
126                "http://www.jwz.org/xscreensaver/\n" .
127                "Copyright \251 $year by $authors.\n");
128
129   my $desc2 = ("$name $vers,\n" .                       # Info.plist
130                "\302\251 $year $authors.\n" .
131                "From the XScreenSaver collection:\n" .
132                "http://www.jwz.org/xscreensaver/\n" .
133                "\n" .
134                $desc .
135                "\n");
136
137   # unwrap lines, but only when it's obviously ok: leave blank lines,
138   # and don't unwrap if that would compress leading whitespace on a line.
139   #
140   $desc2 =~ s/^(From |http:)/\n$1/gm;
141   1 while ($desc2 =~ s/([^\s])[ \t]*\n([^\s])/$1 $2/gs);
142   $desc2 =~ s/\n\n(From |http:)/\n$1/gs;
143
144   $body =~ s@(<_description>)(.*?)(</_description>)@$1$desc1$3@s;
145
146   if ($obody eq $body) {
147     print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
148   } else {
149     my $file_tmp = "$filename.tmp";
150     open(OUT, ">$file_tmp") || error ("$file_tmp: $!");
151     print OUT $body || error ("$file_tmp: $!");
152     close OUT || error ("$file_tmp: $!");
153
154     if (!rename ("$file_tmp", "$filename")) {
155       unlink "$file_tmp";
156       error ("mv \"$file_tmp\" \"$filename\": $!");
157     }
158     print STDERR "$progname: wrote $filename\n" if ($verbose);
159   }
160
161   return ($desc1, $desc2);
162 }
163
164
165
166 sub set_plist_key($$$$) {
167   my ($filename, $body, $key, $val) = @_;
168
169   if ($body =~ m@^(.*
170                   \n\t<key>$key</key>
171                   \n\t<string>)([^<>]*)(</string>
172                   .*)$@xs) {
173 #    print STDERR "$progname: $filename: $key was: $2\n" if ($verbose);
174     $body = $1 . $val . $3;
175   } else {
176     error ("$filename: unparsable")
177       unless ($body =~ m@^(.*)(\n</dict>\n</plist>\n)$@s);
178     $body = ($1 .
179              "\n\t<key>$key</key>" .
180              "\n\t<string>$val</string>" .
181              $2);
182   }
183
184   return $body;
185 }
186
187
188 sub set_icon($) {
189   my ($app_dir) = @_;
190   $app_dir =~ s@/+$@@s;
191
192   # "seticon" is from osxutils, http://osxutils.sourceforge.net/
193
194   my $icon = "$app_dir/../../../XScreenSaver.icns";
195   my @cmd = ("seticon", "-d", $icon, $app_dir);
196   print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
197     if ($verbose > 1);
198   system (@cmd);
199 }
200
201
202 sub update($) {
203   my ($app_dir) = @_;
204
205   error ("$app_dir: no name") 
206     unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
207   my $app_name = $1;
208
209   my ($filename, $plist) = read_info_plist ($app_dir);
210   my $oplist = $plist;
211
212   error ("$filename: no version number")
213     unless ($plist =~ m@<key>CFBundleShortVersionString</key>\s*
214                         <string>([^<>]+)</string>@sx);
215   my $vers = $1;
216   my ($ignore, $info_str) = update_saver_xml ($app_dir, $vers);
217
218   $info_str =~ m@^([^\n]+)\n@s ||
219     error ("$filename: unparsable copyright");
220   my $copyright = "$1";
221   $copyright =~ s/\b\d{4}-(\d{4})\b/$1/;
222
223   # Lose the Wikipedia URLs.
224   $info_str =~ s@http:.*?\b(wikipedia|mathworld)\b[^\s]+[ \t]*\n?@@gm;
225
226   $info_str =~ s/(\n\n)\n+/$1/gs;
227   $info_str =~ s/(^\s+|\s+$)//gs;
228   $plist = set_plist_key ($filename, $plist, 
229                           "NSHumanReadableCopyright", $copyright);
230   $plist = set_plist_key ($filename, $plist,
231                           "CFBundleLongVersionString",$copyright);
232   $plist = set_plist_key ($filename, $plist,
233                           "CFBundleGetInfoString",    $info_str);
234
235   if ($oplist eq $plist) {
236     print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
237   } else {
238     my $file_tmp = "$filename.tmp";
239     open(OUT, ">$file_tmp") || error ("$file_tmp: $!");
240     print OUT $plist || error ("$file_tmp: $!");
241     close OUT || error ("$file_tmp: $!");
242
243     if (!rename ("$file_tmp", "$filename")) {
244       unlink "$file_tmp";
245       error ("mv \"$file_tmp\" \"$filename\": $!");
246     }
247     print STDERR "$progname: wrote $filename\n" if ($verbose);
248   }
249
250   set_icon ($app_dir);
251 }
252
253
254 sub error($) {
255   my ($err) = @_;
256   print STDERR "$progname: $err\n";
257   exit 1;
258 }
259
260 sub usage() {
261   print STDERR "usage: $progname [--verbose] program.app ...\n";
262   exit 1;
263 }
264
265 sub main() {
266   my @files = ();
267   while ($_ = $ARGV[0]) {
268     shift @ARGV;
269     if    (m/^--?verbose$/s)  { $verbose++; }
270     elsif (m/^-v+$/)          { $verbose += length($_)-1; }
271     elsif (m/^--?q(uiet)?$/s) { $verbose = 0; }
272     else                      { push @files, $_; }
273   }
274   usage() unless ($#files >= 0);
275   foreach (@files) {
276     update ($_);
277   }
278 }
279
280 main();
281 exit 0;