#!/usr/bin/perl -w
use File::Basename;
use Archive::Zip;
use warnings;

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) ... ";
foreach (@path_list) { readsub($_); }
print "Done!\n\n Analyzing ... \n\n";

eval{
$obj = Archive::Zip->new();
foreach my $myzip (@zip_list){
$status = $obj->read("$myzip");
}

if ($status != AZ_OK) {
die('Error in file!');
} else {
foreach $member ($obj->members()) {
push @file_list, [$member->fileName(), $member->externalFileName()];
}
}
my %files;
foreach my $path (@file_list) {
my ($name, $dir) = fileparse($path->[0]);
    push @{$files{$name}}, $path;
}
foreach my $file (keys %files) {
    if (@{$files{$file}} > 1) {
    print join(", ", map(@$_, @{$files{$file}})), "\n";
    }
}
};

sub readsub
{
        my $file_t = shift;
        push(@file_list,[$file_t," "]) if(-f $file_t && $file_t =~ /\.class$/);
        if (-f $file_t && $file_t =~ /\.jar$|\.ear$|\.war$/)
        {
                push(@zip_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");
                        }
                }
        }
}