--- mythlink.pl-orig	2011-04-26 12:49:39.000000000 -0400
+++ mythlink.pl	2011-04-26 15:22:21.000000000 -0400
@@ -22,7 +22,7 @@
     use MythTV;
 
 # Some variables we'll use here
-    our ($dest, $format, $usage, $underscores, $live, $rename);
+    our ($dest, $format, $usage, $underscores, $live, $rename, $maxlength);
     our ($chanid, $starttime, $filename);
     our ($dformat, $dseparator, $dreplacement, $separator, $replacement);
     our ($db_host, $db_user, $db_name, $db_pass, $video_dir, $verbose);
@@ -39,6 +39,7 @@
     $format      = $dformat;
     $separator   = $dseparator;
     $replacement = $dreplacement;
+    $maxlength   = -1;
 
 # Load the cli options
     GetOptions('link|dest|destination|path:s' => \$dest,
@@ -50,6 +51,7 @@
                'separator=s'                  => \$separator,
                'replacement=s'                => \$replacement,
                'rename'                       => \$rename,
+               'maxlength=i'                  => \$maxlength,
                'usage|help|h'                 => \$usage,
                'underscores'                  => \$underscores,
                'verbose'                      => \$verbose
@@ -227,6 +229,19 @@
     represents recordings using human-readable file names or use mythlink.pl to
     create links with human-readable names to the recording files.
 
+--maxlength length
+
+    Ensure the link name is length or fewer characters.  If the link name is
+    longer than length, truncate the name.  Zero or any negative value for
+    length disables length checking.
+
+    Note that this option does not take into account the path length, so on a
+    file system used by applications with small path limitations (such as
+    Windows Explorer and Windows Command Prompt), you should specify a length
+    that takes into account characters used by the path to the dest directory.
+
+    default:  Unlimited
+
 --verbose
 
     Print debug info.
@@ -247,6 +262,12 @@
         die "The arguments --chanid and --starttime must be used together.\n";
     }
 
+# Ensure --maxlength specifies a reasonable value (though filenames may
+# still be useless at such short lengths)
+    if ($maxlength > 0 and $maxlength < 20) {
+        die "The --maxlength must be 20 or higher.\n";
+    }
+
 # Check the separator and replacement characters for illegal characters
     if ($separator =~ /(?:[\/\\:*?<>|"])/) {
         die "The separator cannot contain any of the following characters:  /\\:*?<>|\"\n";
@@ -353,14 +374,48 @@
         $safe_file = "'$safe_file'";
     # Figure out the suffix
         my ($suffix) = ($show->{'basename'} =~ /(\.\w+)$/);
+    # Check the link name's length
+        if ($maxlength > 0) {
+            my $charsavailable = $maxlength - length($suffix);
+            if ($charsavailable > 0) {
+                $name = substr($name, 0, $charsavailable);
+            }
+            else {
+                vprint("Unable to represent recording; maxlength too small.");
+                next;
+            }
+        }
     # Link destination
     # Check for duplicates
         if (-e "$dest/$name$suffix") {
             if ((!defined($filename) and !defined($chanid)) or
                 (! -l "$dest/$name$suffix")) {
                 $count = 2;
+                if ($maxlength > 0) {
+                    my $charsavailable = $maxlength - length("$suffix.$count");
+                    if ($charsavailable > 0) {
+                        $name = substr($name, 0, $charsavailable);
+                    }
+                    else {
+                        vprint("Unable to represent recording; maxlength too ".
+                               "small.");
+                        next;
+                    }
+                }
                 while (-e "$dest/$name.$count$suffix") {
                     $count++;
+                    if ($maxlength > 0) {
+                        my $charsavailable = $maxlength -
+                                             length("$suffix.$count");
+                        if ($charsavailable > 0) {
+                            $name = substr($name, 0, $charsavailable);
+                        }
+                        else {
+                            vprint("Unable to represent recording; maxlength ".
+                                   "too small.");
+                            next;
+                        }
+                    }
                 }
                 $name .= ".$count";
             } else {
