//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

#include "AppGlobalF.h"
#include <tchar.h>
//---------------------------------------------------------------------------
USEFORM("FRM\FrmControllerInfoF.cpp", FrmControllerInfo);
USEFORM("FrmMainF.cpp", FrmMain);
USEFORM("FRM\FrmOptionF.cpp", FrmOption);
USEFORM("FRM\FrmSetBrghF.cpp", FrmSetBrgh);
USEFORM("FRM\FrmSetConfigF.cpp", FrmSetConfig);
USEFORM("FRM\FrmSetTimeF.cpp", FrmSetTime);
//---------------------------------------------------------------------------
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\\";

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

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

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

    /*
    *  환경설정정로를 읽고 난 후에 FTP 관련 경로를 생성한다.
    */
    g_sFtpFormDir   = g_sFtpHomeDir + FTP_FORM   + "\\";    // FTP Form Directory(VMS 폼정보를 저장할 디렉리를 FTP 경로 아래로 설정한다. 디렉토리 아래에 VMS별로 저장한다.)
    g_sFtpVideoDir  = g_sFtpHomeDir + FTP_VIDEO  + "\\";    // FTP Video Directory
    g_sFtpStaticDir = g_sFtpHomeDir + FTP_STATIC + "\\";    // FTP 정적폼 Directory
    g_sFtpImageDir  = g_sFtpHomeDir + FTP_IMAGE  + "\\";    // FTP Image Directory

    ForceDirectories(g_sFtpHomeDir.c_str());
    ForceDirectories(g_sFtpFormDir.c_str());
    ForceDirectories(g_sFtpVideoDir.c_str());
    ForceDirectories(g_sFtpStaticDir.c_str());
    ForceDirectories(g_sFtpImageDir.c_str());

    ITSLog = new TITSLog(g_sLogDir, g_sAppName, g_AppCfg.sLogDay, true);
    ITSLog->FLogCfg = g_LogCfg;
    LOGINFO("VMSCommServer Start....");


#if 1
	try
	{
        AnsiString sPidFile   = ChangeFileExt(ExtractFileName(Application->ExeName), ".pid");
        AnsiString sMutexName = "PTATMS_VMSCommServer.lock";
        if (APP_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;
    }
#endif

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

	try
	{
        ReportMemoryLeaksOnShutdown = true;
		Application->Initialize();
		Application->MainFormOnTaskBar = true;
		Application->Title = "VMS Communication Server";
        Application->CreateForm(__classid(TFrmMain), &FrmMain);
         Application->Run();
	}
	catch (Exception &exception)
	{
#if 1
		Application->ShowException(&exception);
#endif
	}
	catch (...)
	{
#if 1
		try
		{
			throw Exception("");
		}
		catch (Exception &exception)
		{
			Application->ShowException(&exception);
		}
#endif
	}

    LOGINFO("VMSCommServer End....");

    free(g_SysInfo);

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