#!/usr/pkg/bin/perl -w

# perl script to find duplicate files within a directory structure.

use strict;
use Digest::MD5;
use File::Find;

$|=1;

my ($dir,$dig, $file, $filecount, $tick);
my (%h);

my @wheel = ('\\','|','/','-');

sub Usage {
    printf "Usage: $0 /path [/path2 ... ]\n";
}


sub spin {
    $tick++;
    print STDERR "\r" . $wheel[$tick % scalar(@wheel)];
}

sub GetMD5 {
    my ($path, $digest, $ctx);

    $path = $File::Find::name;
    if ( -d $path ) {
        1;
    } elsif ( -f $path ) {
	if (-s $path) {
	    if ( open(FILE, $path) ) {
                $filecount++;
                $ctx = Digest::MD5->new;

                $ctx->addfile(*FILE);

                $digest = $ctx->hexdigest;
                close(FILE);

                push @{ $h{$digest} }, $path;
                spin;
            } else {
                print STDERR "\ncould not open file ($path): $!\n";
            }
        }
    } else {
        print STDERR "\nfile ($path) unreadable: $!\n";
    }

}


if ((scalar @ARGV) < 1) {
    &Usage;
    exit(1);
}

find(\&GetMD5, @ARGV);

foreach $dig (keys %h) {
    
    if ((scalar @{$h{$dig}}) > 1 ) {
        printf ("%s: %s\n",$dig,join(" ",@{$h{$dig}}));
    }
}

