/***************************************
*                                      *
*   Copyright (c) 1998 Jean-Eric Pin   *
*   All rights reserved.               *
*                                      *
*   TAB = 2 spaces                     *
*                                      *
***************************************/

/*-------------------------------------------------------------------
 * Main.c    Jean-Eric Pin 07/12/96
 *-------------------------------------------------------------------
 */     

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <memory.h>

#include "Main.h"
#include "Calcul.h"
#include "DclassesIteratif.h"
#include "Blocs.h"
#include "Exemples.h"
#include "FichierUnix.h"
#include "Globales.h"
#include "Initialisation.h"
#include "InitLaTeX.h"
#include "Inverses.h"
#include "Menu.h"
#include "Memoire.h"
#include "Preferences.h"
#include "Reduction.h"
#include "Sortie.h"
#include "SortieLaTeX.h"
#include "SortieDclasses.h"
#include "Syntactique.h"
#include "TriParTas.h"
#include "Utilitaires.h"
#include "Zero.h"

#ifdef PROFIL
  #include <Profiler.h> 
  OSErr error;
#endif   /* PROFIL */
  
#ifdef DEBUG
  extern long ElementsAlloues;
#endif   /* DEBUG */

clock_t clock0, clock1, delta, delta1, delta2, delta3;

extern unsigned long NbElements, NbReelElements, NbIdempotents, NbRclasses, 
       NbLclasses, NbDclasses;
extern unsigned long *TableDeHachage; 
extern unsigned long NbRelations;
extern unsigned short OptionAction, SauvegardeSouhaitee, NbLettres, 
    TypeSemigroupe, SortieLaTeX, LongueurMax, PartiePointee, FichierLaTeXOuvert;
extern EntreePartie_ EntreePartie;
extern short OptionMenu;
extern char **Messages;

char NomFichier[255] = "";
char NomFichierLaTeX[255] = "";
char CheminPref[255] = "";
char CheminExemples[255] = "";   /* Chemin figurant dans .SemigroupePrefs */
char CheminLaTeX[255] = "";       /* Chemin figurant dans .SemigroupePrefs */
char DirBABEL[255] = "";
char DirExemples[255] = "";        
char DirLaTeX[255] = "";     
char DirHome[255] = "";
FILE *fichier, *fichierLaTeX;      /* Fichiers KR p.158 */

int main(int argc, char **argv)
{
  AlloueMemoireMessages();

  strcpy(DirHome, argv[1]);
  strcpy(DirBABEL, argv[2]);
  strcpy(CheminPref, argv[1]);
#if SYSTEME == MAC || SYSTEME == UNIX
  strcat(DirBABEL, "/Babel/");
  strcat(CheminPref, "/");
#elif SYSTEME == WINDOWS
  strcat(DirHome, "\\");
  strcat(DirBABEL, "\Babel");
  strcat(CheminPref, "\\");
#endif
  strcat(CheminPref, NOM_PREF);

  LecturePreferences(CheminPref, DirBABEL);
  Baniere();
  MenuLaTeX();
  do
  {
    do
    {
      InitVariables();
      Menu();
    }
    while (OptionMenu == Prefs);
    if (OptionMenu == Prefs + 1)
      break;                  /* Sortie directe */
    if (SortieLaTeX)
    {
      ChoixNomLaTeX(); 
      fichierLaTeX = Ouverture(NomFichierLaTeX, "w");
      FichierLaTeXOuvert = 1;
      EnTeteFichierLaTeX(fichierLaTeX);
    }
    if (TypeSemigroupe == MatricesEntieres)
      SortieSemiAnneauZst();
/*     printf("***** Sortie Generateurs *******\n");  */
    if (!SortieLaTeX)
      SortieGenerateurs(); 
#ifdef DEBUG
  printf("Generators successfully computed\n");
#endif   /* DEBUG */
    clock0 = clock();
    Calcul();
     printf("*******************\n"); 
#ifdef DEBUG
  printf("Computation successfull\n");
#endif   /* DEBUG */
    clock1 = clock();
    delta1 = clock1 - clock0;
#ifndef PROFIL
    if (SauvegardeSouhaitee)
      ModificationFichier(Ouverture(NomFichier, "r+"));
#endif   /* PROFIL */
    free(TableDeHachage);  /* En principe, la table de hachage ne sert plus */
#ifndef PROFIL
    printf("%s\n", Messages[M_Computation_Dclasses]); 
    clock0 = clock();
    CalculDclasses();
    clock1 = clock();
    delta2 = clock1 - clock0;
    printf("%s\n", Messages[M_Computation_Hclasses]); 
    clock0 = clock();
#ifdef DEBUG
  printf("Elements Alloues : %ld \n", ElementsAlloues);
#endif   /* DEBUG */
    CalculHclasses();
    clock1 = clock();
    delta3 = clock1 - clock0;
    delta = delta1 + delta2 + delta3;
    printf("%s %lds  %ld/100\n", Messages[M_Time_Elements], delta1/CLOCKS_PER_SEC,
            ((delta1-(delta1/CLOCKS_PER_SEC)*CLOCKS_PER_SEC)*100)/CLOCKS_PER_SEC);
    printf("%s %lds  %ld/100\n", Messages[M_Time_Dclasses], delta2/CLOCKS_PER_SEC,
            ((delta2-(delta2/CLOCKS_PER_SEC)*CLOCKS_PER_SEC)*100)/CLOCKS_PER_SEC);
    printf("%s %lds  %ld/100\n", Messages[M_Time_Hclasses], delta3/CLOCKS_PER_SEC,
            ((delta3-(delta3/CLOCKS_PER_SEC)*CLOCKS_PER_SEC)*100)/CLOCKS_PER_SEC);
    printf("%s %lds  %ld/100\n", Messages[M_Time_Total], delta/CLOCKS_PER_SEC,
            ((delta-(delta/CLOCKS_PER_SEC)*CLOCKS_PER_SEC)*100)/CLOCKS_PER_SEC);
    printf("%s %lu\n", Messages[M_Number_of_elements], NbReelElements);
    printf("%s %lu\n", Messages[M_Number_of_relations], NbRelations);
    printf("%s %1d\n", Messages[M_Computation_over], LongueurMax); 
    SortieNbDclasses(); 
    CalculIdempotents(); 
    printf("%s %1lu\n",Messages[M_Number_of_idempotents], NbIdempotents); 
    TesteZero();
    SortieZero();
    CalculListeIdempotents();
    CalculxOmega();
    CalculReguliers();
    CalculDansUnGroupe();
    CalculBlocs(); 
    TriParTas();
    if (PartiePointee)
      EntreePartie(); 
#endif   /* PROFIL */
#ifdef DEBUG
  printf("Elements Alloues : %ld \n", ElementsAlloues);
#endif   /* DEBUG */
    OptionAction = 0;
    if (SortieLaTeX)
    {
      LongueurLaTeX();
      SortieLaTeXGenerale(fichierLaTeX); 
    }
    do
      MenuActions();
    while ((OptionAction != OptionRetour) && (OptionAction != OptionFin));  
    if (SortieLaTeX && FichierLaTeXOuvert) 
    {
      FinFichierLaTeX(fichierLaTeX); 
      Fermeture(fichierLaTeX); 
      FichierLaTeXOuvert = 0;
    }
  }
  while (OptionAction != OptionFin);
  printf("%s\n", Messages[M_TheEnd]);
/*  for (i = 0; i < M_Last_Line; i++)
    free(Messages[i]); */
  return(0);  
}