2 # Copyright © 2006-2013 Jamie Zawinski <jwz@jwz.org>
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
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.
16 # This is invoked by a final "Shell Script" build action on each of the
17 # .saver targets in the XCode project.
19 # Created: 8-Mar-2006.
22 #use diagnostics; # Fails on some MacOS 10.5 systems
25 use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
26 use IO::Compress::Gzip qw(gzip $GzipError);
28 my ($exec_dir, $progname) = ($0 =~ m@^(.*?)/([^/]+)$@);
30 my $version = q{ $Revision: 1.26 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
32 $ENV{PATH} = "/usr/local/bin:$ENV{PATH}"; # for seticon
34 my $thumbdir = $ENV{HOME} . '/www/xscreensaver/screenshots/';
40 sub convert_plist($$) {
41 my ($data, $to_binary_p) = @_;
42 my $is_binary_p = ($data =~ m/^bplist/s);
43 if ($data && (!$is_binary_p) != (!$to_binary_p)) {
44 print STDERR "$progname: converting plist\n" if ($verbose > 2);
45 my $which = ($to_binary_p ? 'binary1' : 'xml1');
46 my $cmd = "plutil -convert $which -s -o - -";
47 my $pid = open3 (my $in, my $out, undef, $cmd) || error ("pipe: $cmd: $!");
50 local $/ = undef; # read entire file
58 sub read_info_plist($) {
60 my $file = "$app_dir/Contents/Info.plist";
61 my $file2 = "$app_dir/Info.plist";
64 if (open ($in, '<', $file)) {
65 } elsif (open ($in, '<', $file2)) {
70 print STDERR "$progname: read $file\n" if ($verbose > 2);
71 local $/ = undef; # read entire file
75 $body = convert_plist ($body, 0); # convert to xml plist
76 return ($file, $body);
80 sub read_saver_xml($) {
82 error ("$app_dir: no name")
83 unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
86 return () if ($name eq 'XScreenSaver');
87 return () if ($name eq 'SaverTester');
89 my $file = "$app_dir/Contents/Resources/" . lc($name) . ".xml";
90 my $file2 = "$app_dir/" . lc($name) . ".xml";
91 my $file3 = "$app_dir/Contents/PlugIns/$name.saver/Contents/Resources/" .
95 if (open ($in, '<', $file)) {
96 } elsif (open ($in, '<', $file2)) { $file = $file2;
97 } elsif (open ($in, '<', $file3)) { $file = $file3;
101 print STDERR "$progname: read $file\n" if ($verbose > 2);
102 local $/ = undef; # read entire file
106 # Uncompress the XML if it is compressed.
108 gunzip (\$body, \$body2) || error ("$app_dir: xml gunzip: $GunzipError");
109 my $was_compressed_p = ($body ne $body2);
110 return ($file, $body2, $was_compressed_p);
114 sub update_saver_xml($$) {
115 my ($app_dir, $vers) = @_;
116 my ($filename, $body, $was_compressed_p) = read_saver_xml ($app_dir);
119 return () unless defined ($filename);
121 $body =~ m@<screensaver[^<>]*?[ \t]_label=\"([^\"]+)\"@m ||
122 error ("$filename: no name label");
125 $body =~ m@<_description>(.*?)</_description>@s ||
126 error ("$filename: no description tag");
128 $desc =~ s/^([ \t]*\n)+//s;
131 # in case it's done already...
132 $desc =~ s@<!--.*?-->@@gs;
133 $desc =~ s/^.* version \d[^\n]*\n//s;
134 $desc =~ s/^From the XScreenSaver.*\n//m;
135 $desc =~ s@^http://www\.jwz\.org/xscreensaver.*\n@@m;
137 s/\nCopyright [^ \r\n\t]+ (\d{4})(-\d{4})? (.*)\.$/\nWritten $3; $1./s;
140 error ("$filename: description contains bad characters")
141 if ($desc =~ m/([^\t\n -~]|[<>])/);
143 error ("$filename: can't extract authors")
144 unless ($desc =~ m@^(.*)\nWritten by[ \t]+(.+)$@s);
150 if ($authors =~ m@^(.*?)\s*[,;]\s+(\d\d\d\d)([-\s,;]+\d\d\d\d)*[.]?$@s) {
155 error ("$filename: can't extract year") unless $year;
156 my $cyear = 1900 + ((localtime())[5]);
157 $year = "$cyear" unless $year;
158 if ($year && ! ($year =~ m/$cyear/)) {
159 $year = "$year-$cyear";
162 $authors =~ s/[.,;\s]+$//s;
164 # List me as a co-author on all of them, since I'm the one who
165 # did the OSX port, packaged it up, and built the executables.
167 my $curator = "Jamie Zawinski";
168 if (! ($authors =~ m/$curator/si)) {
169 if ($authors =~ m@^(.*?),? and (.*)$@s) {
170 $authors = "$1, $2, and $curator";
172 $authors .= " and $curator";
176 my $desc1 = ("$name, version $vers.\n\n" . # savername.xml
179 "From the XScreenSaver collection: " .
180 "http://www.jwz.org/xscreensaver/\n" .
181 "Copyright \251 $year by $authors.\n");
183 my $desc2 = ("$name $vers,\n" . # Info.plist
184 "\302\251 $year $authors.\n" .
185 "From the XScreenSaver collection:\n" .
186 "http://www.jwz.org/xscreensaver/\n" .
191 # unwrap lines, but only when it's obviously ok: leave blank lines,
192 # and don't unwrap if that would compress leading whitespace on a line.
194 $desc2 =~ s/^(From |http:)/\n$1/gm;
195 1 while ($desc2 =~ s/([^\s])[ \t]*\n([^\s])/$1 $2/gs);
196 $desc2 =~ s/\n\n(From |http:)/\n$1/gs;
198 $body =~ s@(<_description>)(.*?)(</_description>)@$1$desc1$3@s;
200 if ($obody eq $body && $was_compressed_p) {
201 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
206 gzip (\$body, \$body2) || error ("$app_dir: xml gzip: $GzipError");
209 my $file_tmp = "$filename.tmp";
210 open (my $out, '>:raw', $file_tmp) || error ("$file_tmp: $!");
211 print $out $body || error ("$file_tmp: $!");
212 close $out || error ("$file_tmp: $!");
214 if (!rename ("$file_tmp", "$filename")) {
216 error ("mv \"$file_tmp\" \"$filename\": $!");
218 print STDERR "$progname: wrote $filename\n" if ($verbose);
221 return ($desc1, $desc2);
225 sub compress_all_xml_files($) {
227 opendir (my $dirp, $dir) || error ("$dir: $!");
228 my @files = readdir ($dirp);
230 foreach my $f (sort @files) {
231 next unless ($f =~ m/\.xml$/si);
232 my $filename = "$dir/$f";
233 open (my $in, '<', $filename) || error ("$filename: $!");
234 print STDERR "$progname: read $filename\n" if ($verbose > 2);
235 local $/ = undef; # read entire file
239 if ($body =~ m/^<\?xml/s) {
241 gzip (\$body, \$body2) || error ("$filename: xml gzip: $GzipError");
243 my $file_tmp = "$filename.tmp";
244 open (my $out, '>:raw', $file_tmp) || error ("$file_tmp: $!");
245 print $out $body || error ("$file_tmp: $!");
246 close $out || error ("$file_tmp: $!");
248 if (!rename ("$file_tmp", "$filename")) {
250 error ("mv \"$file_tmp\" \"$filename\": $!");
252 print STDERR "$progname: compressed $filename\n" if ($verbose);
253 } elsif ($verbose > 2) {
254 print STDERR "$filename: already compressed\n";
260 sub set_plist_key($$$$) {
261 my ($filename, $body, $key, $val) = @_;
265 \n\t<string>)([^<>]*)(</string>
267 # print STDERR "$progname: $filename: $key was: $2\n" if ($verbose);
268 $body = $1 . $val . $3;
270 error ("$filename: unparsable")
271 unless ($body =~ m@^(.*)(\n</dict>\n</plist>\n)$@s);
273 "\n\t<key>$key</key>" .
274 "\n\t<string>$val</string>" .
284 $app_dir =~ s@/+$@@s;
286 # "seticon" is from osxutils, http://osxutils.sourceforge.net/
288 my $icon = ($app_dir =~ m/\.saver$/ ? 'XScreenSaver' : 'SaverRunner');
289 $icon = "$app_dir/../../../$icon.icns";
290 my @cmd = ("seticon", "-d", $icon, $app_dir);
291 print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
300 return unless ($app_dir =~ m@\.saver/?$@s);
302 my @cmd = ("$exec_dir/update-thumbnail.pl", $thumbdir, $app_dir);
303 push @cmd, "-" . ("v" x $verbose) if ($verbose);
304 print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
308 exit ($exit) if $exit;
315 error ("$app_dir: no name")
316 unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
319 my ($filename, $plist) = read_info_plist ($app_dir);
322 error ("$filename: no version number")
323 unless ($plist =~ m@<key>CFBundleShortVersionString</key>\s*
324 <string>([^<>]+)</string>@sx);
326 my ($ignore, $info_str) = update_saver_xml ($app_dir, $vers);
328 # No, don't do this -- the iOS version reads the XML file in a few
329 # different places, and most of those places don't understand gzip.
331 if ($app_name eq 'XScreenSaver') {
332 compress_all_xml_files ($app_dir);
333 } elsif (! defined($info_str)) {
334 print STDERR "$progname: $filename: no XML file\n" if ($verbose > 1);
337 $info_str =~ m@^([^\n]+)\n@s ||
338 error ("$filename: unparsable copyright");
339 my $copyright = "$1";
340 $copyright =~ s/\b\d{4}-(\d{4})\b/$1/;
342 # Lose the Wikipedia URLs.
343 $info_str =~ s@http:.*?\b(wikipedia|mathworld)\b[^\s]+[ \t]*\n?@@gm;
345 $info_str =~ s/(\n\n)\n+/$1/gs;
346 $info_str =~ s/(^\s+|\s+$)//gs;
347 $plist = set_plist_key ($filename, $plist,
348 "NSHumanReadableCopyright", $copyright);
349 $plist = set_plist_key ($filename, $plist,
350 "CFBundleLongVersionString",$copyright);
351 $plist = set_plist_key ($filename, $plist,
352 "CFBundleGetInfoString", $info_str);
354 if ($oplist eq $plist) {
355 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
357 $plist = convert_plist ($plist, 1); # convert to binary plist
358 my $file_tmp = "$filename.tmp";
359 open (my $out, '>:raw', $file_tmp) || error ("$file_tmp: $!");
360 print $out $plist || error ("$file_tmp: $!");
361 close $out || error ("$file_tmp: $!");
363 if (!rename ("$file_tmp", "$filename")) {
365 error ("mv \"$file_tmp\" \"$filename\": $!");
367 print STDERR "$progname: wrote $filename\n" if ($verbose);
372 set_thumb ($app_dir);
378 print STDERR "$progname: $err\n";
383 print STDERR "usage: $progname [--verbose] program.app ...\n";
390 while ($_ = $ARGV[0]) {
392 if (m/^--?verbose$/s) { $verbose++; }
393 elsif (m/^-v+$/) { $verbose += length($_)-1; }
394 elsif (m/^--?q(uiet)?$/s) { $verbose = 0; }
395 elsif (m/^-/s) { usage(); }
396 else { push @files, $_; }
398 usage() unless ($#files >= 0);