+my $MUSIC = $ENV{MUSIC} // "/srv/music";
+
+# get_item_thumbnails({ options }, file, ...)
+# get_item_thumbnails(file, ...)
+#
+# For each music file listed, obtain a thumbnail (if any) for the
+# cover art.
+#
+# The first argument is a hash reference to control the mode of
+# operation; it may be omitted for default options.
+#
+# get_item_thumbnails({ small => 1 }, ...) - smaller thumbnails
+#
+# The returned list consists of strings (in the same order as the filename
+# arguments) suitable for use directly in FVWM menus; by default the filename
+# is bracketed by asterisks (e.g., "*thumbnail.png*"); in small mode it is
+# surrounded by % (e.g., "%thumbnail.png%"). If no cover art was found, the
+# empty string is returned for that file.
+sub get_item_thumbnails {
+ my @results = ();
+ my $flags = {};
+ my @opts = ();
+
+ $flags = shift if (reftype($_[0]) eq "HASH");
+ return @results unless @_;
+
+ my $c = "*";
+ if ($flags->{small}) {
+ push @opts, "--small";
+ $c = "%";
+ }
+
+ foreach (@_) {
+ open THUMB, "-|", "$FindBin::Bin/thumbnail.zsh", "--music",
+ @opts, $_;
+ my $thumb = <THUMB>;
+ chomp $thumb;
+
+ $thumb = "$c$thumb$c" if (-f $thumb);
+ push @results, $thumb;
+ close THUMB;
+ die("thumbnail.zsh failed") if ($?);
+ }
+
+ return @results;
+}
+
+# Given a music filename, search for the cover art in the same directory.
+sub mpd_cover_filename {
+ my ($dir) = @_;
+ my $file;
+
+ $dir =~ s/\/[^\/]*$//;
+ foreach ("cover.png", "cover.jpg", "cover.tiff", "cover.bmp") {
+ if (-f "$dir/$_") {
+ $file = "$dir/$_";
+ last;
+ }
+ }
+ return unless defined $file;
+
+ # Follow one level of symbolic link to get to the scans directory.
+ $file = readlink($file) // $file;
+ $file = "$dir/$file" unless ($file =~ /^\//);
+ return $file;
+}