2 # Copyright © 2006-2009 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.13 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
30 sub read_info_plist($) {
32 my $file = "$app_dir/Contents/Info.plist";
36 error ("$file: $!") unless open (IN, "<$file");
37 while (<IN>) { $body .= $_; }
39 return ($file, $body);
43 sub read_saver_xml($) {
45 error ("$app_dir: no name")
46 unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
48 my $file = "$app_dir/Contents/Resources/$name.xml";
52 error ("$file: $!") unless open (IN, "<$file");
53 while (<IN>) { $body .= $_; }
55 return ($file, $body);
59 sub update_saver_xml($$) {
60 my ($app_dir, $vers) = @_;
61 my ($filename, $body) = read_saver_xml ($app_dir);
64 $body =~ m@<screensaver[^<>]*?[ \t]_label=\"([^\"]+)\"@m ||
65 error ("$filename: no name label");
68 $body =~ m@<_description>(.*?)</_description>@s ||
69 error ("$filename: no description tag");
71 $desc =~ s/^([ \t]*\n)+//s;
74 # in case it's done already...
75 $desc =~ s@<!--.*?-->@@gs;
76 $desc =~ s/^.* version \d[^\n]*\n//s;
77 $desc =~ s/^From the XScreenSaver.*\n//m;
78 $desc =~ s@^http://www\.jwz\.org/xscreensaver.*\n@@m;
80 s/\nCopyright [^ \r\n\t]+ (\d{4})(-\d{4})? (.*)\.$/\nWritten $3; $1./s;
83 error ("$filename: description contains bad characters")
84 if ($desc =~ m/([^\t\n -~]|[<>])/);
86 error ("$filename: can't extract authors")
87 unless ($desc =~ m@^(.*)\nWritten by[ \t]+(.+)$@s);
93 if ($authors =~ m@^(.*?)\s*[,;]\s+(\d\d\d\d)([-\s,;]+\d\d\d\d)*[.]?$@s) {
98 error ("$filename: can't extract year") unless $year;
99 my $cyear = 1900 + ((localtime())[5]);
100 $year = "$cyear" unless $year;
101 if ($year && ! ($year =~ m/$cyear/)) {
102 $year = "$year-$cyear";
105 $authors =~ s/[.,;\s]+$//s;
107 # List me as a co-author on all of them, since I'm the one who
108 # did the OSX port, packaged it up, and built the executables.
110 my $curator = "Jamie Zawinski";
111 if (! ($authors =~ m/$curator/si)) {
112 if ($authors =~ m@^(.*?),? and (.*)$@s) {
113 $authors = "$1, $2, and $curator";
115 $authors .= " and $curator";
119 my $desc1 = ("$name, version $vers.\n\n" . # savername.xml
122 "From the XScreenSaver collection: " .
123 "http://www.jwz.org/xscreensaver/\n" .
124 "Copyright \251 $year by $authors.\n");
126 my $desc2 = ("$name $vers,\n" . # Info.plist
127 "\302\251 $year $authors.\n" .
128 "From the XScreenSaver collection:\n" .
129 "http://www.jwz.org/xscreensaver/\n" .
134 # unwrap lines, but only when it's obviously ok: leave blank lines,
135 # and don't unwrap if that would compress leading whitespace on a line.
137 $desc2 =~ s/^(From |http:)/\n$1/gm;
138 1 while ($desc2 =~ s/([^\s])[ \t]*\n([^\s])/$1 $2/gs);
139 $desc2 =~ s/\n\n(From |http:)/\n$1/gs;
141 $body =~ s@(<_description>)(.*?)(</_description>)@$1$desc1$3@s;
143 if ($obody eq $body) {
144 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
146 my $file_tmp = "$filename.tmp";
147 open(OUT, ">$file_tmp") || error ("$file_tmp: $!");
148 print OUT $body || error ("$file_tmp: $!");
149 close OUT || error ("$file_tmp: $!");
151 if (!rename ("$file_tmp", "$filename")) {
153 error ("mv \"$file_tmp\" \"$filename\": $!");
155 print STDERR "$progname: wrote $filename\n" if ($verbose);
158 return ($desc1, $desc2);
163 sub set_plist_key($$$$) {
164 my ($filename, $body, $key, $val) = @_;
168 \n\t<string>)([^<>]*)(</string>
170 # print STDERR "$progname: $filename: $key was: $2\n" if ($verbose);
171 $body = $1 . $val . $3;
173 error ("$filename: unparsable")
174 unless ($body =~ m@^(.*)(\n</dict>\n</plist>\n)$@s);
176 "\n\t<key>$key</key>" .
177 "\n\t<string>$val</string>" .
187 $app_dir =~ s@/+$@@s;
189 # "seticon" is from osxutils, http://osxutils.sourceforge.net/
191 my $icon = "$app_dir/../../../XScreenSaver.icns";
192 my @cmd = ("seticon", "-d", $icon, $app_dir);
193 print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
202 error ("$app_dir: no name")
203 unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
206 my ($filename, $plist) = read_info_plist ($app_dir);
209 error ("$filename: no version number")
210 unless ($plist =~ m@<key>CFBundleShortVersionString</key>\s*
211 <string>([^<>]+)</string>@sx);
213 my ($ignore, $info_str) = update_saver_xml ($app_dir, $vers);
215 $info_str =~ m@^([^\n]+)\n@s ||
216 error ("$filename: unparsable copyright");
217 my $copyright = "$1";
218 $copyright =~ s/\b\d{4}-(\d{4})\b/$1/;
220 # Lose the Wikipedia URLs.
221 $info_str =~ s@http:.*?\b(wikipedia|mathworld)\b[^\s]+[ \t]*\n?@@gm;
223 $info_str =~ s/(\n\n)\n+/$1/gs;
224 $info_str =~ s/(^\s+|\s+$)//gs;
225 $plist = set_plist_key ($filename, $plist,
226 "NSHumanReadableCopyright", $copyright);
227 $plist = set_plist_key ($filename, $plist,
228 "CFBundleLongVersionString",$copyright);
229 $plist = set_plist_key ($filename, $plist,
230 "CFBundleGetInfoString", $info_str);
232 if ($oplist eq $plist) {
233 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
235 my $file_tmp = "$filename.tmp";
236 open(OUT, ">$file_tmp") || error ("$file_tmp: $!");
237 print OUT $plist || error ("$file_tmp: $!");
238 close OUT || error ("$file_tmp: $!");
240 if (!rename ("$file_tmp", "$filename")) {
242 error ("mv \"$file_tmp\" \"$filename\": $!");
244 print STDERR "$progname: wrote $filename\n" if ($verbose);
253 print STDERR "$progname: $err\n";
258 print STDERR "usage: $progname [--verbose] program.app ...\n";
264 while ($#ARGV >= 0) {
266 if ($_ eq "--verbose") { $verbose++; }
267 elsif (m/^-v+$/) { $verbose += length($_)-1; }
268 elsif (m/^-./) { usage; }
269 else { push @files, $_; }
271 usage() unless ($#files >= 0);