#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use open qw/:std :utf8/;

use Encode;
use Getopt::Long;

my ($programme) = $0 =~ m|^(?:.*/)?(.+)|;
$programme = decode_utf8($programme);

my $source = ""; #par exemple "Vieil_v2.source"
my $dir    = ""; #par exemple "Vv2_new"

eval	{
	$SIG{__WARN__} = sub {usage(1);};
		GetOptions(
		"source=s"      => \$source,
		"dir=s"         => \$dir,
		);
	};
$SIG{__WARN__} = sub {warn $_[0];};

usage(2) if not $dir or not $source;

open(SOURCE, "<:encoding(UTF-8)", $source) 
	or die "Couldn't open file $source, $!";

chomp(my @sourcefile = <SOURCE>);
my $size = @sourcefile;
print STDERR $size;
close SOURCE;

my %correspondance;
for(my $n=0 ; $n<= $#sourcefile ; $n++ )
{
	$sourcefile[$n] =~ /(\w{40})\W*?(\w+)/;
	$correspondance{$1} = $2;
}

#print "$_ $correspondance{$_}\n" for (keys %correspondance);#imprimer une table d'hachage

my $size_cles = keys %correspondance;
print STDERR $size_cles;

opendir(DIR, $dir) or die "Couldn't open the directory $dir, $!";
my @files = grep {$_ ne '.' and $_ ne '..'} readdir(DIR);
closedir(DIR);

foreach my $file (@files){
	my $new = $file;
	$new =~ s/(\w{40}).txt/$1/;
	if ($correspondance{"$new"})
	{
		$new = $correspondance{"$new"};
		rename("$dir/$file","$dir/$new.txt");
		print "$file => $new.txt\n";
	}
	else
	{
		print "Pas de correspondance pour le fichier $file\n";
	}
}

exit 0;

sub usage
{
my $code = shift;

print STDERR "Usage : $programme -s source -d dir\n";

exit $code;
}
