Ticket #5379: mythrename.patch

File mythrename.patch, 3.5 KB (added by johan@…, 4 years ago)

Patch file for enhancements to mythrename.pl

  • mythrename.pl

    old new  
    2222    use MythTV; 
    2323 
    2424# Some variables we'll use here 
    25     our ($dest, $format, $usage, $underscores, $live); 
     25    our ($dest, $filename, $format, $usage, $underscores, $live); 
    2626    our ($dformat, $dseparator, $dreplacement, $separator, $replacement); 
    2727    our ($db_host, $db_user, $db_name, $db_pass, $video_dir, $verbose); 
    2828    our ($hostname, $dbh, $sh, $q, $count, $base_dir); 
     
    4141 
    4242# Load the cli options 
    4343    GetOptions('link|dest|destination|path:s' => \$dest, 
     44               'file:s'                       => \$filename, 
    4445               'format=s'                     => \$format, 
    4546               'live'                         => \$live, 
    4647               'separator=s'                  => \$separator, 
     
    6970    WARNING: ALL symlinks within the destination directory and its 
    7071    subdirectories (recursive) will be removed when using the --link option. 
    7172 
     73--file filename 
     74 
     75    only rename the given file instead of the entire directory 
     76 
    7277--live 
    7378 
    7479    Include live tv recordings, affects both linking and renaming. 
     
    253258        } 
    254259    # Bad path 
    255260        die "$dest is not a directory.\n" unless (-d $dest); 
    256     # Delete any old links 
    257         find sub { if (-l $_) { 
    258                        unlink $_ or die "Couldn't remove old symlink $_: $!\n"; 
    259                    } 
    260                  }, $dest; 
     261    # Delete any old links if the --file option is not specified 
     262        if (!defined($filename)) { 
     263            find sub { if (-l $_) { 
     264                        unlink $_ or die "Couldn't remove old symlink $_: $!\n"; 
     265                       } 
     266                     }, $dest; 
     267        } 
    261268    # Delete empty directories (should this be an option?) 
    262269    # Let this fail silently for non-empty directories 
    263270        finddepth sub { rmdir $_; }, $dest; 
     
    273280    my %rows = $Myth->backend_rows('QUERY_RECORDINGS Delete'); 
    274281    foreach my $row (@{$rows{'rows'}}) { 
    275282        my $show = new MythTV::Recording(@$row); 
     283 
    276284    # Skip LiveTV recordings? 
    277285        next unless (defined($live) || $show->{'recgroup'} ne 'LiveTV'); 
    278286    # File doesn't exist locally 
    279287        next unless (-e $show->{'local_path'}); 
     288    # If the --file option has been given check to see if the current file is the one we need to rename 
     289        if (defined($filename)) { 
     290                next unless (($show->{'basename'} eq $filename) or ($show->{'local_path'} eq $filename)); 
     291        } 
    280292    # Format the name 
    281293        my $name = $show->format_name($format,$separator,$replacement,$dest,$underscores); 
    282294    # Get a shell-safe version of the filename (yes, I know it's not needed in this case, but I'm anal about such things) 
     
    289301        if ($dest) { 
    290302        # Check for duplicates 
    291303            if (-e "$dest/$name$suffix") { 
    292                 $count = 2; 
    293                 while (-e "$dest/$name.$count$suffix") { 
    294                     $count++; 
     304                if (!defined($filename)) { 
     305                    $count = 2; 
     306                    while (-e "$dest/$name.$count$suffix") { 
     307                        $count++; 
     308                    } 
     309                    $name .= ".$count"; 
     310                } else { 
     311                    unlink "$dest/$name$suffix" or die "Couldn't remove old symlink $dest/$name$suffix: $!\n"; 
    295312                } 
    296                 $name .= ".$count"; 
    297313            } 
    298314            $name .= $suffix; 
    299315        # Create the link