//modified by loïc berthelot, CryoNetworks, may 2001

// D:\oldD\test\SCP1032
// Shell folder:
// HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktop




#include <windows.h>
#include <stdio.h>
#include <commctrl.h>
#include <direct.h>

#include "defines.h"
#include "scolinstaller.h"
#include "iFilesOperations.h"
#include "iReboot.h"
#include "iWinVersion.h"
#include "iBackground.h"

//debug
#include <time.h>
//







LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);


















int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
  static char szAppName[]="Install";
  MSG msg;
  HWND hwnd;
  WNDCLASSEX wndclass;
  HANDLE TokenHandle;
  char MyPath[1024];
  long msize=1024;
  char *argv[128];
  int argc;
  int i,j,inst;
  char curdir[1024];





  OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&TokenHandle);
  SetPrivilege(TokenHandle,SE_SHUTDOWN_NAME ,TRUE);

	curdir[0]=0;
	GetModuleFileName(NULL,curdir,1024);
	i=0;j=-1;
	while(curdir[i])
	{
		if ((curdir[i]=='/')||(curdir[i]=='\\')) j=i;
		i++;
	}
	if (j>=0)
	{
		curdir[j]=0;
		_chdir(curdir);
	}

	Installdir[0]=0;
	inst=1;



	/* Détection si scol est installé */
	if (RegQueryValue(HKEY_CLASSES_ROOT,"scol_file\\shell\\open\\command",MyPath,&msize)!=ERROR_SUCCESS) {
		if (RegQueryValue(HKEY_CLASSES_ROOT,"scm_auto_file\\shell\\open\\command",MyPath,&msize)!=ERROR_SUCCESS)
			inst=0;
	}

	argc=strCutting(szCmdLine,argv);

	for (i=0 ; i<argc ; i++) {
		_log("arg[%d]=<%s>\n",i,argv[i]);

		if (!stricmp(argv[i],"/UNINSTALL")) {
			Uninstall=TRUE;
		}
		else if (!stricmp(argv[i],"/MINI")) {
			Mini=TRUE;
		}
		else if (!stricmp(argv[i],"/SILENT")) {
			Mini=TRUE;
			Silent=TRUE;
		}
		else if (!stricmp(argv[i],"/FORCE")) {
			Force=TRUE;
		}
		else {
			/* sauvegarde du répertoire d'installation ou de désinstallation */
			if ((argv[i][0]=='"') || (argv[i][0]=='\''))
				strcpy(Installdir,argv[i]+1);
			else
				strcpy(Installdir,argv[i]);
			if ((Installdir[strlen(Installdir)-1]=='"') || (Installdir[strlen(Installdir)-1]=='\''))
				Installdir[strlen(Installdir)-1]='\0';
			if ((Installdir[strlen(Installdir)-1]=='/') || (Installdir[strlen(Installdir)-1]=='\\'))
				Installdir[strlen(Installdir)-1]='\0';
		}
	}

	_log("installdir=<%s>\n",Installdir);

	if (!Uninstall && inst && Installdir[0]=='\0') {
		FILE*f=fopen("setup.txt","rt");
		if (f) {
			fclose(f);
			Uninstall=TRUE;
		}
	}

	INIFile=fopen("setup.ini","rt");

  initvars();

  Instance=hInstance;
  wndclass.cbSize=sizeof(wndclass);
  wndclass.style=CS_HREDRAW | CS_VREDRAW;
  wndclass.lpfnWndProc=WndProc;
  wndclass.cbClsExtra=0;
  wndclass.cbWndExtra=0;
  wndclass.hInstance=hInstance;
  wndclass.hIcon=NULL;
  wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
  wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
  wndclass.lpszMenuName=NULL;
  wndclass.lpszClassName=szAppName;
  wndclass.hIconSm=NULL;

  RegisterClassEx(&wndclass);

  HWND h=NULL;
  
  h=FindWindow( NULL,    // pointer to class name
                "SCOL Setup Progress"    // pointer to window name
              );



  hwnd=CreateWindowEx(WS_EX_TOPMOST,szAppName,"Setup",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, h,NULL,hInstance,NULL);

  

  ShowWindow(hwnd,SW_HIDE);

  if (!Silent)
    UpdateWindow(hwnd);

  





  while(GetMessage(&msg,NULL,0,0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
#ifdef _PICTURE
//  ShowWindow(hwnd,SW_HIDE);
//  DialogBox(hInstance,"FINISH",NULL,(DLGPROC)FinishDlgProc);
#endif
  return msg.wParam;
}   







LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
  static HINSTANCE hInstance;
  HRESULT hCOM;



  switch (iMsg)
  {


  case WM_CREATE :

	RECT rect;

	MainHWND=hwnd;
	GetClientRect (MainHWND, &rect);
	MainHWNDwidth = rect.right - rect.left;
	MainHWNDheight = rect.bottom - rect.top;
	MainHWNDtop = rect.top;
	MainHWNDleft = rect.left;

    hCOM=CoInitialize(NULL); // Initialize the COM !!!
    InitCommonControls();
    hInstance=((LPCREATESTRUCT)lParam)->hInstance;
    PostMessage(hwnd,WM_USER,0,0);
    return 0;


  case WM_PAINT:

    BeginPaint(hwnd, &MainPS); 
	iBkg_paint();
    EndPaint(hwnd, &MainPS); 
    break;



  case WM_USER:
    {
        if (!INIFile) 
        {
					if (!Silent)
						MsgBox(hwnd,NULL,"ERROR","No setup.ini file...");
          if (!oldinstallfailled) 
            DelSemaphore();
          PostQuitMessage(-1);		
          return -1;
        }

        oldinstallfailled=SetSemaphore();
        if (!Uninstall)
          ProcessIniFile (hwnd,"Setup");
        else 
          ProcessIniFile (hwnd,"Uninstall");

        unsigned long dummy;
        SendMessageTimeout(
          HWND_BROADCAST,           // handle of destination window
          WM_SETTINGCHANGE,            // message to send
          NULL,       // first message parameter
          NULL,       // second message parameter
          SMTO_ABORTIFHUNG,        // how to send the message
          50,       // time-out duration
          &dummy   // return value for synchronous call
        );
      CoUninitialize();
      if (!oldinstallfailled) 
        DelSemaphore();
      PostQuitMessage(0);                                                   
      return 0;
    }


  case WM_DESTROY:
    iBkg_close();
    CoUninitialize();
    if (!oldinstallfailled) 
      DelSemaphore();
    PostQuitMessage(0);
    return 0;
  }
  return DefWindowProc (hwnd,iMsg,wParam,lParam);
}

