#!/usr/bin/perl

use utf8;
binmode STDIN, ":utf8";
binmode STDOUT, ":utf8";
binmode STDERR, ":utf8";

print('Entrez le nom du fichier à compiler: ');
$src = <STDIN>;
chop($src);
print('Entrez le nom du fichier à créer: ');
$trgt = <STDIN>;
chop($trgt);



$cpt = 0;

open(INH,"<:encoding(UTF-8)",$src);
open(SRC,">:encoding(UTF-8)",$trgt.'.txt');
while($truc = <INH>){
   # print "====\n";
  while ($truc =~ /(.)/ig){
      #print "+\n";
      $char = $1;
      if (($char =~ /\s/)||(ord($char)==0)){
	  $char = ' ';
      }
      print SRC ($cpt . "\t" . ($cpt + 1) . "\t" . ord($char) . "\n");
      $alp{$char}=1;
      $cpt++;
  }
}

#print %alp;

print SRC $cpt . "\n";
close(SRC);

open(ALP,">:encoding(UTF-8)",$trgt.'.alp');
open(SYG,">:encoding(UTF-8)",'sygma_' . $trgt . '.txt');
print ALP "_eps\t0\n";
foreach my $key (sort(keys(%alp))){
    #print $kay . "\n";
    if ($key eq ' '){
	print ALP "_esp\t" . ord($key) . "\n";
    }else{
	print ALP $key . "\t" . ord($key) .  "\n";
    }
    print SYG "0\t1\t" . ord($key) ."\t" .ord($key) .  "\n";
}
print SYG "1\n";
close(ALP);
close(SYG);

# open(GRM,">:encoding(UTF-8)",$trgt.'.grm');
# $cpt=0;
# print GRM 'export sygma = ';
# foreach my $key (sort(keys(%alp))){
#     if ($cpt !=0){
# 	print GRM '|';
# 	if($cpt % 12 == 0){
# 	    print GRM "\n\t";
# 	}
#     }
#     if ($key eq "\""){
# 	print GRM ' "\\'.$key.'" ';
#     }else{
# 	print GRM ' "'.$key.'" ';
#     }
#     $cpt++;
# }
# print GRM ";\n\n";
# print GRM 'export id = ';
# $cpt =0;
# foreach my $key (sort(keys(%alp))){
#     if ($cpt !=0){
# 	print GRM '|';
# 	if($cpt % 8 == 0){
# 	    print GRM "\n\t";
# 	}
#     }
#     if ($key eq "\""){
# 	print GRM ' ("\\'.$key.'":"\\'.$key.'") ';
#     }else{
# 	print GRM ' ("'.$key.'":"'.$key.'") ';
#     }
#     $cpt++;
# }
# print GRM ";\n\n";
# close(GRM);

print "conversion terminée\n";

