// $Source: /usr/local/SimSoft/Sfx3D/src/gts/gtsini.cpp,v $
// $Revision: 1.2 $
// $Author: baum $
// $Date: 2001/11/29 15:18:10 $
//
// 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: gtsini.cpp,v $
// Revision 1.2  2001/11/29 15:18:10  baum
// -Realzeitsteuerung eingebaut
// -Handsteuerung eingebaut
//

/*** Windows headers ***/
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
//#include <winnt.h>
#endif /*WIN32*/

#include "IniFile.h"
#include "StrmSet.h"
#include "FileSys.h"

#include "gtsio.h"
#include "gtsglob.h"
#include "gtsgraf.h"
#include "gtsini.h"
#include "gtstgb.h"
#include "gtspal.h"
#include "gtstfk.h"
#include "gtstzt.h"
#include "gtstlm.h"
#include "gtstas.h"

#ifdef WIN32
#include <shellapi.h>

void CoutToDebugWindow (char* txt) {
  static HWND DBWin32Window = 0;
  if (!DBWin32Window)
    DBWin32Window = (HWND) GetProp (GetDesktopWindow (), "DBWin32-Handle");
  SendMessage (DBWin32Window, WM_SETTEXT, 0, (unsigned) txt);
}
#endif

void GTSInit ()
{
//  Ustring cdir (FileSystem::CurrentDir());
//  Meld.Ausgabe(cdir);
  Ustring FileName (FileSystem::ExePath());
  FileName += "Gts.ini";
  FileSystem::switchPathForOS (FileName.str());
  if (!FileSystem::FileExists (FileName.str ()))
     Msg.Error ("GtsInit()", "Datei >%s< nicht gefunden", FileName.str());
  IniFile::setup (FileName.str());

  BausteinListe    = new typBausteinListe      ();
  FahrkursListe    = new typFahrkursListe      ();
  EreignisListe    = new EreignisVerwaltung    ();
  ProzessListe     = new ProzessVerwaltung     ();
  ArbeitsplanListe = new ArbeitsplanVerwaltung ();
  Messager::setMsgHdlFunc (GTSMsgHandleFunc);

  if (IniFile::DefIniFile.readInt ("GTS", "Output_in_Datei", 0))
    coutController.setToOstream (clog);
  else
#ifdef WIN32
    if (IniFile::DefIniFile.readInt ("GTS", "dprintf_DEBUG", 0))
    {
      coutController.setToCallback (CoutToDebugWindow);
      ShellExecute (0, NULL, "DBWin.exe", NULL, NULL, SW_HIDE);
      ShellExecute (0, NULL, "DBWin32.exe", NULL, NULL, SW_HIDE);
    }
#else
    if (IniFile::DefIniFile.readInt ("GTS", "dprintf_DEBUG", 0))
       coutController.setToDefault ();
#endif

  initprog = IniFile::DefIniFile.readInt ("GTS", "Initprog", 0) ? true : false;
  testprog = IniFile::DefIniFile.readInt ("GTS", "Testprog", 0) ? true : false;

  Ustring exePath (FileSystem::ExePath ());
 //---rudi---20.03.2002
  // Dem exePath wird das Verzeichnis zugewiesen, das in der
  // Systemvariable SFX3D zugewiesen ist
  // 255 ist beliebig problematisch!!!!
#ifdef WIN32
  char mybuf [MAX_PATH];
#else
  char mybuf [255];
#endif

  char* p = getenv ("SFX3D_VC6");
  if (p != NULL) {
  	exePath = Ustring (p) + Ustring ("\\");
  	sprintf(mybuf, "Sfx3D-Verzeichnis: %s\n",exePath.str());
   Meld.Ausgabe(mybuf);
  }
  //---rudi---20.03.2002


  BausteinBibliothek = IniFile::DefIniFile.readString ("GTS", "PermTGBFile",
                        (exePath + "../tgb/tgb_tgb.3bb").str ());
  PalettenBibliothek = IniFile::DefIniFile.readString ("GTS", "TempTGBFile",
                        (exePath + "../tgb/tgb_tgb.3pp").str ());
  Ustring TexturesPath = IniFile::DefIniFile.readString ("GTS", "TexturePath",
                         "../Textures/");
  Ustring VrmlPath = IniFile::DefIniFile.readString ("GTS", "VRMLPath",
                         "../tgb/vrml/");

  FileSystem::switchPathForOS (BausteinBibliothek.str());
  FileSystem::switchPathForOS (PalettenBibliothek.str());
  FileSystem::switchPathForOS (TexturesPath.str ());
  FileSystem::switchPathForOS (VrmlPath.str ());

  char c = (TexturesPath.str ()) [TexturesPath.len () - 1];
  if (c != FileSystem::PathSeparator ())
     TexturesPath += FileSystem::PathSeparator ();
  if ((TexturesPath.str ()) [0] != FileSystem::PathSeparator ()
      && (TexturesPath.str ()) [1] != ':')
    TexturesPath = exePath + TexturesPath;

  c = (VrmlPath.str ()) [VrmlPath.len () - 1];
  if (c != FileSystem::PathSeparator ())
     VrmlPath += FileSystem::PathSeparator ();
  if ((VrmlPath.str ()) [0] != FileSystem::PathSeparator ()
      && (VrmlPath.str ()) [1] != ':')
    VrmlPath = exePath + VrmlPath;

  if (!FileSystem::FileExists (PalettenBibliothek.str ()))
     Msg.Error ("LeseGTSIniDatei()",
                "Palettenbibliothek >%s< nicht gefunden", 
                PalettenBibliothek.str());

  if (!FileSystem::FileExists (BausteinBibliothek.str ()))
     Msg.Error ("LeseGTSIniDatei()",
                "Bausteinbibliothek >%s< nicht gefunden", 
                BausteinBibliothek.str());

  if (initprog)
  {
    Write ("\nPermTGBFile= %s\n", BausteinBibliothek.str());
    Write ("TempTGBFile= %s\n",   PalettenBibliothek.str());
    Write ("TexturesPath= %s\n",  TexturesPath.str());
    Write ("VRMLPath= %s\n",      VrmlPath.str());
  }

  TgbBibliothek    = new typBibliothek (BausteinBibliothek);
  PalBibliothek    = new Bibliothek (PalettenBibliothek);
  TgbBibliothek->setTexturesPath (TexturesPath);
  TgbBibliothek->setVrmlPath     (VrmlPath);

  double movestep = IniFile::DefIniFile.readDouble ("GTS", "MoveStep", 0);
  double anglestep = IniFile::DefIniFile.readUnsigned ("GTS", "AngleStep", 0);
  GTSCB.setMoveStep(movestep);
  GTSCB.setAngleStep(anglestep);
  InitGrafik ();
  IniFile::cleanup ();
}  // ENDE GTSInit

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void GTSExit ()
{
   GTSSpace->MySelection()->removeAll ();
   if (TgbBibliothek)    delete TgbBibliothek;
   if (PalBibliothek)    delete PalBibliothek;
   if (BausteinListe)    delete BausteinListe;
   if (FahrkursListe)    delete FahrkursListe;
   if (EreignisListe)    delete EreignisListe;
   if (ProzessListe)     delete ProzessListe;
   if (ArbeitsplanListe) delete ArbeitsplanListe;
   coutController.setToDefault();
   ExitGrafik ();
}  // ENDE GTSExit

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SchreibeModellIniDatei () {

#ifdef WIN32
  char mybuf [MAX_PATH];
#else
  char mybuf [255];
#endif
  Ustring fileName (ExpName + ".ini");
  if (!FileSystem::FileExists (fileName.str ()) || (ModellRelease == 0)) {
    Ustring sourceFileName;
    if ((ModellIniDatei.len() == 0) || (ModellRelease == 0)) {

      sprintf (mybuf, "ModellRelease = %5.2f\n", ModellRelease);
      Meld.Ausgabe (mybuf);
      sourceFileName = FileSystem::ExePath ();
      sourceFileName += "../template/modell.ini";
    }
    else
      sourceFileName = ModellIniDatei;
    FileSystem::switchPathForOS (sourceFileName.str ());

    if (FileSystem::FileExists (sourceFileName.str ()))
    {
      ifstream i (sourceFileName.str ());
      ofstream o (fileName.str ());
      o << i.rdbuf ();
      o.close ();
      i.close ();
    }
    else
    {
      Msg.Warning ("SchreibeModellIniDatei()",
                   "Template >%s< nicht gefunden", sourceFileName.str());
      return;
    }
  }
  IniFile::setup (fileName.str ());

  IniFile::DefIniFile.writeString ("TGB", "PermTGBFile",
                      BausteinBibliothek.str());
  IniFile::DefIniFile.writeString ("TGB", "TempTGBFile",
                      PalettenBibliothek.str());
  IniFile::cleanup ();
}  // ENDE SchreibeModellIniDatei

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool PruefeModellIniDatei(Ustring modellname)
{
  Ustring modellpfad (modellname);
  Ustring TGBmitPfad;
  Ustring PALmitPfad;
  ModellIniDatei = "";

  FileSystem::switchPathForOS (modellpfad.str());
  int stelle = modellpfad.findRightPos(FileSystem::PathSeparator ());
  if (stelle > 0)
     modellpfad.delPart(stelle + 1, modellpfad.len() - (stelle + 1));

  Ustring FileName = modellname + ".ini";
  FileSystem::switchPathForOS (FileName.str());
  if (!FileSystem::FileExists (FileName.str ()))
    return true;

  IniFile::setup (FileName.str());
  ModellIniDatei = FileName;

  /**< Releaseversion des Modells wird eingelesen.
       Modelle ab Release 15.0 werden anders eingelesen!!!*/
  ModellRelease = IniFile::DefIniFile.readDouble ("ALLGEMEIN", "Release", 0);     
  Ustring exePath (FileSystem::ExePath ());
  Ustring BstTgb = IniFile::DefIniFile.readString ("TGB", "PermTGBFile",
                            (exePath + "../tgb/tgb_tgb.3bb").str ());
  Ustring PalTgb = IniFile::DefIniFile.readString ("TGB", "TempTGBFile",
                            (exePath + "../tgb/tgb_tgb.3bb").str ());
  Ustring TexturesPath = IniFile::DefIniFile.readString ("TGB", "TexturePath",
                         (exePath + "../Textures/").str());
  Ustring VrmlPath = IniFile::DefIniFile.readString ("TGB", "VRMLPath",
                         (exePath + "../tgb/vrml/").str());
  IniFile::cleanup ();

  FileSystem::switchPathForOS (BstTgb.str());
  FileSystem::switchPathForOS (PalTgb.str());
  FileSystem::switchPathForOS (TexturesPath.str ());
  FileSystem::switchPathForOS (VrmlPath.str ());

  char c = (TexturesPath.str ()) [TexturesPath.len ()- 1];

  if (c != FileSystem::PathSeparator ())
     TexturesPath += FileSystem::PathSeparator ();
  
  if ((TexturesPath.str ()) [0] != FileSystem::PathSeparator ()
      && (TexturesPath.str ()) [1] != ':')
     TexturesPath = exePath + TexturesPath;

  c = (VrmlPath.str ()) [VrmlPath.len ()- 1];

  if (c != FileSystem::PathSeparator ())
     VrmlPath += FileSystem::PathSeparator ();
  
  if ((VrmlPath.str ()) [0] != FileSystem::PathSeparator ()
      && (VrmlPath.str ()) [1] != ':')
     VrmlPath = exePath + VrmlPath;
  
  if ((BstTgb.str ()) [0] != FileSystem::PathSeparator ()
       && (BstTgb.str ()) [1] != ':')
    TGBmitPfad = modellpfad + BstTgb;
  else
    TGBmitPfad = BstTgb;

  if ((PalTgb.str ()) [0] != FileSystem::PathSeparator ()
       && (PalTgb.str ()) [1] != ':')
    PALmitPfad = modellpfad + PalTgb;
  else
    PALmitPfad = PalTgb;

  if (!FileSystem::FileExists (TGBmitPfad.str ()))
  {
    TGBmitPfad = exePath + BstTgb;
    if (!FileSystem::FileExists (TGBmitPfad.str ()))
    {
      Msg.Warning ("PruefeModellIniDatei()",
                   "Bausteinbibliothek >%s< nicht gefunden", BstTgb.str());
      return false;
    }
  }

  if (TGBmitPfad != BausteinBibliothek)
  {
    delete TgbBibliothek;
    BausteinBibliothek = TGBmitPfad;
    TgbBibliothek = new typBibliothek (BausteinBibliothek);
    TgbBibliothek->setTexturesPath(TexturesPath);
    TgbBibliothek->setVrmlPath(VrmlPath);
  }


  if (!FileSystem::FileExists (PALmitPfad.str ()))
  {
    PALmitPfad = exePath + PalTgb;
    if (!FileSystem::FileExists (PALmitPfad.str ()))
    {
      Msg.Warning ("PruefeModellIniDatei()",
                   "Palettenbibliothek >%s< nicht gefunden", PalTgb.str());
      return false;
    }
  }
  
  if (PALmitPfad != PalettenBibliothek)
  {
    delete PalBibliothek;
    PalettenBibliothek = PALmitPfad;
    PalBibliothek = new Bibliothek (PalettenBibliothek);
  }
  return true;
}  // ENDE  PruefeModellIniDatei
