2 # Copyright © 2006-2013 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.2 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
25 $ENV{PATH} = "/opt/local/bin:$ENV{PATH}"; # MacPorts, for ImageMagick
31 my $exit_value = $? >> 8;
32 my $signal_num = $? & 127;
33 my $dumped_core = $? & 128;
34 error ("$cmd[0]: core dumped!") if ($dumped_core);
35 error ("$cmd[0]: signal $signal_num!") if ($signal_num);
36 error ("$cmd[0]: exited with $exit_value!") if ($exit_value);
40 # Returns true if the two files differ (by running "cmp")
43 my ($file1, $file2) = @_;
45 my @cmd = ("cmp", "-s", "$file1", "$file2");
46 print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
50 my $exit_value = $? >> 8;
51 my $signal_num = $? & 127;
52 my $dumped_core = $? & 128;
54 error ("$cmd[0]: core dumped!") if ($dumped_core);
55 error ("$cmd[0]: signal $signal_num!") if ($signal_num);
61 my ($src_dir, $app_dir) = @_;
63 # Apparently Apple wants Resources/{thumbnail.png to be 90x58,
64 # and Resources/thumbnail@2x.png to be 180x116. Let's just
65 # make the former, but make it be the latter's size.
69 error ("$app_dir does not exist") unless (-d $app_dir);
70 error ("$app_dir: no name")
71 unless ($app_dir =~ m@/([^/.]+).(saver|app)/?$@x);
75 $app_dir .= "/Contents/Resources";
77 error ("$app_dir does not exist") unless (-d $app_dir);
78 my $target = "$app_dir/thumbnail.png";
80 $src_dir .= "/" unless ($src_dir =~ m@/$@s);
81 my $src_dir2 = "${src_dir}retired/";
83 $app_name =~ s/rdbomb/rd-bomb/si; # sigh
85 my $img = $src_dir . lc($app_name) . ".jpg";
86 my $img2 = $src_dir2 . lc($app_name) . ".jpg";
87 $img = $img2 if (! -f $img && -f $img2);
88 error ("$img does not exist") unless (-f $img);
90 my $tmp = sprintf ("%s/thumb-%08x.png",
91 ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp"),
95 "-resize", $size . "^",
100 print STDERR "$progname: exec: " . join(' ', @cmd) . "\n"
106 error ("failed: " . join(" ", @cmd));
109 if (! cmp_files ($tmp, $target)) {
111 print STDERR "$progname: $target: unchanged\n" if ($verbose > 1);
112 } elsif (! rename ($tmp, $target)) {
114 error ("mv $tmp $target: $!");
116 print STDERR "$progname: wrote $target\n" if ($verbose);
123 print STDERR "$progname: $err\n";
128 print STDERR "usage: $progname [--verbose] image-dir program.app ...\n";
136 while ($_ = $ARGV[0]) {
138 if (m/^--?verbose$/s) { $verbose++; }
139 elsif (m/^-v+$/) { $verbose += length($_)-1; }
140 elsif (m/^--?q(uiet)?$/s) { $verbose = 0; }
141 elsif (m/^-/s) { usage(); }
142 elsif (! $src_dir) { $src_dir = $_; }
143 else { push @files, $_; }
145 usage() unless ($src_dir && $#files >= 0);
147 update ($src_dir, $_);