#!/usr/bin/perl
 
# Written by Limeng
# May 27, 2010 Changsha
 
use strict;
use File::Basename;
use warnings;

my $error_file = "error.txt";
my $clone_report_file = "clone_report.txt";
 
my @error;
my @path_list;
if ($ARGV[0] =~ /^(-h|-\?|\/\?|-help)$/i or !@ARGV) {
        print "\n Usage: find_clone [path1] [path2] [path3] ... \n\n Press Enter to close me ... \7";
         <STDIN>;
         exit;
}
else {
        foreach (@ARGV) {
                 s/\\/\//g;
                 chop if (/.*\/$/);
                 if (! -d $_) {
                          print " The path ($_) does not exist! Please check!\n\7";
                          push (@error,"The path ($_) does not exist! Please check!");
                  }
                  else { push(@path_list,$_); }
        }
        if (!@path_list) {
                 print "\n The path(s) you input are invalid, try again please!\n\n Press Enter to close me ... \7";
                 <STDIN>;
                 exit;
        }
}
 
$| = 1;
print "\n Scanning the path(s) ... ";
my @file_list;
foreach (@path_list) { readsub($_); }
print "Done!\n\n Analyzing ... \n\n";

my %files;

foreach my $path (@file_list) {
    my ($name, $dir) = fileparse($path);
    push @{$files{$name}}, $path;
}

foreach my $file (keys %files) {
    if (@{$files{$file}} > 1) {
        print "@{$files{$file}}\n";
    }
}

 

#my @progress = ('-','-','-','\\','\\','\\','|','|','|','/','/','/');
#my $s = 0;
#open FH, ">$clone_report_file";
#foreach (keys %clone) {
#        if (@{$clone{$_}} > 1) {
#                 foreach(@{$clone{$_}}) {
#                         my @stat = stat($_);
#                         print FH time_transfer($stat[9]),"\t$_\n";
#                 }
#                 print FH "\n";
#                 $clone++;
#         }
#}
#if ($clone) { print qq~\n\n $clone group(s) clone files found, details see "$clone_report_file"!\n~; }
#else {
#        print qq~\n\n No clone files found!\n~;
#        print FH qq~No clone files found!~;
#}
#close FH;
 
if (@error) {
        open FH,">$error_file";
        foreach (@error) { print FH "$_\n"; }
         close FH;
         print "\n ",$#error+1," error(s) ocurred, details please see \"$error_file\"!";
}
else { print qq~\n No error ocurred!~; }
 
print "\n";
sub readsub
{
        my $file_t = shift;
        if (-f $file_t)
        {
                push(@file_list,$file_t);
        }
        if (-d $file_t) {
                opendir(AA,$file_t);
                my @list = readdir(AA);
                close (AA);
                my $file_to_act;
                foreach $file_to_act (sort @list)
                {
                        if ($file_to_act =~ /^\.|\.$/) { next; }
                        else
                        {
                                readsub("$file_t/$file_to_act");
                        }
                }
        }
}