Ticket #5379: mythrename.patch
| File mythrename.patch, 3.5 KB (added by johan@…, 4 years ago) |
|---|
-
mythrename.pl
old new 22 22 use MythTV; 23 23 24 24 # Some variables we'll use here 25 our ($dest, $f ormat, $usage, $underscores, $live);25 our ($dest, $filename, $format, $usage, $underscores, $live); 26 26 our ($dformat, $dseparator, $dreplacement, $separator, $replacement); 27 27 our ($db_host, $db_user, $db_name, $db_pass, $video_dir, $verbose); 28 28 our ($hostname, $dbh, $sh, $q, $count, $base_dir); … … 41 41 42 42 # Load the cli options 43 43 GetOptions('link|dest|destination|path:s' => \$dest, 44 'file:s' => \$filename, 44 45 'format=s' => \$format, 45 46 'live' => \$live, 46 47 'separator=s' => \$separator, … … 69 70 WARNING: ALL symlinks within the destination directory and its 70 71 subdirectories (recursive) will be removed when using the --link option. 71 72 73 --file filename 74 75 only rename the given file instead of the entire directory 76 72 77 --live 73 78 74 79 Include live tv recordings, affects both linking and renaming. … … 253 258 } 254 259 # Bad path 255 260 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 } 261 268 # Delete empty directories (should this be an option?) 262 269 # Let this fail silently for non-empty directories 263 270 finddepth sub { rmdir $_; }, $dest; … … 273 280 my %rows = $Myth->backend_rows('QUERY_RECORDINGS Delete'); 274 281 foreach my $row (@{$rows{'rows'}}) { 275 282 my $show = new MythTV::Recording(@$row); 283 276 284 # Skip LiveTV recordings? 277 285 next unless (defined($live) || $show->{'recgroup'} ne 'LiveTV'); 278 286 # File doesn't exist locally 279 287 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 } 280 292 # Format the name 281 293 my $name = $show->format_name($format,$separator,$replacement,$dest,$underscores); 282 294 # 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) … … 289 301 if ($dest) { 290 302 # Check for duplicates 291 303 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"; 295 312 } 296 $name .= ".$count";297 313 } 298 314 $name .= $suffix; 299 315 # Create the link
