/******************************************************************************
 * program:     wp2latex                                                      *
 * function:    Main wp2latex windows form.                                   *
 * modul:       Frm_MainSheet.cpp                                             *
 * dependency:  C++ Builder                                                   *
 * licency:     Shareware                                                     *
 ******************************************************************************/
#include <vcl.h>
#include <fcntl.h>
#include "param_fix.h"

#pragma hdrstop
USEFORM("Frm_Copyright.cpp", Frm_Copyright1);
USEFORM("FRM_ErrLog.cpp", ErrLog);
USEFORM("Frm_GetPwd.cpp", GetPassword);
USEFORM("Frm_Help.cpp", Frm_Help1);
USEFORM("Frm_MainSheet.cpp", Frm_Main);
USEFORM("Frm_Progress.cpp", Progress);
USEFORM("Frm_Results.cpp", FrmResults);
USEFORM("Frm_Splash.cpp", Frm_Splash);
USEFORM("Frm_Wait4Jobs.cpp", Frm_WaitForJobs);
//---------------------------------------------------------------------------
#include "frm_ErrLog.h"
#ifdef _REENTRANT
 #include "../sources.cc/jobs/jobs.h"
#endif

//---------------------------------------------------------------------------
void CheckCompiler(void);


//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR CmdLine, int)
{
try
   {
   if(CreateMutex(NULL, FALSE, "Global\\WP2LaTeX_Mutex")==NULL)  // One global mutex is sufficient enough.
     {		// Create local mutex when global mutex cannot be created.
     CreateMutex(NULL, FALSE, "WP2LaTeX_Mutex");	// Mutex that prevents uninstalling when application is running.
     }

   FileOut = stderr;

   PatchArgs(CmdLine);
#ifdef _REENTRANT
   InitJobs(-1);
#endif
   
   if(!_pipe(TErrLog::PipeHandles,1024,O_BINARY))
      {
      //FileIn = _fdopen(PipeHandles[0],"r");
      FileOut = _fdopen(TErrLog::PipeHandles[1],"w");
      }

   Application->Initialize();
   Application->CreateForm(__classid(TFrm_Main), &Frm_Main);
                 Application->CreateForm(__classid(TErrLog), &ErrLog);
                 Application->CreateForm(__classid(TFrmResults), &FrmResults);
                 Application->CreateForm(__classid(TGetPassword), &GetPassword);
                 Application->CreateForm(__classid(TProgress), &Progress);
                 Application->CreateForm(__classid(TFrm_Splash), &Frm_Splash);
                 Application->CreateForm(__classid(TFrm_Copyright1), &Frm_Copyright1);
                 Application->CreateForm(__classid(TFrm_Help1), &Frm_Help1);
                 Application->CreateForm(__classid(TFrm_WaitForJobs), &Frm_WaitForJobs);
                 CheckCompiler();

   Application->Run();
   }
  catch (Exception &exception)
  {
    Application->ShowException(&exception);
  }

#ifdef _REENTRANT
  EndJobs();
#endif

return 0;
}
//---------------------------------------------------------------------------


