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

#include <vcl.h>
#pragma hdrstop
#include <tchar.h>
#include <cxFormats.hpp>
//---------------------------------------------------------------------------
#include "AppGlobalF.h"

//---------------------------------------------------------------------------
USEFORM("MAIN\FrmInitializeF.cpp", FrmInitialize);
USEFORM("PLUGIN\VMS0500M\FrmVmsControlF.cpp", FrmVmsControl);
//---------------------------------------------------------------------------
WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
    DateSeparator = '-';
    TimeSeparator = ':';
    ShortDateFormat ="yyyy-MM-dd";
    ShortTimeFormat = "hh:nn:ss";
    LongTimeFormat  = "hh:nn:ss";

    int nRes;
    g_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_sFormsDir = g_sCfgDir + "Forms\\";
    g_sImageDir = g_sAppDir + "Image\\";
    g_sVideoDir = g_sAppDir + "Video\\";
    g_sMapDir   = g_sAppDir + "MAPDATA\\";

    ForceDirectories(g_sCfgDir.c_str());
    ForceDirectories(g_sLogDir.c_str());

    String sAppDir = ExtractFilePath(Application->ExeName);
    ChDir(sAppDir);

    /*
    *  시스템 운영환경을 ini 파일에서 읽어 온다.
    */
    LoadDefaultConfigInfo("");

    HANDLE hMutex;
	try
	{
        String sLockFile = ChangeFileExt(ExtractFileName(Application->ExeName), ".lock");
        String sProgMutexNm = "HANTE_PT_" + sLockFile;
        if ((hMutex=OpenMutex(MUTEX_ALL_ACCESS, false, sProgMutexNm.c_str()))==NULL)
            hMutex = CreateMutex(NULL, true, sProgMutexNm.c_str());
        else 
        {
            LOGINFO("Program exit.... Program already running....");
            ActiveProgram();
            return 0;
        }
    }
    catch (Exception &exception)
    {
        LOGINFO("Program exit.... Exception Error....");
        Application->ShowException(&exception);
        return 0;
    }

    int nArgs = ParamCount();
    if (nArgs == 1) {
        if (!CheckParamRandKey(ParamStr(1))) {
            LOGINFO("Program exit.... Param Value Error....");
            goto prog_exit;
        }
    }
    else {
        LOGINFO("Program exit.... Param Count Error....");
        Application->MessageBox(L"\r\nVMS FORM 편집기 프로그램을 실행 할 수 없습니다.\r\n권한이 없습니다.\r\n",
                                L"프로그램 실행 권한 오류!!!",
                                MB_OK|MB_ICONERROR);
        goto prog_exit;
    }

    try
    {
        ReportMemoryLeaksOnShutdown = true;
        Application->Initialize();
        Application->MainFormOnTaskBar = true;
        Application->Title = "VMS 제어";
         Application->CreateForm(__classid(TFrmVmsControl), &FrmVmsControl);
         Application->Run();
    }
    catch (Exception &exception)
    {
         Application->ShowException(&exception);
    }
    catch (...)
    {
         try
         {
             throw Exception("");
         }
         catch (Exception &exception)
         {
             Application->ShowException(&exception);
         }
    }

prog_exit:
    try
    {
        ReleaseMutex(hMutex);
        CloseHandle(hMutex);
        hMutex = NULL;
        LOGINFO("Program end....");
    }
    catch(...)
    {
    }
    
    return 0;
}
//---------------------------------------------------------------------------

