Index: mythrename.pl
===================================================================
--- mythrename.pl	(revision 8370)
+++ mythrename.pl	(working copy)
@@ -22,7 +22,7 @@
     use File::Path;
 
 # Some variables we'll use here
-    our ($dest, $format, $usage, $underscores);
+    our ($dest, $format, $usage, $underscores, $sub);
     our ($dformat, $dseparator, $dreplacement, $separator, $replacement);
     our ($db_host, $db_user, $db_name, $db_pass, $video_dir);
     our ($hostname, $dbh, $sh, $sh2, $q, $q2, $count);
@@ -43,9 +43,11 @@
     GetOptions('link|dest|destination|path:s' => \$dest,
                'format=s'                     => \$format,
                'separator=s'                  => \$separator,
+               'sub'                          => \$sub,
                'replacement=s'                => \$replacement,
                'usage|help|h'                 => \$usage,
-               'underscores'                  => \$underscores
+               'underscores'                  => \$underscores,
+               'verbose'                      => \$verbose
               );
 
 # Print usage
@@ -64,6 +66,12 @@
 
     /var/video/show_names/
 
+--sub
+
+    Only takes effect with --link option. If you would like to make subdirectories
+    per title, specify --sub. In the future you will be able to pick the field
+    to sub on.
+
 --format
 
     default:  $dformat
@@ -124,6 +132,10 @@
 
     default:  No underscores
 
+--verbose
+
+    Print some debug info, otherwise be quiet.
+
 --help
 
     Show this help text.
@@ -215,7 +227,9 @@
     # Double-check the destination
         $dest ||= "$video_dir/show_names";
     # Alert the user
-        print "Link destination directory:  $dest\n";
+	if ($verbose) {
+            print "Link destination directory:  $dest\n";
+	}
     # Create nonexistent paths
         unless (-e $dest) {
             mkpath($dest, 0, 0755) or die "Failed to create $dest:  $!\n";
@@ -244,6 +258,7 @@
     while (my $ref = $sh->fetchrow_hashref()) {
         my %info = %{$ref};
         die "This script requires mythtv >= 0.19\n" unless ($info{'basename'});
+       	next unless (-e "$video_dir/".$info{'basename'});
     # Default times
         my ($syear, $smonth, $sday, $shour, $sminute, $ssecond) = $info{'starttime'} =~ /(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/;
         my ($eyear, $emonth, $eday, $ehour, $eminute, $esecond) = $info{'endtime'}   =~ /(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/;
@@ -330,6 +345,9 @@
         $name =~ s/(?:(?:$safe_sep)+\s*)+(?=[^\d\s])/$separator /sg;
         $name =~ s/^($safe_sep|$safe_rep|\ )+//s;
         $name =~ s/($safe_sep|$safe_rep|\ )+$//s;
+    # Remove spaces from titles to simplify unlinking (I'm lazy)
+	my $title = $info{'title'};
+        $title =~ s/\s/\_/sg;
     # Underscores?
         if ($underscores) {
             $name =~ tr/ /_/s;
@@ -352,10 +370,43 @@
                 $name .= ".$count";
             }
             $name .= $suffix;
-        # Create the link
-            symlink "$video_dir/".$info{'basename'}, "$dest/$name"
-                or die "Can't create symlink $dest/$name:  $!\n";
-            print "$dest/$name\n";
+
+        # Sub destination
+    	    if (defined($sub)) {
+    	    # Double-check the destination
+        	$sub = "$dest/$title";
+    	    # Alert the user
+ 	        if ($verbose) {
+        	    print "Link destination sub-directory:  $sub\n";
+		}
+    	    # Create nonexistent paths
+        	unless (-e $sub) {
+               	    mkpath($sub, 0, 0755) or die "Failed to create $sub:  $!\n";
+        	}
+    	    # Bad path
+        	die "$sub is not a directory.\n" unless (-d $sub);
+    	    # Delete any old links
+        	foreach my $file (<$sub/*>) {
+	            if ($verbose) {
+		        print "unlinking $file\n";
+		    }
+            	    next unless (-l $file);
+            	    unlink $file or die "Couldn't remove old symlink $file:  $!\n";
+        	}
+           	symlink "$video_dir/".$info{'basename'}, "$sub/$name"
+                    or die "Can't create symlink $sub/$name:  $!\n";
+                if ($verbose) {
+            	    print "$sub/$name\n";
+		}
+    	    } else {
+
+            # Create the link
+            	symlink "$video_dir/".$info{'basename'}, "$dest/$name"
+                    or die "Can't create symlink $dest/$name:  $!\n";
+		if ($verbose) {
+            	    print "$dest/$name\n";
+		}
+	    }
         }
     # Rename the file
         else {
@@ -378,7 +429,9 @@
                     $rows = $sh2->execute($info{'basename'}, $info{'chanid'}, $info{'starttime'});
                     die "Couldn't restore original basename in database for ".$info{'basename'}.":  ($q2)\n" unless ($rows == 1);
                 }
-                print $info{'basename'}."\t-> $name\n";
+		if ($verbose) {
+                    print $info{'basename'}."\t-> $name\n";
+		}
             }
         }
     }
