// $Source: /usr/local/SimSoft/Sfx3D/src/gsl/SfxNum.cpp,v $
// $Revision: 1.2 $
// $Author: baum $
// $Date: 2001/11/29 15:18:09 $
//
// Login name of the user who locked the revision
// $Locker:  $
// Tag name used to checkout the file
// $Name: Release-14 $
// State assigned to revision
// $State: Exp $
//
//
// $Log: SfxNum.cpp,v $
// Revision 1.2  2001/11/29 15:18:09  baum
// -Realzeitsteuerung eingebaut
// -Handsteuerung eingebaut
//

#include <stdio.h>
#include <stdlib.h>

#include "Messager.h"
#include "StrmSet.h"
#include "IniFile.h"
#include "ProgArgs.h"
#include "SimTime.h"
#include "FileSys.h"
#include "Kalender.h"

#include "modell.h"
#include "sfxprg.h"
#include "programm.h"
#include "ereignis.h"

void showUsage () {
  cout << "Benutzung:\tGslNum [-Optionen] Pfad\\Modellname" << endl;
  cout << "Optionen:" << endl;
  cout << "\t-trace\t\t\t\tEreignisprotokoll" << endl;
  cout << "\t-zeittrace <Zeit in min>\tEreignisprotokoll ab <Zeit>" << endl;
  cout << "\t-ausgabe <Pfad relativ>\t\tAusgabe in <Pfad>" << endl;
  exit (1);
}  

int main (int argc, char **argv) {
  bool withTrace = false;

  ProgArgs::setup (argc, argv);
  if (!StdStreamControl::isInitialized ()) StdStreamControl::setup ();
  if (!Messager::isInitialized ()) Messager::setup ();
  if (!FileSystem::isInitialized ()) FileSystem::setup ();                    

  cout << endl << "SIMFLEX/3D - Numerischer Simulator" << endl << endl;

  if (argc < 2) showUsage ();

  char modname[100];
  strcpy (modname, argv[argc - 1]);
  strcat (modname, ".2gs");
  if (FileSystem::FileExists (modname))
    cout << "\nStarte Modell: <" << argv[argc - 1] << ">" << endl;
  else {
    cout << "\nModell <" << modname << "> existiert nicht !!\n" << endl;
    exit (1);
  }

  if (ProgArgs::OptionSet ("-trace")) {
      cout <<  "mit Option -trace ";
      withTrace = true;
  }

  MySFXModel = new SFXModel (argv[argc - 1], false, false);
  MySFXModel->setCycle (false);
  MySFXModel->setTrace (withTrace);
  modelOpen = true;
  notiz::setup ();

  const char *optstr = ProgArgs::OptionString ("-zeittrace");
  double zeit = 0.0;
  if (optstr) {
    if (sscanf (optstr, "%lf", &zeit) != 0) {
      SimTime t (zeit * SimTime::SecondsPerMinute ());
      cout << "mit Option -zeittrace nach " << t.inMinutes ()<< " min" << endl;
      planeEin (t, ETatrace);
    }
    else showUsage ();
  }

  const char *optausg = ProgArgs::OptionString ("-ausgabe");
  char ausgabe[255];
  if (optausg) {
    if (sscanf (optausg, "%s", &ausgabe) != NULL) {
      Ustring ausg (ausgabe);
      cout << "mit Option -ausgabe in Verzeichnis " << ausgabe << endl;
      MySFXModel->setOutputPath (ausg);
    }
    else
    	showUsage ();
  }
  
  ExeContxt::setup ();

  Messager::setMsgHdlFunc (MessagerToCout);

  if (IniFile::DefIniFile.readInt ("FFS", "Output_in_Datei", 0))
    coutController.setToOstream (clog);

  char c;
  PrgModul = new typPrgModul ((MySFXModel->ModelPath () + 
                               MySFXModel->ModelName ()).str (), &c);

  MySFXModel->load ();
  MySFXModel->TGBLibrary ()->DontBuildNumbers ();

  while (waslos () && modelOpen) {
    E_Steuerung ();
  }
  
  delete MySFXModel;
  delete PrgModul;
  PrgModul = NULL;
  ExeContxt::cleanup ();
  notiz::cleanup ();
  if (FileSystem::isInitialized ()) FileSystem::cleanup ();
  if (Messager::isInitialized ()) Messager::cleanup ();
  if (StdStreamControl::isInitialized ()) StdStreamControl::cleanup ();
  ProgArgs::cleanup ();
  return 0;
}
 
