#*********************************************************************************************
#                                 Fait par JGP SOLDAT
#                               Maintenu par Kana-chan
#                             Pour l'Alliance Francophone
#                             Version 4.00, le 21 mai 2007  
#                           Première version 27 janvier 2001
#
#             (Programme d'insersion des données des meilleurs producteurs)
#
#*********************************************************************************************

use Socket;
use DBI;

$test = 0;
$max_users = 100;

# Parametres de connection a la base de donnees
$bd    = '/tmp/memory/fah.db';
$identifiant = '';      # identifiant
$motdepasse  = '';

# Connection a la base de donnees mysql
print("Connexion a la base de donnees $bd\n");
$db = DBI->connect(
        "DBI:SQLite:dbname=$bd",
        $identifiant, $motdepasse, {RaiseError => 1}
      ) or die "Connection impossible a la base de donnees $bd !\n $! \n $@\n$DBI::errstr";

open(file, "pos2.res"); $b=<file>; close (file);
$b++;
open(file, ">pos2.res"); print file ($b); close (file);

print("Recherche dans DATA/daily_user_summary.txt\n\n");
open(file, "DATA/daily_user_summary.txt");
$temp="";
$line=<file>;
$cpt=0;
while ($line ne "") {
  if ($cpt != 0 and $cpt != 1 and $cpt <= $max_users+2) {
    $temp .= $line;
  }
  $line = <file>;
  $cpt += 1;
}
close (file);
unlink("DATA/daily_user_summary.txt");

@separe = split(/\n/,$temp);
$a=0;
$rec=$b;
$donner = "pos_general, name, credit, total, team, rec";
while ($separe[$a] ne "" and $a < $max_users) {
  @temp = split (/\t/, $separe[$a]);
  if ($temp[1] != " ") {
    $pos = $a+1;
    $temp1 = $temp[0];
    $temp1 =~ s/ //g;
    $name = $temp1;
    $name =~ s/'/&#39;/g;
    $temp1 = $temp[1];
    $temp1 =~ s/ //g;
    $credit = int($temp1);
    $temp1 = $temp[2];
    $temp1 =~ s/ //g;
    $wu= int($temp1);
    $temp1 = $temp[3];
    $temp1 =~ s/ //g;
    $team = "T".$temp1;
    $valeur = "'".$pos."', '".$name."', '".$credit."', '".$wu."', '".$team."', '".$rec."'";
    
    if ($test == 1) { print("INSERT INTO meilleur (".$donner.") VALUES (".$valeur.")\n"); }
    $do = $db->prepare("INSERT INTO meilleur (".$donner.") VALUES (".$valeur.")");
    $ErrNum = $do->execute() or die $DBI::errstr;
    if ($ErrNum < 0) {
         print ($ErrNum."  ".$DBI::errstr."\n\n");
    }
    #$do = $db->prepare("SELECT count(*) FROM meilleur WHERE name='".$name."'");
    #$do->execute() or die $ErrNum = 1;
    #$foundname = 0;
    #if (my $value = $do->fetchrow) {
    #if ($value != 0) {
    #  $foundname = 1;
    #  }
    #}
    #$do->finish();
	#if ($foundname == 0) {
	#  print("\nName [".$name."] cannot be saved in database!\n");
	#}
    if ($test == 1) { 
      print("INSERT INTO meilleur (".$donner.") VALUES (".$valeur.")\n");
      print("==>".$pos." ".$name." ".$credit. " ".$wu." ".$team." ".$rec."\n");
    }
  }  else  {
    print("\nPosition [".($a+1)."] = ".$separe[$a]."\n");
  }
  $a++;
}

$db->disconnect();

print("\nFin de recherche dans DATA/daily_user_summary.txt\n\n");
exit;
1;
