
//---------------------------------------------------------------------------
#define COMM_EXCEPTION_MESSAGE    0
//---------------------------------------------------------------------------

#include "AppGlobalF.h"
#include <tchar.h>
//---------------------------------------------------------------------------
USEFORM("FRM\FrmVmsInfoF.cpp", FrmVmsInfo);
USEFORM("FRM\FrmSysLogF.cpp", FrmSysLog);
USEFORM("FRM\FrmOptionF.cpp", FrmOption);
USEFORM("FrmMainF.cpp", FrmMain);
USEFORM("FRM\FrmVmsLogF.cpp", FrmVmsLog);
USEFORM("DM\DMCOMMF.cpp", DMCOMM); /* TDataModule: File Type */
//---------------------------------------------------------------------------
WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
    DateSeparator = '-';
    TimeSeparator = ':';
    ShortDateFormat ="yyyy-MM-dd";
    ShortTimeFormat = "hh:nn:ss";
    LongTimeFormat  = "hh:nn:ss";

    int nPid   = (int)GetCurrentProcessId();
    g_sAppDir  = ExtractFilePath(Application->ExeName);
    g_sAppName = ChangeFileExt(ExtractFileName(Application->ExeName), "");
    g_sCfgDir  = g_sAppDir + "Cfg\\";
    g_sLogDir  = g_sAppDir + "Log\\";
    g_sTempDir = g_sAppDir + "Temp\\";
    g_sImgDir  = g_sAppDir + "Image\\";
    g_sFtpDir  = g_sAppDir + "Ftp\\";
    g_sFormDir = g_sAppDir + "Form\\";

    ForceDirectories(g_sCfgDir.c_str());
    ForceDirectories(g_sLogDir.c_str());
    ForceDirectories(g_sTempDir.c_str());
    ForceDirectories(g_sImgDir.c_str());
    //ForceDirectories(g_sFtpDir.c_str());
    ForceDirectories(g_sFormDir.c_str());

    String sTmpDir = g_sLogDir + "Comm\\";
    ForceDirectories(sTmpDir.c_str());

    ChDir(g_sAppDir);
    g_AppCfg.Clear();
    APP_LoadConfigInfo();

    ITSLog = new TITSLog(g_sLogDir,          g_sAppName,  g_AppCfg.sLogDay);
    FDbLog = new TITSLog(g_sLogDir + "Db\\", "Database",  g_AppCfg.sLogDay);
    ITSLog->FLogCfg = g_LogCfg;
    FDbLog->FLogCfg = g_LogCfg;
    LOGINFO("VMSCommServer Start....");


	try
	{
        AnsiString sPidFile   = ChangeFileExt(ExtractFileName(Application->ExeName), ".pid");
        AnsiString sMutexName = "HANTE_VMSCommServerPasig.lock";
        if (SYS_ApplicationSingleInstance(sMutexName, g_sCfgDir+sPidFile))
        {
            LOGWARN("Program already running. program exit...");
#if 0
            int    nMsgType  = MB_OK|MB_ICONERROR|MB_APPLMODAL;
            String sMsgTitle = g_AppCfg.sTitle;
            String sMsgError = ExtractFileName(Application->ExeName) + "\r\nProgram is already running.\r\nPlease check the program in the task manager.";

            Application->MessageBox(sMsgError.c_str(), sMsgTitle.c_str(), nMsgType);
#endif
            return 0;
        }
    }
    catch (Exception &exception)
    {
        //Application->ShowException(&exception);
        return 0;
    }

    /* allocate memory for system */
    if ((g_SysInfo = (SYSTEM_INFORMATION *) malloc(sizeof(SYSTEM_INFORMATION))) == NULL)
    {
        return 0;
    }
    memset(g_SysInfo, 0x00, sizeof(SYSTEM_INFORMATION));

	try
	{
		Application->Initialize();
		Application->MainFormOnTaskBar = true;
		Application->Title = "PASIG City - VMS Communication Server";
         Application->CreateForm(__classid(TFrmMain), &FrmMain);
         Application->CreateForm(__classid(TDMCOMM), &DMCOMM);
         Application->CreateForm(__classid(TFrmVmsLog), &FrmVmsLog);
         Application->Run();
	}
	catch (Exception &exception)
	{
		#if COMM_EXCEPTION_MESSAGE
		Application->ShowException(&exception);
		#endif
	}
	catch (...)
	{
		#if COMM_EXCEPTION_MESSAGE
		try
		{
			throw Exception("");
		}
		catch (Exception &exception)
		{
			Application->ShowException(&exception);
		}
		#endif
	}

	//g_Log->WriteLog(LOG_INFO, "VMSCommSvr End...");
    LOGINFO("VMSCommServer End....");

    /* free memory */
    free(g_SysInfo);

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