2 # Copyright © 2006-2012 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 # Converts and installs a thumbnail image inside a .saver bundle.
14 # Created: 26-Jul-2012.
17 #use diagnostics; # Fails on some MacOS 10.5 systems
20 my $progname = $0; $progname =~ s@.*/@@g;
21 my $version = q{ $Revision: 1.1 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
29 my $exit_value = $? >> 8;
30 my $signal_num = $? & 127;
31 my $dumped_core = $? & 128;
32 error ("$cmd[0]: core dumped!") if ($dumped_core);
33 error ("$cmd[0]: signal $signal_num!") if ($signal_num);
34 error ("$cmd[0]: exited with $exit_value!") if ($exit_value);
38 # Returns true if the two files differ (by running "cmp")
41 my ($file1, $file2) = @_;
43 my @cmd = ("cmp", "-s", "$file1", "$file2");
44 print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
48 my $exit_value = $? >> 8;
49 my $signal_num = $? & 127;
50 my $dumped_core = $? & 128;
52 error ("$cmd[0]: core dumped!") if ($dumped_core);
53 error ("$cmd[0]: signal $signal_num!") if ($signal_num);
59 my ($src_dir, $app_dir) = @_;
61 # Apparently Apple wants Resources/{thumbnail.png to be 90x58,
62 # and Resources/thumbnail@2x.png to be 180x116. Let's just
63 # make the former, but make it be the latter's size.
67 error ("$app_dir does not exist") unless (-d $app_dir);
68 error ("$app_dir: no name")
69 unless ($app_dir =~ m@/([^/.]+).(saver|app)/?$@x);
73 $app_dir .= "/Contents/Resources";
75 error ("$app_dir does not exist") unless (-d $app_dir);
76 my $target = "$app_dir/thumbnail.png";
78 $src_dir .= "/" unless ($src_dir =~ m@/$@s);
79 my $src_dir2 = "${src_dir}retired/";
81 $app_name =~ s/rdbomb/rd-bomb/si; # sigh
83 my $img = $src_dir . lc($app_name) . ".jpg";
84 my $img2 = $src_dir2 . lc($app_name) . ".jpg";
85 $img = $img2 if (! -f $img && -f $img2);
86 error ("$img does not exist") unless (-f $img);
88 my $tmp = sprintf ("%s/thumb-%08x.png",
89 ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp"),
93 "-resize", $size . "^",
98 print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
104 error ("failed: " . join(" ", @cmd));
107 if (! cmp_files ($tmp, $target)) {
109 print STDERR "$progname: $target: unchanged\n" if ($verbose > 1);
110 } elsif (! rename ($tmp, $target)) {
112 error ("mv $tmp $target: $!");
114 print STDERR "$progname: wrote $target\n" if ($verbose);
121 print STDERR "$progname: $err\n";
126 print STDERR "usage: $progname [--verbose] image-dir program.app ...\n";
134 while ($_ = $ARGV[0]) {
136 if (m/^--?verbose$/s) { $verbose++; }
137 elsif (m/^-v+$/) { $verbose += length($_)-1; }
138 elsif (m/^--?q(uiet)?$/s) { $verbose = 0; }
139 elsif (m/^-/s) { usage(); }
140 elsif (! $src_dir) { $src_dir = $_; }
141 else { push @files, $_; }
143 usage() unless ($src_dir && $#files >= 0);
145 update ($src_dir, $_);