--- /usr/share/doc/mythtv-docs-0.21/contrib/mythrename.pl	2009-06-14 03:30:39.000000000 -0600
+++ /shared/sbin/mythrename.pl	2009-07-14 19:23:17.000000000 -0600
@@ -22,7 +22,7 @@
     use MythTV;
 
 # Some variables we'll use here
-    our ($dest, $format, $usage, $underscores, $live);
+    our ($dest, $dest_hard, $format, $usage, $underscores, $live);
     our ($dformat, $dseparator, $dreplacement, $separator, $replacement);
     our ($db_host, $db_user, $db_name, $db_pass, $video_dir, $verbose);
     our ($hostname, $dbh, $sh, $q, $count, $base_dir);
@@ -41,6 +41,7 @@
 
 # Load the cli options
     GetOptions('link|dest|destination|path:s' => \$dest,
+               'hardlink:s'                   => \$dest_hard,
                'format=s'                     => \$format,
                'live'                         => \$live,
                'separator=s'                  => \$separator,
@@ -69,6 +70,14 @@
     WARNING: ALL symlinks within the destination directory and its
     subdirectories (recursive) will be removed when using the --link option.
 
+--hardlink [destination directory]
+
+    Exactly the same as --link, only hardlinks are created, not symlinks.
+
+    WARNING:  It's allowed to use both --link and --hardlink in the same
+    command, but strange things may happen if you attempt to use the same
+    destination for both!
+
 --live
 
     Include live tv recordings, affects both linking and renaming.
@@ -263,13 +272,42 @@
         finddepth sub { rmdir $_; }, $dest;
     }
 
+# Hardlink destination
+    if (defined($dest_hard)) {
+    # Double-check the destination
+        $dest_hard ||= "$base_dir/show_names";
+    # Alert the user
+        vprint("Hardlink destination directory:  $dest_hard");
+    # Create nonexistent paths
+        unless (-e $dest_hard) {
+            mkpath($dest_hard, 0, 0755) or die "Failed to create $dest_hard:  $!\n";
+        }
+    # Bad path
+        die "$dest_hard is not a directory.\n" unless (-d $dest_hard);
+    # Delete any old hardlinks (symlinks too!)
+        find sub { if (-l $_) {
+                       unlink $_ or die "Couldn't remove old symlink $_: $!\n";
+                   }
+                   elsif (! -d _) {
+                   # hardlinks have a link count > 1
+                       my $nlink = (stat _)[3];
+                       if ($nlink > 1) {
+                           unlink $_ or die "Couldn't remove old hardlink $_: $!\n";
+                       }
+                   }
+                 }, $dest_hard;
+    # Delete empty directories (should this be an option?)
+    # Let this fail silently for non-empty directories
+        finddepth sub { rmdir $_; }, $dest_hard;
+    }
+
 # Only if we're renaming files
-    unless ($dest) {
+    unless ($dest or $dest_hard) {
         $q  = 'UPDATE recorded SET basename=? WHERE chanid=? AND starttime=FROM_UNIXTIME(?)';
         $sh = $dbh->prepare($q);
     }
 
-# Create symlinks for the files on this machine
+# Create symlinks/hardlinks for the files on this machine
     my %rows = $Myth->backend_rows('QUERY_RECORDINGS Delete');
     foreach my $row (@{$rows{'rows'}}) {
         my $show = new MythTV::Recording(@$row);
@@ -277,8 +315,6 @@
         next unless (defined($live) || $show->{'recgroup'} ne 'LiveTV');
     # File doesn't exist locally
         next unless (-e $show->{'local_path'});
-    # Format the name
-        my $name = $show->format_name($format,$separator,$replacement,$dest,$underscores);
     # 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)
         my $safe_file = $show->{'local_path'};
         $safe_file =~ s/'/'\\''/sg;
@@ -286,28 +322,55 @@
     # Figure out the suffix
         my ($suffix) = ($show->{'basename'} =~ /(\.\w+)$/);
     # Link destination
-        if ($dest) {
-        # Check for duplicates
-            if (-e "$dest/$name$suffix") {
-                $count = 2;
-                while (-e "$dest/$name.$count$suffix") {
-                    $count++;
+        if ($dest or $dest_hard) {
+            if ($dest) {
+                # Format the name
+                my $name = $show->format_name($format,$separator,$replacement,$dest,$underscores);
+                # Check for duplicates
+                if (-e "$dest/$name$suffix") {
+                    $count = 2;
+                    while (-e "$dest/$name.$count$suffix") {
+                        $count++;
+                    }
+                    $name .= ".$count";
+                }
+                $name .= $suffix;
+                # Create the link
+                my $directory = dirname("$dest/$name");
+                unless (-e $directory) {
+                    mkpath($directory, 0, 0755)
+                        or die "Failed to create $directory:  $!\n";
                 }
-                $name .= ".$count";
+                symlink $show->{'local_path'}, "$dest/$name"
+                    or die "Can't create symlink $dest/$name:  $!\n";
+                vprint("$dest/$name");
             }
-            $name .= $suffix;
-        # Create the link
-            my $directory = dirname("$dest/$name");
-            unless (-e $directory) {
-                mkpath($directory, 0, 0755)
-                    or die "Failed to create $directory:  $!\n";
+             if ($dest_hard) {
+                # Format the name
+                my $name = $show->format_name($format,$separator,$replacement,$dest_hard,$underscores);
+                # Check for duplicates
+                if (-e "$dest_hard/$name$suffix") {
+                    $count = 2;
+                    while (-e "$dest_hard/$name.$count$suffix") {
+                        $count++;
+                    }
+                    $name .= ".$count";
+                }
+                $name .= $suffix;
+                # Create the link
+                my $directory = dirname("$dest_hard/$name");
+                unless (-e $directory) {
+                    mkpath($directory, 0, 0755)
+                        or die "Failed to create $directory:  $!\n";
+                }
+                link $show->{'local_path'}, "$dest_hard/$name"
+                    or die "Can't create symlink $dest_hard/$name:  $!\n";
+                vprint("$dest_hard/$name");
             }
-            symlink $show->{'local_path'}, "$dest/$name"
-                or die "Can't create symlink $dest/$name:  $!\n";
-            vprint("$dest/$name");
-        }
+       }
     # Rename the file, but only if it's a real file
         elsif (-f $show->{'local_path'}) {
+            my $name = $show->format_name($format,$separator,$replacement,undef,$underscores);
             if ($show->{'basename'} ne $name.$suffix) {
             # Check for duplicates
                 $video_dir = $sgroup->FindRecordingDir($show->{'basename'});
