#!/usr/bin/perl

$directory=$ARGV[0];
print"$directory\n";
  my @file_name = ();
  opendir (my $dir_fn, $directory)
  or die "impossible to open directory $directory\n";
  my @file_dir = grep { !/^\.\.?$/ } readdir($dir_fn);
  closedir ($dir_fn);
  
foreach my $name (@file_dir) {
    if ( -f "$directory/$name" ) {
# print"liste :$name\n";
       $file_ext[1]="";
       @file_ext=split(/\./,$name);
       if ($file_ext[1] eq "fits"){
#       print "$file_ext[0], $file_ext[1]\n";
        $input_file = "$directory/$file_ext[0]".".fits";
	$input_md5 = "$directory/$file_ext[0]".".md5";
print "$input_file ";
        system "digest -a md5 $input_file > toto.md5";
        open(FH1,"<toto.md5") or die "impossible to open toto.md5\n";
        open(FH2,"<$file_ext[0]".".md5")  or die "impossible to open $file_ext[0].md5\n";
        while(<FH1>){push(@line1,$_)};
        while(<FH2>){push(@line2,$_)};
        $val_md5_init=$line2[0];
        @valeur=split(/ /,$val_md5_init);
        $val_md5_calc=$line1[0]; 
        $val_md5_calc =~ s/\n//; 
        $val_md5_init=$valeur[0]; 
#        print"$valeur[0]:::$valeur[1]\n";
         print "$val_md5_calc,$val_md5_init\n";
        if ($val_md5_calc ne $val_md5_init) {
            print "probleme MD5 sur $directory/$name \n";
#            print "$val_md5_calc,$val_md5_init\n";
            }
        close(FH1);
        close(FH2);
     }
   }
}
