#include <windows.h> 

#include "ce_setup.h"



codeINSTALL_INIT
Install_Init(
    HWND        hwndParent,
    BOOL        fFirstCall,
    BOOL        fPreviouslyInstalled,
    LPCTSTR     pszInstallDir
)
{
	
	return codeINSTALL_INIT_CONTINUE;
	
	//	return codeINSTALL_INIT_CANCEL;
}

codeINSTALL_EXIT
Install_Exit(
    HWND    hwndParent,
    LPCTSTR pszInstallDir,
    WORD    cFailedDirs,
    WORD    cFailedFiles,
    WORD    cFailedRegKeys,
    WORD    cFailedRegVals,
    WORD    cFailedShortcuts
)
{

#if 0
	// TCHAR szFolder[255]=TEXT("\\windows\\");
	// TCHAR szFilename[255] = TEXT("Test");
	// TCHAR szExtension[255] = TEXT(".exe");
	// _stprintf(szInstallDir, TEXT("%s%s%d%s"), szFolder,szFilename,i,szExtension)

	// codice demo da usare se volessimo lanciare l'applicazione dopo l'install
	PROCESS_INFORMATION pi;
	TCHAR szPath[MAX_PATH]; 
	_tcscpy(szPath, pszInstallDir);
	if (_T(’\\’) != pszInstallDir[_tcslen(pszInstallDir) - 1])
	{
	_tcscat(szPath, _T("\\"));
	}
	_tcscat(szPath, _T("TestApplication.exe"));
	// Start the application, and don't wait for it to exit 
	if (!CreateProcess(szPath, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, &pi)) 
	{ 
	// Failed to launch executable so indicate an error
	// and cause wceload to rollback the installation
	cie = codeINSTALL_EXIT_UNINSTALL; 
	}
#endif

	// crea la directory del filesystem scrivibile dal loader fba
	// patch - 06 luglio 2008 - rimozione userdata
#if 0
	TCHAR szPath[MAX_PATH]; 

	_tcscpy(szPath, pszInstallDir); 
	_tcscat(szPath, _T("\\userdata\\")); 

	CreateDirectory(szPath, NULL);
#endif

	return codeINSTALL_EXIT_DONE;
}

codeUNINSTALL_INIT
Uninstall_Init(
    HWND        hwndParent,
    LPCTSTR     pszInstallDir
)
{
	
	// quando inizio la disinstallazione, procediamo svuotando la directory
	// del filesystem che possiamo aver riempito tramite il loader FBA
#if 0
    WIN32_FIND_DATA findfiledata;
    HANDLE hfind;
    hfind = FindFirstFile(pszfilepath,&findfiledata);

    if(hfind != INVALID_HANDLE_VALUE) //database file exists

    {
        //delete database

        DeleteFile(pszfilepath);
    }
#endif

	// userdata
	TCHAR szPath[MAX_PATH], szFilterPath[MAX_PATH], szNewPath[MAX_PATH];; 

	_tcscpy(szPath, pszInstallDir); 
	_tcscpy(szFilterPath, pszInstallDir);
	
	// patch - 06 luglio 2008 - rimozione userdata
#if 0
	_tcscat(szPath, _T("\\userdata\\")); 
	_tcscat(szFilterPath, _T("\\userdata\\*.fbaff")); 
#else
	_tcscat(szPath, _T("\\")); 
	_tcscat(szFilterPath, _T("\\*.fbaff")); 
#endif
	//_tcscat(szFilterPath, _T("\\userdata\\*.*")); 

	// cerca nella directory
	WIN32_FIND_DATA FileData;   // Data structure describes the file found
	HANDLE hSearch;             // Search handle returned by FindFirstFile
	BOOL bFinished = FALSE;

	hSearch = FindFirstFile (szFilterPath, &FileData);
	if (hSearch == INVALID_HANDLE_VALUE) {}
	else {
		while (!bFinished) {
			lstrcpy (szNewPath, szPath);
			lstrcat (szNewPath, FileData.cFileName);
			// cancella il file
			DeleteFile(szNewPath);
			// cerca il prox
			 if (!FindNextFile (hSearch, &FileData)) {
				bFinished = TRUE;
			}
		}
		FindClose (hSearch);
	}
	// e infine rimuovi la directory del filesystem
	RemoveDirectory(szPath);

	return codeUNINSTALL_INIT_CONTINUE;

	// return codeUNINSTALL_INIT_CANCEL;

}

codeUNINSTALL_EXIT
Uninstall_Exit(
    HWND    hwndParent
)
{

	return codeUNINSTALL_EXIT_DONE;
}
