2 # Copyright © 2006 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.
25 my $progname = $0; $progname =~ s@.*/@@g;
26 my $version = q{ $Revision: 1.6 $ }; $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/^.* version \d[^\n]*\n//s;
76 $desc =~ s/^From the XScreenSaver.*\n//m;
77 $desc =~ s@^http://www\.jwz\.org/xscreensaver.*\n@@m;
78 $desc =~ s/^Copyright [^ \r\n\t]+ (\d{4})(-\d{4})? (.*)\.$/Written $3; $1./m;
81 error ("$filename: description contains bad characters")
82 if ($desc =~ m/([^\t\n -~]|[<>])/);
84 error ("$filename: can't extract authors")
85 unless ($desc =~ m@^(.*)\nWritten by[ \t]+([^\n]+)$@s);
91 if ($authors =~ m@^(.*?)\s*[,;]\s+(\d\d\d\d)([-\s,;]+\d\d\d\d)*[.]?$@s) {
96 my $cyear = 1900 + ((localtime())[5]);
97 $year = "$cyear" unless $year;
98 if ($year && ! ($year =~ m/$cyear/)) {
99 $year = "$year-$cyear";
102 $authors =~ s/[.,;\s]+$//s;
104 # List me as a co-author on all of them, since I'm the one who
105 # did the OSX port, packaged it up, and built the executables.
107 my $curator = "Jamie Zawinski";
108 if (! ($authors =~ m/$curator/si)) {
109 if ($authors =~ m@^(.*),? and (.*)$@s) {
110 $authors = "$1, $2, and $curator";
112 $authors .= " and $curator";
116 my $cc = "\302\251"; # unicode "©"
118 my $desc1 = ("$name, version $vers.\n\n" .
121 "From the XScreenSaver collection:\n" .
122 "http://www.jwz.org/xscreensaver/\n" .
123 "Copyright \251 $year by $authors.\n");
125 my $desc2 = ("$name $vers,\n" .
126 "$cc $year $authors.\n" .
127 "From the XScreenSaver collection:\n" .
128 "http://www.jwz.org/xscreensaver/\n" .
133 # unwrap lines, but only when it's obviously ok: leave blank lines,
134 # and don't unwrap if that would compress leading whitespace on a line.
136 $desc2 =~ s/^(From |http:)/\n$1/gm;
137 1 while ($desc2 =~ s/([^\s])[ \t]*\n([^\s])/$1 $2/gs);
138 $desc2 =~ s/\n\n(From |http:)/\n$1/gs;
140 $body =~ s@(<_description>)(.*?)(</_description>)@$1$desc1$3@s;
142 if ($obody eq $body) {
143 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
145 my $file_tmp = "$filename.tmp";
146 open(OUT, ">$file_tmp") || error ("$file_tmp: $!");
147 print OUT $body || error ("$file_tmp: $!");
148 close OUT || error ("$file_tmp: $!");
150 if (!rename ("$file_tmp", "$filename")) {
152 error ("mv \"$file_tmp\" \"$filename\": $!");
154 print STDERR "$progname: wrote $filename\n" if ($verbose);
157 return ($desc1, $desc2);
162 sub set_plist_key($$$$) {
163 my ($filename, $body, $key, $val) = @_;
167 \n\t<string>)([^<>]*)(</string>
169 # print STDERR "$progname: $filename: $key was: $2\n" if ($verbose);
170 $body = $1 . $val . $3;
172 error ("$filename: unparsable")
173 unless ($body =~ m@^(.*)(\n</dict>\n</plist>\n)$@s);
175 "\n\t<key>$key</key>" .
176 "\n\t<string>$val</string>" .
186 $app_dir =~ s@/+$@@s;
188 # "seticon" is from osxutils, http://osxutils.sourceforge.net/
190 my $icon = "$app_dir/../../../XScreenSaver.icns";
191 my @cmd = ("seticon", "-d", $icon, $app_dir);
192 print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
201 error ("$app_dir: no name")
202 unless ($app_dir =~ m@/([^/.]+).(app|saver)/?$@x);
205 my ($filename, $plist) = read_info_plist ($app_dir);
208 error ("$filename: no version number")
209 unless ($plist =~ m@<key>CFBundleShortVersionString</key>\s*
210 <string>([^<>]+)</string>@sx);
212 my ($ignore, $info_str) = update_saver_xml ($app_dir, $vers);
214 $info_str =~ m@^([^\n]+)\n@s ||
215 error ("$filename: unparsable copyright");
216 my $copyright = "$1";
217 $copyright =~ s/\b\d{4}-(\d{4})\b/$1/;
219 $plist = set_plist_key ($filename, $plist,
220 "NSHumanReadableCopyright", $copyright);
221 $plist = set_plist_key ($filename, $plist,
222 "CFBundleLongVersionString",$copyright);
223 $plist = set_plist_key ($filename, $plist,
224 "CFBundleGetInfoString", $info_str);
226 if ($oplist eq $plist) {
227 print STDERR "$progname: $filename: unchanged\n" if ($verbose > 1);
229 my $file_tmp = "$filename.tmp";
230 open(OUT, ">$file_tmp") || error ("$file_tmp: $!");
231 print OUT $plist || error ("$file_tmp: $!");
232 close OUT || error ("$file_tmp: $!");
234 if (!rename ("$file_tmp", "$filename")) {
236 error ("mv \"$file_tmp\" \"$filename\": $!");
238 print STDERR "$progname: wrote $filename\n" if ($verbose);
247 print STDERR "$progname: $err\n";
252 print STDERR "usage: $progname [--verbose] program.app ...\n";
258 while ($#ARGV >= 0) {
260 if ($_ eq "--verbose") { $verbose++; }
261 elsif (m/^-v+$/) { $verbose += length($_)-1; }
262 elsif (m/^-./) { usage; }
263 else { push @files, $_; }
265 usage() unless ($#files >= 0);