#*********************************************************************************************
#                                 Fait par JGP SOLDAT
#                               Maintenu par Kana-chan
#                             Pour l'Alliance Francophone
#                             Version 4.00, le 21 mai 2007 
#                             Première version avril 2007
#
#                     (Programme de purge et transfert des données)
#
#*********************************************************************************************

use Socket;
use DBI;

# Parametres de connection a la base de donnees
$bd    = '/opt/fah/folding/saved_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";

$test1=0;
$test2=0;
$test3=0;

$nb_jours = 11*7;

open(file, "pos.res");
$a=<file>;
close (file);

$a=$a-($nb_jours+1);
$rec=0;
$compte=$a-10;
$ErrNum = 0;
while ($compte < $a and $rec < 1) {
  $compte++;
  if ($test1 != 0) { print("SELECT count(rec) FROM Alliance WHERE rec = ".$compte." ==> "); }
  $rec = $db->selectrow_array("SELECT count(rec) FROM Alliance WHERE rec = ".$compte);
  if ($test1 != 0) { print($rec."\n"); }
  if ($rec < 0) {
    $rec = 0;
  }
}

$a1=$compte+10;
if ($a1 > $a) {
  $a1 = $a;
}

if ($compte < $a1) {
  print "\nPurge de la table Alliance des positions ".$compte." a ".($a1-1)."\n";
  sleep(5);
}
$donner = "pos_Alliance, pos_general, nom, score, wu, rec, cpu";
while ($compte < $a1) {
  if ($test1 != 0) { print("DELETE FROM Alliance WHERE rec = ".$compte."\n"); }
  if ($test1 == 0) {
    $do = $db->prepare("DELETE FROM Alliance WHERE rec = ".$compte);
    $ErrNum = $do->execute() or die $DBI::errstr;
    unlink("message\\message-".$compte.".txt");
  }
  $compte++;
}
#***************************************************************************************
#***************************************************************************************
#***************************************************************************************
open(file, "pos1.res");
$b=<file>;
close (file);

$b=$b-($nb_jours+1);
$rec=0;
$compte=$b-10;
while ($compte < $b and $rec < 1) {
  $compte++;
  if ($test2 != 0) { print("SELECT count(rec) FROM Alliance WHERE rec = ".$compte." ==> "); }
  $rec = $db->selectrow_array("SELECT count(rec) FROM equipe WHERE rec = ".$compte);
  if ($test2 != 0) { print($rec."\n"); }
  if ($rec < 0) {
    $rec = 0;
  }
}
$b1=$compte+10;
if ($b1 > $b) {
  $b1 = $b;
}

if ($compte < $b1) {
  print "\nPurge de la table equipe des positions ".$compte." a ".($b1-1)."\n";
  sleep(5);
}

$donner = "pos_general, numeroteam, score, wu, rec";
while ($compte < $b1) {
  if ($test2 != 0) { print("DELETE FROM Alliance WHERE rec = ".$compte."\n"); }
  if ($test2 == 0) {
    $do = $db->prepare("DELETE FROM equipe WHERE rec = ".$compte);
    $ErrNum = $do->execute() or die $DBI::errstr;
  }
  $compte++;
}
#***************************************************************************************
#***************************************************************************************
#***************************************************************************************
open(file, "pos2.res");
$c=<file>;
close (file);

$c=$c-($nb_jours+1);
$rec=0;
$compte=$c-10;
while ($compte < $c and $rec < 1) {
  $compte++;
  if ($test3 != 0) { print("SELECT count(rec) FROM Alliance WHERE rec = ".$compte." == > "); }
  $rec = $db->selectrow_array("SELECT count(rec) FROM meilleur WHERE rec = ".$compte);
  if ($test3 != 0) { print($rec."\n"); }
  if ($rec < 0) {
    $rec = 0;
  }
}

$c1=$compte+10;
if ($c1 > $c) {
  $c1 = $c;
}

if ($compte < $c1) {
  print "\Purge de la table meilleur des positions ".$compte." a ".($c1-1)."\n";
  sleep(5);
}

$donner = "pos_general, name, credit, total, team, rec";
while ($compte < $c1) {
  if ($test3 != 0) { print("DELETE FROM Alliance WHERE rec = ".$compte."\n"); }
  if ($test3 == 0) {
    $do = $db->prepare("DELETE FROM meilleur WHERE rec = ".$compte);
    $ErrNum = $do->execute() or die $DBI::errstr;
  }
  $compte++;
}

$db->disconnect();

print("Fin du traitement\n");
exit;
1;
