Ticket #874: mythrename-sub.diff

File mythrename-sub.diff, 5.5 KB (added by tom@…, 18 years ago)

patch that works with SVN 8669

  • mythrename.pl

     
    2222    use File::Path;
    2323
    2424# Some variables we'll use here
    25     our ($dest, $format, $usage, $underscores, $live);
     25    our ($dest, $format, $usage, $underscores, $live, $sub);
    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, $sh2, $q, $q2, $count);
     
    4444               'format=s'                     => \$format,
    4545               'live'                         => \$live,
    4646               'separator=s'                  => \$separator,
     47               'sub'                          => \$sub,
    4748               'replacement=s'                => \$replacement,
    4849               'usage|help|h'                 => \$usage,
    4950               'underscores'                  => \$underscores,
     
    6667
    6768    /var/video/show_names/
    6869
     70--sub
     71
     72    Only takes effect with --link option. If you would like to make subdirectories
     73    per title, specify --sub. In the future you will be able to pick the field
     74    to sub on. Any titles with spaces in the name will have the spaces changed to
     75    underscores to simplify deleting.
     76
     77    default:  off
     78
    6979--live
    7080
    7181    Include live tv recordings, affects both linking and renaming. This assumes
    72     that recgroup = "LiveTV" for live tv recordings.
     82    that recgroup = "LiveTV" for live tv recordings. Take care that you include
     83    the time in your format if you channel surf alot, otherwise you may get
     84    errors when it tries to link to the same file twice.
    7385
    7486    default: do not link/rename live tv recordings
    7587
     
    231243        $dest ||= "$video_dir/show_names";
    232244    # Alert the user
    233245        if (defined($verbose)) {
     246            print "------------------------------------------------------------\n";
    234247            print "Link destination directory:  $dest\n";
     248            print "------------------------------------------------------------\n";
    235249        }
    236250    # Create nonexistent paths
    237251        unless (-e $dest) {
     
    240254    # Bad path
    241255        die "$dest is not a directory.\n" unless (-d $dest);
    242256    # Delete any old links
    243         foreach my $file (<$dest/*>) {
    244             next unless (-l $file);
    245             unlink $file or die "Couldn't remove old symlink $file:  $!\n";
     257        foreach my $deldir (<$dest/*>) {
     258            if (-d $deldir) {
     259               if (defined($verbose)) {
     260                    print "Deleting subdirectory $deldir\n";
     261               }
     262                rmtree($deldir);
     263            }
     264            foreach my $delfile (<$deldir/*>) {
     265                if (-l $delfile) {
     266                   if (defined($verbose)) {
     267                        print "Removing symlink $delfile\n";
     268                   }
     269                    unlink $delfile or die "Couldn't remove old symlink $delfile:  $!\n";
     270                }
     271            }
    246272        }
     273        if (defined($verbose)) {
     274            print "------------------------------------------------------------\n";
     275        }
    247276    }
    248277
    249278# Prepare a database queries
     
    352381        $name =~ s/(?:(?:$safe_sep)+\s*)+(?=[^\d\s])/$separator /sg;
    353382        $name =~ s/^($safe_sep|$safe_rep|\ )+//s;
    354383        $name =~ s/($safe_sep|$safe_rep|\ )+$//s;
     384        if (defined($sub)) {
     385            # Remove spaces from titles to simplify unlinking (I'm lazy)
     386            $title = $fields{'T'};
     387            $title =~ s/\s/\_/sg;
     388        }
    355389    # Underscores?
    356390        if ($underscores) {
    357391            $name =~ tr/ /_/s;
     
    374408                $name .= ".$count";
    375409            }
    376410            $name .= $suffix;
    377         # Create the link
    378             symlink "$video_dir/".$info{'basename'}, "$dest/$name"
    379                 or die "Can't create symlink $dest/$name:  $!\n";
    380             if (defined($verbose)) {
    381                 print "$dest/$name\n";
     411                if (defined($verbose)) {
     412                    print "Trying to link $dest/$name\n";
     413                }
     414        # Sub destination
     415            if (defined($sub)) {
     416            # Double-check the destination
     417                my $subdir = "$dest/$title";
     418            # Alert the user
     419                if (defined($verbose)) {
     420                    print "Link destination sub-directory:  $subdir\n";
     421                }
     422            # Create nonexistent paths
     423                unless (-e $subdir) {
     424                    mkpath($subdir, 0, 0755) or die "Failed to create $subdir:  $!\n";
     425                }
     426            # Bad path
     427                die "$subdir is not a directory.\n" unless (-d $subdir);
     428                symlink "$video_dir/".$info{'basename'}, "$subdir/$name"
     429                    or die "Can't create symlink $subdir/$name:  $!\n";
     430                if (defined($verbose)) {
     431                    print "Created link $subdir/$name\n";
     432                }
     433            } else {
     434
     435            # Create the link
     436                symlink "$video_dir/".$info{'basename'}, "$dest/$name"
     437                    or die "Can't create symlink $dest/$name:  $!\n";
     438                if (defined($verbose)) {
     439                    print "$dest/$name\n";
     440                }
    382441            }
    383442        }
    384443    # Rename the file
     
    407466                }
    408467            }
    409468        }
     469        if (defined($verbose)) {
     470            print "------------------------------------------------------------\n";
     471        }
    410472    }
    411473
    412474    $sh->finish;