2 # Copyright © 2006-2011 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 my $progname = $0; $progname =~ s@.*/@@g;
26 my $version = q{ $Revision: 1.15 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
28 $ENV{PATH} = "/usr/local/bin:$ENV{PATH}"; # for seticon
33 sub read_info_plist($) {
35 my $file = "$app_dir/Contents/Info.plist";
39 error ("$file: $!") unless open (IN, "<$file");
40 while (<IN>) { $body .= $_; }
42 return ($file, $body);
46 sub read_saver_xml($) {
48 error ("$app_dir: no name")
49 unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
51 my $file = "$app_dir/Contents/Resources/$name.xml";
55 error ("$file: $!") unless open (IN, "<$file");
56 while (<IN>) { $body .= $_; }
58 return ($file, $body);
62 sub update_saver_xml($$) {
63 my ($app_dir, $vers) = @_;
64 my ($filename, $body) = read_saver_xml ($app_dir);
67 $body =~ m@<screensaver[^<>]*?[ \t]_label=\"([^\"]+)\"@m ||
68 error ("$filename: no name label");
71 $body =~ m@<_description>(.*?)</_description>@s ||
72 error ("$filename: no description tag");
74 $desc =~ s/^([ \t]*\n)+//s;
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;
83 s/\nCopyright [^ \r\n\t]+ (\d{4})(-\d{4})? (.*)\.$/\nWritten $3; $1./s;
86 error ("$filename: description contains bad characters")
87 if ($desc =~ m/([^\t\n -~]|[<>])/);
89 error ("$filename: can't extract authors")
90 unless ($desc =~ m@^(.*)\nWritten by[ \t]+(.+)$@s);
96 if ($authors =~ m@^(.*?)\s*[,;]\s+(\d\d\d\d)([-\s,;]+\d\d\d\d)*[.]?$@s) {
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";
108 $authors =~ s/[.,;\s]+$//s;
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.
113 my $curator = "Jamie Zawinski";
114 if (! ($authors =~ m/$curator/si)) {
115 if ($authors =~ m@^(.*?),? and (.*)$@s) {
116 $authors = "$1, $2, and $curator";
118 $authors .= " and $curator";
122 my $desc1 = ("$name, version $vers.\n\n" . # savername.xml
125 "From the XScreenSaver collection: " .
126 "http://www.jwz.org/xscreensaver/\n" .
127 "Copyright \251 $year by $authors.\n");
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" .
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.
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;
144 $body =~ s@(<_description>)(.*?)(</_description>)@$1$desc1$3@s;
146 if ($obody eq $body) {
147 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
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: $!");
154 if (!rename ("$file_tmp", "$filename")) {
156 error ("mv \"$file_tmp\" \"$filename\": $!");
158 print STDERR "$progname: wrote $filename\n" if ($verbose);
161 return ($desc1, $desc2);
166 sub set_plist_key($$$$) {
167 my ($filename, $body, $key, $val) = @_;
171 \n\t<string>)([^<>]*)(</string>
173 # print STDERR "$progname: $filename: $key was: $2\n" if ($verbose);
174 $body = $1 . $val . $3;
176 error ("$filename: unparsable")
177 unless ($body =~ m@^(.*)(\n</dict>\n</plist>\n)$@s);
179 "\n\t<key>$key</key>" .
180 "\n\t<string>$val</string>" .
190 $app_dir =~ s@/+$@@s;
192 # "seticon" is from osxutils, http://osxutils.sourceforge.net/
194 my $icon = "$app_dir/../../../XScreenSaver.icns";
195 my @cmd = ("seticon", "-d", $icon, $app_dir);
196 print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
205 error ("$app_dir: no name")
206 unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
209 my ($filename, $plist) = read_info_plist ($app_dir);
212 error ("$filename: no version number")
213 unless ($plist =~ m@<key>CFBundleShortVersionString</key>\s*
214 <string>([^<>]+)</string>@sx);
216 my ($ignore, $info_str) = update_saver_xml ($app_dir, $vers);
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/;
223 # Lose the Wikipedia URLs.
224 $info_str =~ s@http:.*?\b(wikipedia|mathworld)\b[^\s]+[ \t]*\n?@@gm;
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);
235 if ($oplist eq $plist) {
236 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
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: $!");
243 if (!rename ("$file_tmp", "$filename")) {
245 error ("mv \"$file_tmp\" \"$filename\": $!");
247 print STDERR "$progname: wrote $filename\n" if ($verbose);
256 print STDERR "$progname: $err\n";
261 print STDERR "usage: $progname [--verbose] program.app ...\n";
267 while ($_ = $ARGV[0]) {
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, $_; }
274 usage() unless ($#files >= 0);