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.17 $ }; $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";
37 open (my $in, '<', $file) || error ("$file: $!");
38 local $/ = undef; # read entire file
41 return ($file, $body);
45 sub read_saver_xml($) {
47 error ("$app_dir: no name")
48 unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
50 my $file = "$app_dir/Contents/Resources/" . lc($name) . ".xml";
51 my $file2 = "$app_dir/Contents/PlugIns/$name.saver/Contents/Resources/" .
55 open ($in, '<', $file) ||
56 open ($in, '<', $file2) ||
58 local $/ = undef; # read entire file
61 return ($file, $body);
65 sub update_saver_xml($$) {
66 my ($app_dir, $vers) = @_;
67 my ($filename, $body) = read_saver_xml ($app_dir);
70 $body =~ m@<screensaver[^<>]*?[ \t]_label=\"([^\"]+)\"@m ||
71 error ("$filename: no name label");
74 $body =~ m@<_description>(.*?)</_description>@s ||
75 error ("$filename: no description tag");
77 $desc =~ s/^([ \t]*\n)+//s;
80 # in case it's done already...
81 $desc =~ s@<!--.*?-->@@gs;
82 $desc =~ s/^.* version \d[^\n]*\n//s;
83 $desc =~ s/^From the XScreenSaver.*\n//m;
84 $desc =~ s@^http://www\.jwz\.org/xscreensaver.*\n@@m;
86 s/\nCopyright [^ \r\n\t]+ (\d{4})(-\d{4})? (.*)\.$/\nWritten $3; $1./s;
89 error ("$filename: description contains bad characters")
90 if ($desc =~ m/([^\t\n -~]|[<>])/);
92 error ("$filename: can't extract authors")
93 unless ($desc =~ m@^(.*)\nWritten by[ \t]+(.+)$@s);
99 if ($authors =~ m@^(.*?)\s*[,;]\s+(\d\d\d\d)([-\s,;]+\d\d\d\d)*[.]?$@s) {
104 error ("$filename: can't extract year") unless $year;
105 my $cyear = 1900 + ((localtime())[5]);
106 $year = "$cyear" unless $year;
107 if ($year && ! ($year =~ m/$cyear/)) {
108 $year = "$year-$cyear";
111 $authors =~ s/[.,;\s]+$//s;
113 # List me as a co-author on all of them, since I'm the one who
114 # did the OSX port, packaged it up, and built the executables.
116 my $curator = "Jamie Zawinski";
117 if (! ($authors =~ m/$curator/si)) {
118 if ($authors =~ m@^(.*?),? and (.*)$@s) {
119 $authors = "$1, $2, and $curator";
121 $authors .= " and $curator";
125 my $desc1 = ("$name, version $vers.\n\n" . # savername.xml
128 "From the XScreenSaver collection: " .
129 "http://www.jwz.org/xscreensaver/\n" .
130 "Copyright \251 $year by $authors.\n");
132 my $desc2 = ("$name $vers,\n" . # Info.plist
133 "\302\251 $year $authors.\n" .
134 "From the XScreenSaver collection:\n" .
135 "http://www.jwz.org/xscreensaver/\n" .
140 # unwrap lines, but only when it's obviously ok: leave blank lines,
141 # and don't unwrap if that would compress leading whitespace on a line.
143 $desc2 =~ s/^(From |http:)/\n$1/gm;
144 1 while ($desc2 =~ s/([^\s])[ \t]*\n([^\s])/$1 $2/gs);
145 $desc2 =~ s/\n\n(From |http:)/\n$1/gs;
147 $body =~ s@(<_description>)(.*?)(</_description>)@$1$desc1$3@s;
149 if ($obody eq $body) {
150 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
152 my $file_tmp = "$filename.tmp";
153 open(OUT, ">$file_tmp") || error ("$file_tmp: $!");
154 print OUT $body || error ("$file_tmp: $!");
155 close OUT || error ("$file_tmp: $!");
157 if (!rename ("$file_tmp", "$filename")) {
159 error ("mv \"$file_tmp\" \"$filename\": $!");
161 print STDERR "$progname: wrote $filename\n" if ($verbose);
164 return ($desc1, $desc2);
169 sub set_plist_key($$$$) {
170 my ($filename, $body, $key, $val) = @_;
174 \n\t<string>)([^<>]*)(</string>
176 # print STDERR "$progname: $filename: $key was: $2\n" if ($verbose);
177 $body = $1 . $val . $3;
179 error ("$filename: unparsable")
180 unless ($body =~ m@^(.*)(\n</dict>\n</plist>\n)$@s);
182 "\n\t<key>$key</key>" .
183 "\n\t<string>$val</string>" .
193 $app_dir =~ s@/+$@@s;
195 # "seticon" is from osxutils, http://osxutils.sourceforge.net/
197 my $icon = ($app_dir =~ m/\.saver$/ ? 'XScreenSaver' : 'SaverRunner');
198 $icon = "$app_dir/../../../$icon.icns";
199 my @cmd = ("seticon", "-d", $icon, $app_dir);
200 print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
209 error ("$app_dir: no name")
210 unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
213 my ($filename, $plist) = read_info_plist ($app_dir);
216 error ("$filename: no version number")
217 unless ($plist =~ m@<key>CFBundleShortVersionString</key>\s*
218 <string>([^<>]+)</string>@sx);
220 my ($ignore, $info_str) = update_saver_xml ($app_dir, $vers);
222 $info_str =~ m@^([^\n]+)\n@s ||
223 error ("$filename: unparsable copyright");
224 my $copyright = "$1";
225 $copyright =~ s/\b\d{4}-(\d{4})\b/$1/;
227 # Lose the Wikipedia URLs.
228 $info_str =~ s@http:.*?\b(wikipedia|mathworld)\b[^\s]+[ \t]*\n?@@gm;
230 $info_str =~ s/(\n\n)\n+/$1/gs;
231 $info_str =~ s/(^\s+|\s+$)//gs;
232 $plist = set_plist_key ($filename, $plist,
233 "NSHumanReadableCopyright", $copyright);
234 $plist = set_plist_key ($filename, $plist,
235 "CFBundleLongVersionString",$copyright);
236 $plist = set_plist_key ($filename, $plist,
237 "CFBundleGetInfoString", $info_str);
239 if ($oplist eq $plist) {
240 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
242 my $file_tmp = "$filename.tmp";
243 open(OUT, ">$file_tmp") || error ("$file_tmp: $!");
244 print OUT $plist || error ("$file_tmp: $!");
245 close OUT || error ("$file_tmp: $!");
247 if (!rename ("$file_tmp", "$filename")) {
249 error ("mv \"$file_tmp\" \"$filename\": $!");
251 print STDERR "$progname: wrote $filename\n" if ($verbose);
260 print STDERR "$progname: $err\n";
265 print STDERR "usage: $progname [--verbose] program.app ...\n";
272 while ($_ = $ARGV[0]) {
274 if (m/^--?verbose$/s) { $verbose++; }
275 elsif (m/^-v+$/) { $verbose += length($_)-1; }
276 elsif (m/^--?q(uiet)?$/s) { $verbose = 0; }
277 else { push @files, $_; }
279 usage() unless ($#files >= 0);