Skip to content

Commit

Permalink
beet-cleanup-cover-only-directories: do not leave an empty log file a…
Browse files Browse the repository at this point in the history
…round
  • Loading branch information
trapd00r committed Jun 5, 2023
1 parent c850661 commit e1bae70
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions beet-cleanup-cover-only-directories
Expand Up @@ -21,6 +21,7 @@ my $logfile = 'dirs_with_only_cover_in_them_or_less_than_2M.log';
open(my $fh, '>', $logfile) or die "Can't open $logfile: $!";
$|++;

my $to_delete = 0;
sub check_dir {
my $dir = $_;
return unless -d $dir;
Expand All @@ -35,20 +36,26 @@ sub check_dir {
my $abspath = abs_path($dir);
print sprintf("only %s: %s\n", ls_color($cover_file), ls_color($abspath));
print $fh sprintf("rm -rv @{[ quotemeta($abspath) ]}\n");

$to_delete++;
return;
}
# handle leftover m3u+nfo+sfv files
elsif (scalar @files == scalar grep { /[.](?:m3u|nfo|sfv)$/ } @files) {
my $abspath = abs_path($dir);
print sprintf("only {nfo,sfv,m3u}: %s\n", ls_color($abspath));
print $fh sprintf("rm -rv @{[ quotemeta($abspath) ]}\n");

$to_delete++;
return;
}
# handle cases with leftover m3u+nfo+sfv + a random cover file
elsif (scalar @files == scalar grep { /[.](?:m3u|nfo|sfv|jpg)$/ } @files) {
my $abspath = abs_path($dir);
print sprintf("only {nfo,sfv,m3u,jpg}: %s\n", ls_color($abspath));
print $fh sprintf("rm -rv @{[ quotemeta($abspath) ]}\n");

$to_delete++;
return;
}

Expand All @@ -64,13 +71,18 @@ sub check_dir {

print sprintf("< 2M %s\n", ls_color($abspath));
print $fh sprintf("rm -rv @{[ quotemeta($abspath) ]}\n");

$to_delete++;
}
}

finddepth(\&check_dir, $start_path);



# remove the generated bash script when it's executed
print $fh 'rm -v $0';
close $fh;
if($to_delete) {
print $fh 'rm -v $0';
close $fh;
}
else {
unlink $logfile;
}

0 comments on commit e1bae70

Please sign in to comment.