- my $i;
-
-
- $SIG{CHLD} = \&reaper;
- while (1) {
- if (scalar(@jobs) <= $MAXJOBS) {
- foreach $i (glob('*/tags')) {
- my $dir = dirname($i);
- my $pid =
- Jobs::newjob(sub {
- open(STDERR, ">$dir/log")
- or die("open(STDERR, >$dir/log): $!");
- return flac($dir);
- }, 'debug'=>$debug);
- verbose("new job $pid for $dir\n");
- @Jobs::jobs <= $MAXJOBS or last;
+ my $dir;
+ my @jobs;
+
+ # Get a job for Jobs::run. On each call, look for new fa-rip
+ # directories and append an item to the queue @jobs for each wav
+ # file therein. Then, if we have anything in the queue, return a
+ # function to call flac for it, otherwise sleep for a bit. This
+ # looks forever, never returning undef, so Jobs::run never returns.
+ my $getjob = sub {
+ # Look for new fa-rip directories.
+ while (1) {
+ for my $i (glob('*/tags')) {
+ $dir = dirname($i);
+
+ verbose("Renaming $dir/tags\n");
+ rename("$dir/tags", "$dir/using-tags")
+ or die("rename($dir/tags, $dir/using-tags): $!");
+
+ my ($artist, $album,
+ $discnum, @tags) = get_tags("$dir/using-tags");
+ if (-e "$dir/wav") {
+ # single-file
+ push(@jobs,
+ [$dir, $artist, $album, $discnum,
+ undef, undef, undef, @tags]);
+ } else {
+ #multi-file
+ # Don't need cue file.
+ unlink("$dir/cue") or die("unlink($dir/cue): $!");
+
+ # Go over @tags, store all [n] tags in a list keyed by
+ # n in %tracks_to_tags, store all ARTIST (not
+ # ARTIST[n]) tags in @disc_artist, and leave the rest
+ # in @tags.
+ my %tracks_to_tags;
+ my @disc_artist;
+ my @tmp;
+ for my $tag (@tags) {
+ if ($tag =~ /^([^[]+)\[(\d+)]=(.*)/) {
+ push(@{$tracks_to_tags{$2}->{$1}}, $3);
+ } elsif ($tag =~ /^ARTIST=/) {
+ push(@disc_artist, $tag);
+ } else {
+ push(@tmp, $tag);
+ }
+ }
+ @tags = @tmp;
+
+ push(@jobs,
+ map {
+ [$dir, $artist, $album, $discnum, $_,
+ $tracks_to_tags{$_}, \@disc_artist, @tags]
+ } sort(map(int, keys(%tracks_to_tags))));
+ }