/***************************************
*                                      *
*   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 "Exemples.h"
#include "FichierUnix.h"
#include "Globales.h"
#include "Initialisation.h"
#include "Interface.h"
#include "Inverses.h"
#include "Menu.h"
#include "Memoire.h"
#include "Preferences.h"
#include "Reduction.h"
#include "Sortie.h"
#include "SortieDclasses.h"
#include "Syntactique.h"
#include "Utilitaires.h"
#include "Zero.h"

#ifdef PROFIL
  #include <Profiler.h> 
  OSErr error;
#endif   /* PROFIL */

#if  SYSTEME == MAC
#define __MWERKS__ 
  #include "Fonts.h"
  #include <SIOUX.h>
  #include <console.h>
  #include <LowMem.h>
#endif

#ifdef __MWERKS__
  #include "Events.h"     /* TickCount */
  unsigned long  tickCount, tickCountEnd1, tickCountEnd2, TicTac1, TicTac2;
#endif
  clock_t clock0, clock1, clock2, delta1, delta2;

extern numero NbElements, NbReelElements, NbIdempotents, NbRclasses, NbLclasses, NbDclasses;
extern numero *TableDeHachage; 
extern unsigned long NbRelations;
extern unsigned short PossedeUnNeutre, OptionAction, SauvegardeSouhaitee, NbLettres,
                      TypeSemigroupe, MemoireAllouee;

#if SYSTEME == MAC
  long total, contigu;
#endif

extern unsigned short LongueurMax, PartiePointee;
extern EntreePartie_ EntreePartie;
extern short OptionMenu;
extern char **Messages;
char NomExemple[255] = "";
char CheminPref[255] = "";
char DirBABEL[255] = "";
char CheminExemples[255] = "";   /* Chemin figurant dans .SemigroupePrefs */
char DirExemples[255] = "";     
char DirHome[255] = "";

int main(int argc, char **argv)
{
#if SYSTEME == MAC
  SIOUXSettings.columns = 120;    /* The initial size of the SIOUX window (default 80) */
  SIOUXSettings.rows = 40;        /* The initial size of the SIOUX window (default 24) */
  SIOUXSettings.toppixel = 0;      /* The topleft window position (in pixels) (default 0) */
  SIOUXSettings.leftpixel = 0;    /* (0,0 centers on main screen) ... */
  SIOUXSettings.fontsize = 12;    /* SIOUX's fontsize (default 9) */
  SIOUXSettings.wasteusetempmemory = 1;
  SIOUXSettings.autocloseonquit = 0;
#endif

  AlloueMemoireMessages();

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


  LecturePreferences(CheminPref, DirBABEL);
  Baniere();
#if SYSTEME == MAC
  PurgeSpace(&total, &contigu);
#endif
  do
  {
    do
    {
      InitVariables();
      Menu();
    }
    while (OptionMenu == Prefs);
    if (OptionMenu == Prefs + 1)
      break;                  /* Sortie directe */
    if (TypeSemigroupe == MatricesEntieres)
      SortieSemiAnneauZst();
    SortieGenerateurs(); 
    clock0 = clock();
    Calcul();
    clock1 = clock();
    delta1 = clock1 - clock0;
#ifndef PROFIL
    if (SauvegardeSouhaitee)
      ModificationFichier(Ouverture(NomExemple, "r+"));
#endif   /* PROFIL */
    free(TableDeHachage);  /* En principe, la table de hachage ne sert plus */
#ifndef PROFIL
    printf("%s\n", Messages[M_Computation_Dclasses]); 
    CalculDclasses();
    clock2 = clock();
    delta2 = clock2 - clock0;
    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_Total], delta2/CLOCKS_PER_SEC,
            ((delta2-(delta2/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(); 
    if (PartiePointee)
      EntreePartie();
#endif   /* PROFIL */
#ifdef DEBUG
  printf("Elements Alloues : %ld \n", ElementsAlloues);
#endif   /* DEBUG */
    OptionAction = 0;
    do
      MenuActions();
    while ((OptionAction != OptionRetour) && (OptionAction != OptionFin));  
  }
  while (OptionAction != OptionFin);
  printf("%s\n", Messages[M_TheEnd]);
  return(0);  
}