/*---------------------------------------------------------------------------------------------------------------------
- File      : checker.cc                                                                  Project ELSE, EPFL - DI/LIA -
-                                                                       Evaluation in Language and Speech Engineering -
- Author    : Seydoux Florian   Creation date : 07 July 1999                                                          -
- Eulogist  : -                 Approval date : -                  Version: 0.1                                       -
-                                                                                                                     -
- Descript. : Vérification stricte du format des fichiers suivants                                                    -
-             1) Fichier de configuration:
-                 a)  Format du fichier
-                 b)  Conformité avec les restrictions imposées aux séparateurs
-                 c)  Existence des fichier étiquetés                                                                 -
-             2) Table de correspondance:
-                 a)  Format du fichier
-                 b)  Format des étiquettes systèmes
-                 c)  Définition unique des étiquettes systèmes
-                 d)  Validité des étiquettes de références
-             3) Fichiers étiquetés:
-                 a)  Format du fichier
-                 b)  Existence des étiquettes
-                 c)  Warning: test de la conformité du flux des tokens
-
- Requested : -                                                                                                       -
-                                                                                                                     -
- Gaps      :                                                                                                         -
-                                                                                                                     -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Rev. date | Reviser               | Revise's description                                                            -
- - - - - - + - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ../../....| ........              | ...                                                                             -
---------------------------------------------------------------------------------------------------------------------*/

//
// Version actuelle: 'mono-fichier'
// (Pour la version 'final' utilisant la liste dans le fich. de configuration, il faudra prevoir
// un moyen de donner les corpus d'origine (a inclure dans le fichier de configuration))
// 

#ifndef CHECKER
#define CHECKER

#include <iostream>

#include "grace_tools.h"
#include "tags_mapping_table.h"
#include "messages_manager.h"
#include "chk_tagged_corpus.h"

#ifdef _USE_NAMESPACES
    namespace Else { 
#endif // _USE_NAMESPACES

void dumpHelp();
void checkParams(Parameters&);
int main(int argc, char* argv[]);

conform_string config, refconfig, prefix, corpus, tagged;
bool checkTable, checkCorpus;

void dumpHelp()
{
    cerr << "Description: (User/System part)\n"
         << "  Use 'check' to verify the conformance of all system's file (Config, MapTable, TaggedCorpus).\n"
         << "  Errors, warnings and some other message are displaying to the standard output (by default).\n"
         << "  Only the systems for which 'test' does not produce an error (warnings are accepted) are\n"
         << "  allowed for the evaluation.\n"
         << "Usage: check <user config> [<ref config>] {switches}\n"
         << "\n"
         << "  <user config>  : The configuration file of the system.\n"
         << "  <ref  config>  : Configuration of reference's files (default: grace.cfg).\n"
         << "\n"
         << "Switches:\n"
         << "  -prefix prefix : <prefix> is used to prefix ALL file's name (incl. config).\n"
         << "  -corpus Name   : set <corpus> to Name, default: 'corpus'.\n"
         << "  -rc fileName   : original raw corpus, default: '<prefix><corpus>.rcrp'.\n"
         << "  -tc fileName   : tagged corpus, default: '<prefix><corpus>.tcrp'.\n"
         << "  -chkcfg        : check only the configuration file.\n"
         << "  -chktbl        : check the config. file and the mapping table.\n"
         << "  -chkall        : check all (default).\n"
         << "\n";
    msg.switchesFormat(cerr);
    cerr << "\n\n";
    exit (0);
}

void checkParams(Parameters& params)
{
    conform_string _corpus, _output, _rawcrp, _tagged;

    if ( (params.empty()) ||
         !(strcmp(params.front(), "?"))  || !(strcmp(params.front(), "-?")) || 
         !(strcmp(params.front(), "-h")) || !(strcmp(params.front(), "-help")) || !(strcmp(params.front(), "--help")))
        dumpHelp();

    _corpus = "corpus";
    checkTable = checkCorpus = true;
    refconfig = "grace.cfg";
    prefix.clear();

    config  = params.front(); params.erase(params.begin());
    if (!params.empty() && (params.front()[0] != '-'))
        refconfig = params.front(); params.erase(params.begin());

    msg.checkParams(params);

    for (Parameters::const_iterator currentArg = params.begin(); currentArg != params.end(); ++currentArg) {
        if (!strcmp(*currentArg, "-prefix")) { if (++currentArg != params.end()) prefix = *currentArg; continue; }
        if (!strcmp(*currentArg, "-corpus")) { if (++currentArg != params.end()) _corpus = *currentArg; continue; }
        if (!strcmp(*currentArg, "-rc")) { if (++currentArg != params.end()) _rawcrp  = *currentArg; continue; }
        if (!strcmp(*currentArg, "-tc")) { if (++currentArg != params.end()) _tagged  = *currentArg; continue; }
        if (!strcmp(*currentArg, "-chkcfg")) { checkTable = checkCorpus = false; continue; }
        if (!strcmp(*currentArg, "-chktbl")) { checkTable = !(checkCorpus = false); continue; }
        if (!strcmp(*currentArg, "-chkall")) { checkTable = checkCorpus = true; continue; }
        cerr << "Invalid argument: " << *currentArg << "\nTry 'check --help' for more informations!\n";
    }

    // Read default values...
    config.insert(0, prefix);
    refconfig.insert(0, prefix);
    corpus  = prefix + (_rawcrp.empty()  ? _corpus + string(".rcrp") : _rawcrp);
    tagged  = prefix + (_tagged.empty()  ? _corpus + string(".tcrp") : _tagged);
}


int main(int argc, char* argv[]) {

    Parameters params(argc, argv);
    checkParams(params);    

    UserConfiguration  userConfiguration, refConfiguration;
    TagsMappingTable   translationTable;

    if (!userConfiguration.readFrom(config, UserConfiguration::System, true))
        msg.seriousError(string("Checker"), "User Configuration file is unreadeable, or it has too serious error(s).");
    if (!refConfiguration.readFrom(refconfig, UserConfiguration::Reference, true))
        msg.seriousError(string("Checker"), "Reference Configuration file is unreadeable, or it has too serious error(s).");
    if (!checkTable) exit(0);
    translationTable.readReference(refConfiguration, prefix+refConfiguration.mapName, refConfiguration.mapMode);
    translationTable.readSystem(userConfiguration, prefix+userConfiguration.mapName, userConfiguration.mapMode);
    if (!checkCorpus) exit(0);

    CheckTaggedCorpus corpusChecker(userConfiguration, translationTable);
    corpusChecker.checkSystem(corpus, tagged);
    exit(0);
}

#ifdef __USE_NAMESPACES
}
#endif // _USE_NAMESPACES

#endif // defined UNTAG_MAIN

