#!/usr/bin/perl -w
use strict;

my $top_directory = shift;

my %tigr_cornell = ();
my %cornell_tigr = (
"cLEB",	"TSH",
"cLEC",	"TCA",
"cLED",	"TOV",
"cLEE",	"TSE",
"cLEF",	"TMG",
"cLEG",	"TBF",
"cLEI",	"TGS",
"cLEM",	"TGF",
"cLEN",	"TRR",
"cLER",	"TPR",
"cLES",	"TPS",
"cLET",	"TME",
"cLEW",	"TRD",
"cLEX",	"TRX",
"cLEY",	"TRY",
"cLEZ",	"TRZ",
"cTOA",	"TFA",
"cTOB",	"TFB",
"cTOC",	"TFC",
"cTOD",	"TFD",
"cLHT",	"THT",
"cLPT",	"TPT",
"cLPP",	"TPO",
"cTOE",	"TAI",
"cTOF",	"TOF",
"cLECC","cC-esflcLEL",
"cSML",	"cC-smflcSML",
"cTOG",	"TDG",
"cLEM",	"TGF",
"cTOS",	"TSC",
"cLENV","tomato");

foreach ( keys %cornell_tigr ) {
    if (defined($tigr_cornell{$cornell_tigr{$_}})) {
	print STDERR "$cornell_tigr{$_} already has cornell code $tigr_cornell{$cornell_tigr{$_}}, not $_\n";
    }
    $tigr_cornell{$cornell_tigr{$_}} = $_;
}




process_directory($top_directory);

sub process_directory {
    my ($directory_name) = @_;

    print STDERR "Processing directory $directory_name\n";
    
    opendir THISDIR, "$directory_name"
      or die "Failed opening directory \"$directory_name\" ($!)";

    my @directory = readdir(THISDIR);
    close THISDIR;
    
    my @directory_files = grep { (!/^\./) && -f "$directory_name/$_" } @directory;

    my @directory_dirs = grep { (!/^\./) && -d "$directory_name/$_" } @directory;
    
#    print join("\t",@directory_dirs),"\n";

    my ($file, $tigr_library, $plate, $well, $direction, $rest, $read);
    foreach $file ( sort { $a cmp $b} @directory_files ) {
	if ($file =~ m/^(cC.+?c...)([0-9]{1,2})([A-Z])([0-9]{1,2})(..)/) {
	    my $library = "cLECC";
	    my ($plate, $row, $column, $rest) = ($2, $3, $4, $5);
	    $plate = int($plate);
	    $column = int($column);
	    print "$file\t$library-$plate-$row$column.$rest\n";
#	    system "mkdir -p $target/$library/$plate ; cp $directory_name/$file $target/$library/$plate/$library-$plate-$row$column.$rest.gz\n";
	}
	elsif ($file =~ m/^tomato([0-9][0-9])([0-9][0-9])([0-9][0-9])\.(.+)\.gz/) {
	    my ($cornell_plate,$sub_plate,$well,$rest) = ($1,$2,$3,$4);
	    my $library = "cLENV";
	    $cornell_plate = int($cornell_plate);
	    $sub_plate = $sub_plate - 1;
	    $well = $well - 1;
	    my $row = chr(ord('A') + int($sub_plate/2) + int($well/12)*2);
	    my $col = ($sub_plate%2) + ($well%12)*2 + 1;
	    print "$file\t$library-$cornell_plate-$row$col.$rest\n";
#	    system "mkdir -p $target/$library/$cornell_plate; cp $directory_name/$file $target/$library/$cornell_plate/$library-$cornell_plate-$row$col.$rest.gz\n";
	}
	elsif (($tigr_library, $plate, $well, $direction, $rest) = $file =~ m/(T..)(..)(..)(T[^\.])(.+)/) {
	    my ($a, $b) = split//,$plate;
	    my $plate = (ord($a) - ord('A'))*26 + (ord($b) - ord('A'));
	    my $tigr_plate = $plate % 4;
	    my $cornell_plate = int($plate/4) + 1;
	    my $well_number = (((ord($a) - ord('A'))*26 + (ord($b) - ord('A')))%4)*96 + $well;
	    my $row = chr(ord('A') + int($tigr_plate/2) + int(($well-1)/12)*2);
	    my $col = ($tigr_plate%2) + (($well-1)%12)*2 + 1;
	    
	    if ($rest !~ /^\.gz/) {
		if ($rest =~ m/(.+)\./) {
		    $read = $1;
		} else {
		    print STDERR "Unable to parse rest of id \"$rest\"\n";
		    $read = 'Z';
		}
	    } else {
		($read) = 'A';
	    }
	    if (!defined($tigr_cornell{$tigr_library})) {
		print STDERR "Tigr library id \"$tigr_library\" has no cornell translation\n";
	    } else {
		my $library = $tigr_cornell{$tigr_library};
		print "$file\t$library-$cornell_plate-$row$col.$direction.$read\n";
#		system "mkdir -p $target/$library/$cornell_plate; cp $directory_name/$file $target/$library/$cornell_plate/$library-$cornell_plate-$row$col.$direction.$read.gz\n";	    
	    }
	} else {
	    print STDERR "Skipping non-matching file $file\n";
	}
    }
    
    my $dir;
    foreach $dir ( @directory_dirs ) {
	process_directory("$directory_name/$dir");
    }
}
