#!/usr/bin/perl
use strict;
use warnings;

# reportLongPaths:
# This script reports (to STDOUT) the file paths that are longer than a
# specified threshold.
# The threshold length is expected as the first command-line argument.
# If no threshold length is specified, a default value of 255 is used.
# The directories to be searched are expected as the remaining command-line
# arguments. If no directories are specified, the current directory is searched.
# Sample usage:
#     reportLongPaths 100 ~/Documents ~/Movies
#
# Cameron Hayne (macdev@hayne.net)  January 2009

use File::Find;
use Cwd;

my $pathLenThreshold = 255; # default (can be overridden via command-line arg)

# checkFile: invoked from the 'find' routine on each file or directory in turn
sub checkFile()
{
    return if -d $_; # only interested in files, not directories
    
    my $fullPath = $File::Find::name;
    my $pathLen = length($fullPath);
    if ($pathLen > $pathLenThreshold)
    {
        print "$fullPath\n";
        #print "$pathLen\n"
    }
}

if (defined($ARGV[0]) and ($ARGV[0] =~ /^\d+$/))
{
    # an integer was supplied as the first command-line argument
    $pathLenThreshold = $ARGV[0];
    shift @ARGV;
}
# if no dirs supplied as command-line args, we search the current directory
my @searchDirs = @ARGV ? @ARGV : getcwd();
foreach my $searchDir (@searchDirs)
{
    die "$searchDir is not a directory\n" unless -d $searchDir;
    $searchDir = Cwd::abs_path($searchDir);
}
    
find(\&checkFile, @searchDirs);

