#!/usr/bin/perl
#directory.plx

#This is a perl routine that will descend directories starting at the directory
#of your choice [directory] and move a file to a directory called 
#/cygdrive/g/Du#plicates if there exists another file in the 
#same directory which is the exact same or has a 01 attached before the .m4a 
#extension
#An example is the set of files "Rubber Soul.m4a" and "Rubber Soul 01.m4a"
#"Rubber Soul 01.m4a" will be moved
#
#From the command line type
#$perl Fully_Functional_Move_file [directory]


###################################################
#ADD packageds to get paths
use Cwd 'abs_path';
use Cwd;
use File::Copy;
####################################################
#Get the path of the Top Directory and switch to it

my $Directory_Before_Change=getcwd;
my $File_Container=$Directory_Before_Change."/Sorted_Files.txt";
my $Possible_File_Container=$Directory_Before_Change."Possibles";

open OUT1, "> $File_Container" or die "Can't Open $File_Container: $!\n";
open OUT2, "> $Possible_File_Container" or die "Can't Open $Possible_File_Container: $!\n";

my $Begin_Directory=abs_path($ARGV[0]);
chdir $Begin_Directory;
print "$Begin_Directory\n";

my $cur_directory=getcwd;
my $file_handle_index=0;
my $depth=0;
my $delimiter="/";

#print "$cur_directory\n";

my $count=0;
my $previous_file="Dummy";
my @Files;

my @subdirectories;	#Indexing scheme for filehandles
$subdirectories[0]="/";
my @indexing=("IN_ONE","IN_TWO","IN_THREE","IN_FOUR","IN_FIVE","IN_SIX");

print "$indexing[0]\n";
foreach(@indexing){print "$_\n"}

opendirectory($cur_directory);		#Run the subroutine, get list of files
@Sorted_Files=sort @Files;		#Sort Files using sort function


#Go through Sorted Files, eliminate .m4a or .mp3 extension and compare adjacent strings.  If they differ by 1, move that ass into different directory

foreach(@Sorted_Files){			
	@current=($_=~m/\//g);		
	$current_filename=$';		
	$current_filename_wo_ext=substr($current_filename,0,-1);
	@previous=($previous_file=~m/\//g);
	$previous_filename=$';
	$previous_filename_wo_ext=substr($previous_filename,0,-1);
	
	if (substr($current_filename,0,-4) eq substr($previous_filename,0,-4)){
	print OUT1 "Found A Duplicate Called: $previous_file\n";
	++$count;
	}
  	if (substr($current_filename,0,-4) eq substr($previous_filename,0,-6)){
	print OUT1 "Found A Duplicate Called: $previous_filename\n";
	move($previous_file,"/cygdrive/g/Duplicates/".$previous_file);
	++$count;
	}
	if (substr($current_filename,0,2) eq substr($previous_filename,0,2)){
	print OUT2 "Possible Duplicate:.$previous_file";
	}
	
	$previous_file=$_;

	
	#print OUT "$current_filename_wo_ext\n";
	#print OUT "$previous_filename_wo_ext\n";

	#print OUT "$_\n";
	}
	
	print "Total Amount of Duplicates:$count\n";
	
#########################
#Recursive Call to Function opendirectory
#########################

sub opendirectory{

	my $directory=shift(@_);
	#print "$directory\n";
	chdir $directory;	
	opendir($indexing[$file_handle_index],$directory) or die "Can't Open Directory $directory: $!\n";
	while ($_=readdir($indexing[$file_handle_index])){
		$full_path=$new_directory.$_;	
		next if $_ eq "." or $_ eq "..";
		if (!-d $_){
			$Files[++$#Files]=$full_path;
			#$count++;
		}
		#$full_path=abs_path($_);
		#print "$full_path\n";

		if (-d $_){
			#print "Entering New Directory\n";
			$depth++;
			#print "$depth\n";
			$file_handle_index++;
			$subdirectories[$depth]=$subdirectories[($depth-1)].$_.$delimiter;	
			$new_directory=$cur_directory.$subdirectories[$depth];
			#print "$new_directory\n";
			opendirectory($new_directory);
			#print "Leaving Directory\n";
			pop @subdirectories;
			$depth--;
			$old_directory=$cur_directory.$subdirectories[$depth];
			chdir $old_directory;

			$file_handle_index--;
		}			
	}
}

	
