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

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

//---------------------------------------------------------------------------
USEFORM("MAIN\FrmCameraPlayerF.cpp", FrmCameraPlayer);
USEFORM("MAIN\FrmCameraScreenF.cpp", FrmCameraScreen);
USEFORM("MAIN\FrmVmsCamViewerF.cpp", FrmVmsCamViewer);
USEFORM("MAIN\FRAME\FRAME_ObjListF.cpp", FRAMEObjList); /* TFrame: File Type */
USEFORM("MAIN\FrmCameraScreenManagerF.cpp", FrmCameraScreenManager);
USEFORM("MAIN\FrmInitializeF.cpp", FrmInitialize);
USEFORM("MAIN\FrmCameraFullScreenF.cpp", FrmCameraFullScreen);
USEFORM("MAIN\FrmCameraInfoF.cpp", FrmCameraInfo);
//---------------------------------------------------------------------------
WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
    int nLoginRes;

    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\\";
    ForceDirectories(g_sCfgDir.c_str());
    ForceDirectories(g_sLogDir.c_str());

    String sTempDir = g_sAppDir + "Image\\";
    ForceDirectories(sTempDir.c_str());

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

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

    ITSLog = new TITSLog(g_sLogDir, g_sAppName, g_AppCfg.sLogDay);
    ITSLog->FLogCfg = g_LogCfg;

    HANDLE hMutex;
	try
	{
        String sLockFile = ChangeFileExt(ExtractFileName(Application->ExeName), ".lock");
        String sProgMutexNm = "HANTE_" + 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 영상 모니터링 프로그램을 실행 할 수 없습니다.\r\n권한이 없습니다.\r\n",
                                L"프로그램 실행 권한 오류!!!",
                                MB_OK|MB_ICONERROR);
        goto prog_exit;
    }

    try
    {
        ReportMemoryLeaksOnShutdown = true;
        Application->Initialize();
        Application->MainFormOnTaskBar = true;
        Application->Title = g_AppCfg.sTitle;
        Application->Title = "VMS 영상 모니터링";
         Application->CreateForm(__classid(TFrmVmsCamViewer), &FrmVmsCamViewer);
         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;
}
//---------------------------------------------------------------------------

