export prototype Init();    
export prototype InitGlobal();
export prototype SetComponentRegisryInfo(string,string,string) ;
export prototype string GetComponentRegisryInfo(string ,string,string) ; 
export prototype AmIUpgrade();
export prototype SetFlagDriversInstaled();
export prototype  CheckFeatureTOInstallOnSilentMode();  
export prototype  GetMsiPackList();     
export prototype ParseProductVersion();   
export prototype DoIneedReboot ();
export prototype ExFn_System(HWND);   
export prototype ChangeDisplayName( );       
export prototype UninstallFeatures();      
export prototype WhoAmI();   
export prototype GetListOfDriversToInstall();   
export prototype SetDriverFlag(string);
export prototype  ClearListOfDriverToInstall();      
export prototype  GetInstalledToolsList();       
export prototype CopyInstalationLogs(); 
export prototype CheckForBiosManufacturer();
export prototype DriverQuery(STRING);
prototype SetInstalledDriversList();
export prototype SetRestartBeforeUpgradeFlag(); 
export prototype CheckRestartBeforeUpgradeFlag();
export prototype ProcessNewFeatures(string);
export prototype ProcessListOfDriversToUpdate(LIST);
export prototype AddDriverFlag(string);
export prototype RemoveDriverFlag(string);
export prototype ProcessXCopyResult(NUMBER);

function number CheckForBiosManufacturer() 
	string Command,szRes;
	number ret,nvFileHandle,nvErr;
	LIST lstResult;
begin
    Command =  "/c wmic bios get Manufacturer /value";
	ret = LaunchAppAndWait("cmd.exe",Command,  LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN);
	
    Command =  "/c wmic bios get Manufacturer /value" + " > \"" + FOLDER_TEMP ^"BiosManufacturerValue.log\" 2>&1";
	ret = LaunchAppAndWait   ("cmd.exe",Command,  LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN); 
	ret =  LAAW_PARAMETERS.nLaunchResult  ; 
	WriteToLogFile(" LaunchAppAndWait   (cmd.exe," + Command + ",  LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN); ",ret,1);
    OpenFileMode (FILE_MODE_NORMAL);
    if (OpenFile (nvFileHandle, FOLDER_TEMP, "BiosManufacturerValue.log") < 0) then 
		WriteToLogFile("Host type (RHEV-H or RHEL) cannot be validated.",0,4);
        return FALSE; 
    endif;
    nvErr = 0;
    while(GetLine (nvFileHandle, szRes) = 0) 
		StrTrim(szRes);
		WriteToLogFile(szRes,0,2);
		if(szRes != "") then
			ret = StrFind(szRes,"Manufacturer=");
			if(nvErr = 0) then
				if(ret < 0) then
					WriteToLogFile("Host type (RHEV-H or RHEL) cannot be validated.",0,4);
					nvErr = 1;
				else
				    ret = StrFind(szRes,"=");
				    StrSub(szRes,szRes,ret+1,StrLengthChars(szRes));
				    if(szRes = "Seabios") then
				    	return TRUE;
				    endif;
				endif;
			endif;		
		endif; 
    endwhile; 
    CloseFile (nvFileHandle);
    
    return FALSE;
end;

function DriverQuery(szDriverName)
	number nRet, nvFileHandle;
	string szCommand, svLine;
begin
    szCommand =  "/c driverquery /fo csv | findstr /i " + szDriverName + " > \"" + FOLDER_TEMP ^"DriverQuery.log\" 2>&1";
	nRet = LaunchAppAndWait   ("cmd.exe", szCommand,  LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN); 
	nRet = LAAW_PARAMETERS.nLaunchResult;

	WriteToLogFile(" LaunchAppAndWait   (cmd.exe," + szCommand + ",  LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN); ",nRet,1);
    OpenFileMode (FILE_MODE_NORMAL);
    if (OpenFile(nvFileHandle, FOLDER_TEMP, "DriverQuery.log") < 0) then 
		WriteToLogFile("Can't open DriverQuery.log.",0,4);
        return FALSE;
    endif;
    
	while(GetLine(nvFileHandle, svLine) = 0)
		StrTrim(svLine);
		WriteToLogFile(svLine,0,2);
		if(svLine != "") then
			nRet = StrFindEx(svLine, "\""+szDriverName+"\"", 0);
			if(nRet = 0) then
				WriteToLogFile("Driver " + szDriverName + " was found.",0,4);
				return TRUE;
			endif;
		endif; 
    endwhile;

    CloseFile (nvFileHandle);
    return FALSE;
end;

function SetInstalledDriversList()
	BOOL bRet;
begin
	bRet = TRUE;
	
	if (DriverQuery("netkvm")) then
		if(LogWriteCustomNumber("netkvm", 1) >= ISERR_SUCCESS) then
			WriteToLogFile("NetKVM driver logged.",1,4);
		else
			WriteToLogFile("Error logging NetKVM driver.",0,4);
			bRet = FALSE;
		endif;
	endif;
	
	return bRet;
end;

function GetInstalledDriver(szDriverName)
	STRING svDrvState;
	NUMBER nvDrvState;
begin
	if (LogReadCustomNumber(szDriverName, nvDrvState) >= ISERR_SUCCESS) then
		NumToStr(svDrvState, nvDrvState);
		WriteToLogFile("Driver state for driver " + szDriverName + " is " + svDrvState,1,4);
		if (nvDrvState = 1) then
			return TRUE;
		else
			return FALSE;
		endif;
	else
		WriteToLogFile("Can't read driver state for driver " + szDriverName,1,4);
		return FALSE;
	endif;
end;

function WriteToLogFile(LogFileLine,returnValue,Trans)   
   string sSucsess;
  begin 
       // 1 = transelate error
       // 2 = dont transelate
       // 3 = dialog ret value
       // 4 = true or false  
        if(DEBUG_MODE = 1) then  
       //  if (returnValue = 1) then
            // sSucsess = "SUCCESS";
         //else
         //   sSucsess = "FAIL"; 
         //NumToStr(sSucsess,returnValue); 
          switch (Trans)
             case 1:   sSucsess =  FormatMessage ( returnValue );
             case 2:  sSucsess = "Information"; 
             case 3:  sSucsess =  "dialog"; 
             case 4:  if (returnValue) then
                        sSucsess = "TRUE";
                      else
                        sSucsess = "FALSE";  
                      endif;
          endswitch ;
           
        // endif;    
	     FileInsertLine (LOG_FILE_GLOBAL_VAR, LogFileLine + " Status: " + sSucsess, LOG_FILE_LINE_NUMBER_GLOBAL_VAR, AFTER);  
	     LOG_FILE_LINE_NUMBER_GLOBAL_VAR++;  
	   endif;
  end;
 
 function WriteToLogPreReqFile(LogFileLine,returnValue,Trans)   
   string sSucsess;
  begin 
       // 1 = transelate error
       // 2 = dont transelate
       // 3 = dialog ret value
       // 4 = true or false  
        if(DEBUG_MODE = 1) then  
       //  if (returnValue = 1) then
            // sSucsess = "SUCCESS";
         //else
         //   sSucsess = "FAIL"; 
         //NumToStr(sSucsess,returnValue); 
          switch (Trans)
             case 1:  sSucsess =  FormatMessage ( returnValue );
             case 2:  sSucsess = "Information"; 
             case 3:  sSucsess =  "dialog"; 
             case 4:  if (returnValue) then
                        sSucsess = "TRUE";
                      else
                        sSucsess = "FALSE";  
                      endif;
          endswitch ;
           
        // endif;    
	     FileInsertLine (PRE_LOG_FILE_GLOBAL_VAR, LogFileLine + " Status: " + sSucsess, PRE_LOG_FILE_LINE_NUMBER_GLOBAL_VAR, AFTER);  
	     PRE_LOG_FILE_LINE_NUMBER_GLOBAL_VAR++;  
	   endif;
  end;
   
   
   

 function Init()    
		  number  nvFileHandle;  
		  string  runfromdir,LogFileName,sProductCode; 
		  number ret;
  begin  
         IsForceReboot = FALSE;
         gListOfSucssesInstalls = "";  
         FuncName =  "Init()"; 
         IsSilentMode =FALSE;
         sProductCode  = PRODUCT_GUID;
			// init lists for the end of installtion message see OnEnd event
			// GlobalWarningIndex = 0;
			// GlobalWarninglistID = ListCreate(STRINGLIST); 
			//  GlobalErrorsIndex = 0;
			//  GlobalErrorslistID = ListCreate(STRINGLIST);
			//     if(DEBUG_MODE = 1) then    // init logger for DEBUG  for DEBUG_MODE see decl.h
		     LogFileName =   LOG_FILE_NAME;  
		     LOG_FILE_GLOBAL_VAR = FOLDER_TEMP^LogFileName;  
		     ret = OpenFileMode (FILE_MODE_APPEND);
		     ret = CreateFile ( nvFileHandle, FOLDER_TEMP, LogFileName); 
		     CloseFile( nvFileHandle);  
		     LOG_FILE_LINE_NUMBER_GLOBAL_VAR = 0;   
		     
		     LogFileName =   "PreReq_"+LOG_FILE_NAME;  
		     PRE_LOG_FILE_GLOBAL_VAR = FOLDER_TEMP^LogFileName;  
		     ret = OpenFileMode (FILE_MODE_APPEND);
		     ret = CreateFile ( nvFileHandle, FOLDER_TEMP, LogFileName); 
		     CloseFile( nvFileHandle);  
		     PRE_LOG_FILE_LINE_NUMBER_GLOBAL_VAR = 0; 
	         // endif; 
	   // get from environmet    
	  RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ;
        // setting log location to registry for log collection
      ret = RegDBSetKeyValueEx ( REDHAT_ROOT_KEY, "InstallLogLocation", REGDB_STRING, FOLDER_TEMP, -1 ); 
	    
      GetMsiPackList();
      ParsePath (runfromdir,PACKAGE_LOCATION,PATH);
 
	  if (Is(FILE_EXISTS ,runfromdir ^"Osbehavior.ini")) then 
      	OSBEHAV_INI = runfromdir ^"Osbehavior.ini";
	  else 
    	OSBEHAV_INI = SUPPORTDIR ^ "Osbehavior.ini";      
	  endif;        
  
  end;

function CheckFeatureTOInstallOnSilentMode()

begin
     SetFlagDriversInstaled();    //Check if driver is installd , if true we should upgrade.
     //	    gbAgentIsInstalled   gbSSOIsInstalled    gbUsbIsInstalled  gbAgentIsInstalled  
     // gbNetworkIsInstalled 
     if (gbSSOIsInstalled) then    //  SSO driver is installed
     		gSSOInstall =TRUE;
     endif;
     
     if (gbNetworkIsInstalled)  then   //  Network driver is installed 
			gNetworkInstall	=TRUE;	
	 endif;
	             
      if (gbAgentIsInstalled) then      //  Agent driver is installed
	       gAgentInstall =TRUE;
     endif;   
       
	if (gbSpiceIsInstalled)  then    //  Spice driver is installed
			gSpiceInstall=TRUE;		     
    endif;
    if (gbSpiceAgentIsInstalled)  then    //  Spice driver is installed
			gSpiceAgentInstall=TRUE;		     
    endif;   
   
    if (gbUsbIsInstalled) then  //  USB driver is installed
            gUsbInstall =TRUE;
    endif;
     if !(IsUpgrade)then   
     //bIntstallAgent,bIntstallNetwork,bIntstallSpice,bIntstallSSO,bIntstallUSB
        if (bIntstallSpice) then
			gSpiceInstall=TRUE;	
		endif;
		if (bIntstallSpiceAgent) then
			gSpiceAgentInstall=TRUE;	
		endif;
		if (bIntstallSSO) then		     
        	gSSOInstall =TRUE; 
        endif; 	 
        if (bIntstallNetwork) then
        	gNetworkInstall	=TRUE;
        endif;	
        if (bIntstallAgent) then	
        	gAgentInstall =TRUE;
        endif;	
        if (bIntstallUSB) then 
         gUsbInstall =TRUE;  
        endif; 
    endif;    
end;	                 
        
                       
function SetComponentRegisryInfo(ComponentName,String,Value)
   string  msg,szKey, szName,szValue; 
   number ret,nType,nSize;
begin   
  RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ;
    
  szKey = REDHAT_ROOT_KEY + "\\" + ComponentName; 
  szName = String ;
  szValue = Value;       
  nSize = -1;
  nType = REGDB_STRING;
  ret = RegDBSetKeyValueEx ( szKey, szName, nType, szValue, nSize ); 
  
  Sprintf(msg,"SetComponentRegisryInfo  ----  %d = RegDBSetKeyValueEx ( %s, %s, %d, %s, %d );",ret,szKey, szName, nType, szValue, nSize);
  WriteToLogFile(msg,0,2); 	

end; 

function string GetComponentRegisryInfo(szKey,ComponentName,String)
   string  msg,szName,szValue; 
   number ret,nType,nSize;
begin   
  
  
  RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ;
    
  szKey = szKey + "\\" + ComponentName; 
  szName = String ;
  
  ret = RegDBGetKeyValueEx ( szKey, szName, nType, szValue, nSize );  
  Sprintf(msg,"GetComponentRegisryInfo  ----  %d = RegDBGetKeyValueEx ( %s, %s, %d, %s, %d );",ret,szKey, szName, nType, szValue, nSize);
  WriteToLogFile(msg,0,2);
        
  return szValue;

end; 

function  DoIneedReboot()
    string    szPropertyValue;
    number ret;
begin                                 
     szPropertyValue = GetComponentRegisryInfo("SOFTWARE\\Redhat","","DriverNeedToReboot");
     if   !(szPropertyValue ="")then
     	BATCH_INSTALL = 1;       
     	//reset needs reboot key
     	RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ;
        RegDBSetKeyValueEx("SOFTWARE\\Redhat" ,"DriverNeedToReboot",REGDB_STRING, "", -1); 	
     endif; 
     if (IsUpgrade)then
        BATCH_INSTALL = 1;  
     endif; 
     
     if(IsForceReboot) then
     	BATCH_INSTALL = 1;
     endif;
end;  

function AmIUpgrade()
    string InstallCommand;
    string szPropertyValue ,ret;
begin     
   // WriteToLogFile("AmIUpgrade : check if it is an upgrade ",1); 
  /* szPropertyValue = GetComponentRegisryInfo(REDHAT_ROOT_KEY,"RHEV","DriversTools");
   WriteToLogFile("AmIUpgrade : check if it is an upgrade GetComponentRegisryInfo\"(RHEV,DriversTools)\"",1,1); 
    if (szPropertyValue = "1") then
		IsUpgrade = TRUE;  
		WriteToLogFile("AmIUpgrade : it is an upgrade IsUpgrade = TRUE",0,1); 
	    gAgentOldVerIsInstalled = FALSE ;
	    gSpiceOldVerIsInstalled = FALSE ;
	    gBalloonOldVerIsInstalled = FALSE ; 
	    gOldVerNetworkIsInstalled  = FALSE ; 
	    gUsbOldVerIsInstalled = FALSE ;   
	    gSSOOldVerIsInstalled  = FALSE ;
	    SetFlagDriversInstaled(); 
	   
	    
	else
	    WriteToLogFile("AmIUpgrade : it is NOT an upgrade ",0,1); 	                
	endif;	
       */
       if (MAINTENANCE)then
          IsUpgrade = TRUE;  
          WriteToLogFile("AmIUpgrade : it is an upgrade IsUpgrade = TRUE",0,1); 
       else
           WriteToLogFile("AmIUpgrade : it is NOT an upgrade ",0,1);   
       endif;
    
  end; 

function SetFlagDriversInstaled ()
    string  svSubStr, InstallCommand ; 
    number  nLocation; 
    string msg;
begin 
        WriteToLogFile(" ++++++++++++++++ SetFlagDriversInstaled () +++++++++++++++++",0,2);
        if (SYSINFO.bIsWow64)then
            InstallCommand = NETWORK_INSTALLER_64;   
		    nLocation = StrFind (InstallCommand, "_"); 
		    StrSub ( svSubStr, InstallCommand, 0, nLocation ); 
		    gbNetworkIsInstalled = IsDriverInstaled(svSubStr); 
		    Sprintf(msg,"gbNetworkIsInstalled(64)  ----  %d = IsDriverInstaled(%s);",gbNetworkIsInstalled,svSubStr);
            WriteToLogFile(msg,0,2);
        else 
		    InstallCommand = NETWORK_INSTALLER_32;   
		    nLocation = StrFind (InstallCommand, "_"); 
		    StrSub ( svSubStr, InstallCommand, 0, nLocation ); 
		    gbNetworkIsInstalled = IsDriverInstaled(svSubStr); 
		    Sprintf(msg,"gbNetworkIsInstalled(32)  ----  %d = IsDriverInstaled(%s);",gbNetworkIsInstalled,svSubStr);
            WriteToLogFile(msg,0,2);
	    endif;
	    
	    InstallCommand = AGENT_INSTALLER_32;  
	     nLocation = StrFind (InstallCommand, "_"); 
	    StrSub ( svSubStr, InstallCommand, 0, nLocation ); 
	    gbAgentIsInstalled =IsDriverInstaled(svSubStr);
	    Sprintf(msg,"gbAgentIsInstalled(32)  ----  %d = IsDriverInstaled(%s);",gbAgentIsInstalled,svSubStr);
        WriteToLogFile(msg,0,2);  
        
	    InstallCommand = SPICE_INSTALLER_32;   
	    nLocation = StrFind (InstallCommand, "_"); 
	    StrSub ( svSubStr, InstallCommand, 0, nLocation ); 
	    gbSpiceIsInstalled =IsDriverInstaled(svSubStr);
	    Sprintf(msg,"gbSpiceIsInstalled(32)  ----  %d = IsDriverInstaled(%s);",gbSpiceIsInstalled,svSubStr);
        WriteToLogFile(msg,0,2); 
        
        InstallCommand = SPICE_AGENT_INSTALLER_32;   
	    nLocation = StrFind (InstallCommand, "_"); 
	    StrSub ( svSubStr, InstallCommand, 0, nLocation ); 
	    gbSpiceAgentIsInstalled =IsDriverInstaled(svSubStr);
	    Sprintf(msg,"gbSpiceIsInstalled(32)  ----  %d = IsDriverInstaled(%s);",gbSpiceAgentIsInstalled,svSubStr);
        WriteToLogFile(msg,0,2);
	
	    InstallCommand = USB_INSTALLER_32;   
	    nLocation = StrFind (InstallCommand, "_"); 
	    StrSub ( svSubStr, InstallCommand, 0, nLocation ); 
	    gbUsbIsInstalled = IsDriverInstaled(svSubStr);
	    Sprintf(msg,"gbUsbIsInstalled(32)  ----  %d = IsDriverInstaled(%s);",gbUsbIsInstalled,svSubStr);
        WriteToLogFile(msg,0,2);
	
	    InstallCommand = SSO_INSTALLER_32;
	    nLocation = StrFind (InstallCommand, "_"); 
	    StrSub ( svSubStr, InstallCommand, 0, nLocation ); 
	    gbSSOIsInstalled = IsDriverInstaled(svSubStr); 
        Sprintf(msg,"gbSSOIsInstalled(32)  ----  %d = IsDriverInstaled(%s);",gbSSOIsInstalled,svSubStr);
        WriteToLogFile(msg,0,2); 
        
        InstallCommand = VIOSTOR_INSTALLER_32;
	    nLocation = StrFind (InstallCommand, "_"); 
	    StrSub ( svSubStr, InstallCommand, 0, nLocation ); 
	    gbViostorInstalled = IsDriverInstaled(svSubStr); 
        Sprintf(msg,"gbViostorInstalled(32)  ----  %d = IsDriverInstaled(%s);",gbViostorInstalled,svSubStr);
        WriteToLogFile(msg,0,2);
	    
	    InstallCommand = VIOSERIAL_INSTALLER_32;
	    nLocation = StrFind (InstallCommand, "_"); 
	    StrSub ( svSubStr, InstallCommand, 0, nLocation ); 
	    gbViostorInstalled = IsDriverInstaled(svSubStr); 
        Sprintf(msg,"gbVioserialInstalled(32)  ----  %d = IsDriverInstaled(%s);",gbVioserialInstalled,svSubStr);
        WriteToLogFile(msg,0,2);

end;  

function InitGlobal()    
		  number  nvFileHandle;  
		  string  LogFileName,sGetCommandline,svString; 
		  number ret,nResult,nCount,iCount; 
		  LIST sListCommandLine;
begin 
		
		sListCommandLine = ListCreate (STRINGLIST); 

		sGetCommandline = CMDLINE ;
	    StrGetTokens(sListCommandLine,sGetCommandline," ");  
	    //if ( StrGetTokens(sListCommandLine,sGetCommandline," ")<0) then 
	   
	    nCount = ListCount(sListCommandLine);
	    
	   // nResult = ListGetFirstString (sListCommandLine, svString); 

         nResult = ListGetFirstString (sListCommandLine, svString);
        // Loop while list items continue to be retrieved. 
       for iCount = 1 to nCount 
                     
            // Display the current element. 

                     
			if (StrCompare (svString,"ISSILENTMODE")=0 ) then
				IsSilentMode =TRUE;
			elseif (StrCompare(svString,"ISUPDATEONLY")=0) then
				IsUpdateOnly = TRUE;
			endif;
			nResult = ListGetNextString (sListCommandLine, svString); 
		          
        endfor;
       
		
end;


function GetMsiPackList()          
   string szFileName,szSectionName,szKeyName;
   string svVioserialResult,svViostorResult,svVioscsiResult,svUSBResult,svNetworkResult,svAgentResult,svSpiceResult,svSSOResult,svQemuGAResult;
begin  
       
      
      szFileName = SUPPORTDIR^"msi_pack_list.ini";
      szKeyName ="file";
      
      szSectionName="Pvnet";
	  GetProfString ( szFileName, szSectionName, szKeyName,svNetworkResult ); 
  	  NETWORK_INSTALLER_32 = svNetworkResult;	
      NETWORK_INSTALLER_32_FULL_PATH = SUPPORTDIR^"NETWORK"^NETWORK_INSTALLER_32; 
      
      szSectionName="Pvnet64";
	  GetProfString ( szFileName, szSectionName, szKeyName,svNetworkResult ); 
  	  NETWORK_INSTALLER_64 = svNetworkResult;	
      NETWORK_INSTALLER_64_FULL_PATH = SUPPORTDIR^"NETWORK"^NETWORK_INSTALLER_64; 
    
      szSectionName="Agent";
	  GetProfString ( szFileName, szSectionName, szKeyName, svAgentResult ); 
      AGENT_INSTALLER_32 = svAgentResult;
      AGENT_INSTALLER_32_FULL_PATH = SUPPORTDIR^"AGENT"^AGENT_INSTALLER_32; 
      
      szSectionName="Agent64";
	  GetProfString ( szFileName, szSectionName, szKeyName, svAgentResult ); 
      AGENT_INSTALLER_64 = svAgentResult;
      AGENT_INSTALLER_64_FULL_PATH = SUPPORTDIR^"AGENT"^AGENT_INSTALLER_64; 
     
      szSectionName= "Spice";
	  GetProfString (szFileName, szSectionName, szKeyName, svSpiceResult ); 
      SPICE_INSTALLER_32 =svSpiceResult;
      SPICE_INSTALLER_32_FULL_PATH =SUPPORTDIR^"SPICE"^SPICE_INSTALLER_32;
      
      szSectionName= "Spice64";
	  GetProfString (szFileName, szSectionName, szKeyName, svSpiceResult ); 
      SPICE_INSTALLER_64 =svSpiceResult;
      SPICE_INSTALLER_64_FULL_PATH =SUPPORTDIR^"SPICE"^SPICE_INSTALLER_64;
      
      szSectionName= "SpiceAgent";
	  GetProfString (szFileName, szSectionName, szKeyName, svSpiceAgentResult ); 
      SPICE_AGENT_INSTALLER_32 =svSpiceAgentResult;
      SPICE_AGENT_INSTALLER_32_FULL_PATH =SUPPORTDIR^"SPICEAGENT"^SPICE_AGENT_INSTALLER_32;
      
      szSectionName= "SpiceAgent64";
	  GetProfString (szFileName, szSectionName, szKeyName, svSpiceAgentResult ); 
      SPICE_AGENT_INSTALLER_64 =svSpiceAgentResult;
      SPICE_AGENT_INSTALLER_64_FULL_PATH =SUPPORTDIR^"SPICEAGENT"^SPICE_AGENT_INSTALLER_64;
     
      szSectionName="USB";
	  GetProfString ( szFileName, szSectionName, szKeyName, svUSBResult ); 
	  USB_INSTALLER_32 =svUSBResult;
      USB_INSTALLER_32_FULL_PATH =SUPPORTDIR^"USB"^USB_INSTALLER_32;
      
      szSectionName="SSO";
	  GetProfString ( szFileName, szSectionName, szKeyName, svSSOResult ); 
      SSO_INSTALLER_32 =svSSOResult;
      SSO_INSTALLER_32_FULL_PATH =SUPPORTDIR^"SSO"^SSO_INSTALLER_32;
      
      szSectionName="SSO64";
	  GetProfString ( szFileName, szSectionName, szKeyName, svSSOResult ); 
      SSO_INSTALLER_64 =svSSOResult;
      SSO_INSTALLER_64_FULL_PATH =SUPPORTDIR^"SSO"^SSO_INSTALLER_64;
      
      szSectionName="viostor";
	  GetProfString ( szFileName, szSectionName, szKeyName, svViostorResult ); 
      VIOSTOR_INSTALLER_32 =svViostorResult;
      VIOSTOR_INSTALLER_32_FULL_PATH =SUPPORTDIR^"Viostor"^VIOSTOR_INSTALLER_32;
      
      szSectionName="viostor64";
	  GetProfString ( szFileName, szSectionName, szKeyName, svViostorResult ); 
      VIOSTOR_INSTALLER_64 =svViostorResult;
      VIOSTOR_INSTALLER_64_FULL_PATH =SUPPORTDIR^"Viostor"^VIOSTOR_INSTALLER_64;
      
      szSectionName="vioscsi";
	  GetProfString ( szFileName, szSectionName, szKeyName, svVioscsiResult ); 
      VIOSCSI_INSTALLER_32 =svVioscsiResult;
      VIOSCSI_INSTALLER_32_FULL_PATH =SUPPORTDIR^"VioSCSI"^VIOSCSI_INSTALLER_32;
      
      szSectionName="vioscsi64";
	  GetProfString ( szFileName, szSectionName, szKeyName, svVioscsiResult ); 
      VIOSCSI_INSTALLER_64 =svVioscsiResult;
      VIOSCSI_INSTALLER_64_FULL_PATH =SUPPORTDIR^"VioSCSI"^VIOSCSI_INSTALLER_64;      
      
	  szSectionName="vioserial";
	  GetProfString ( szFileName, szSectionName, szKeyName, svVioserialResult ); 
      VIOSERIAL_INSTALLER_32 =svVioserialResult;
      VIOSERIAL_INSTALLER_32_FULL_PATH =SUPPORTDIR^"Vioserial"^VIOSERIAL_INSTALLER_32;
      
      szSectionName="vioserial64";
	  GetProfString ( szFileName, szSectionName, szKeyName, svVioserialResult ); 
      VIOSERIAL_INSTALLER_64 =svVioserialResult;
      VIOSERIAL_INSTALLER_64_FULL_PATH =SUPPORTDIR^"Vioserial"^VIOSERIAL_INSTALLER_64;
      
      szSectionName="Balloon";
	  GetProfString ( szFileName, szSectionName, szKeyName, svVioserialResult ); 
      BALLOON_INSTALLER_32 =svVioserialResult;
      BALLOON_INSTALLER_32_FULL_PATH =SUPPORTDIR^"BALLOON"^BALLOON_INSTALLER_32;
      
      szSectionName="Balloon64";
	  GetProfString ( szFileName, szSectionName, szKeyName, svVioserialResult ); 
      BALLOON_INSTALLER_64 =svVioserialResult;
      BALLOON_INSTALLER_64_FULL_PATH =SUPPORTDIR^"BALLOON"^BALLOON_INSTALLER_64;
      
      szSectionName="QemuGA";
	  GetProfString ( szFileName, szSectionName, szKeyName, svQemuGAResult ); 
	  QGA_INSTALLER_32 =svQemuGAResult;
      QGA_INSTALLER_32_FULL_PATH =SUPPORTDIR^"QGA"^QGA_INSTALLER_32;
      
      szSectionName="QemuGA64";
	  GetProfString ( szFileName, szSectionName, szKeyName, svQemuGAResult ); 
	  QGA_INSTALLER_64 =svQemuGAResult;
      QGA_INSTALLER_64_FULL_PATH =SUPPORTDIR^"QGA"^QGA_INSTALLER_64;
end;   
 
function ParseProductVersion()
     number nvVersionResult,ret,nvVersionMajor, nvVersionMinor, nvVersionBuild;
begin 
   VerProductStrToNum ( nvVersionResult, IFX_PRODUCT_VERSION ); 
   VerProductVerPartsFromVer ( nvVersionResult,nvVersionMajor, nvVersionMinor, nvVersionBuild);
   // gVersionMajor, gVersionMinor, gVersionBuild ); 
   NumToStr ( gVersionMajor, nvVersionMajor ); 
   NumToStr ( gVersionMinor, nvVersionMinor );
   NumToStr ( gVersionBuild, nvVersionBuild );
end;
   
function ExFn_System(hMSI) 
begin 
         WriteToLogFile("ExFn_System : OnEnd ",0,2); 
         System (SYS_BOOTWIN);  
         WriteToLogFile("System (SYS_BOOTWIN)",0,2); 
end; 

function ChangeDisplayName( )
   string msg,sKey,sVersionBuild,sVersionMajor,sVersionMinor,Product_name,Product_key,Product_ver,New_Display_name; 
   number  ret, nLength,nvVersionResult,nvVersionMajor, nvVersionMinor, nvVersionBuild;
begin
   Product_key = IFX_PRODUCT_KEY;  
   Sprintf(msg,"ChangeDisplayName  --- IFX_PRODUCT_KEY  (%s ) ",Product_key);
   WriteToLogFile(msg,0,2);
   Product_ver = IFX_PRODUCT_VERSION;  
   Sprintf(msg,"ChangeDisplayName  --- IFX_PRODUCT_VERSION  (%s ) ",Product_ver);
   WriteToLogFile(msg,0,2);
   Product_name = IFX_PRODUCT_NAME;   
   Sprintf(msg,"ChangeDisplayName  --- IFX_PRODUCT_NAME  (%s ) ",Product_name);
   WriteToLogFile(msg,0,2);
  
   VerProductStrToNum ( nvVersionResult, Product_ver ); 
   VerProductVerPartsFromVer ( nvVersionResult,nvVersionMajor, nvVersionMinor, nvVersionBuild);
   // gVersionMajor, gVersionMinor, gVersionBuild ); 
   NumToStr ( sVersionMajor, nvVersionMajor ); 
   NumToStr ( sVersionMinor, nvVersionMinor );
   NumToStr ( sVersionBuild, nvVersionBuild );   
   
   if ( Product_name = "RHEV-Tools ") then
   		Product_name =  "RHEV-Tools" ;
   		New_Display_name = Product_name + " " + sVersionMajor + "." +  sVersionMinor + "." + sVersionBuild;
   else
        New_Display_name = Product_name;
   endif;		
   //RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ); 
  // sKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" ^  Product_key;  
   RegDBSetItem ( REGDB_UNINSTALL_NAME, New_Display_name ); 

   Sprintf(msg,"ChangeDisplayName  --- RegDBSetItem ( %s, %s ); ",REGDB_UNINSTALL_NAME,New_Display_name);
   WriteToLogFile(msg,0,2);
 
  
   ret = RegDBSetKeyValueEx ( REDHAT_ROOT_KEY, "Version", REGDB_STRING, Product_ver, -1 ); 
  
   Sprintf(msg,"ChangeDisplayName  --- %d = RegDBSetKeyValueEx ( REDHAT_ROOT_KEY, \"Version\", REGDB_STRING, %s, -1 );  ",ret,Product_ver);
   WriteToLogFile(msg,0,2);

end;   

function UninstallFeatures()
     string msg;
begin 
    Sprintf(msg,"gSSOUNInstall --- if (%d ) then",gSSOUNInstall);
	WriteToLogFile(msg,0,2);
    if (gSSOUNInstall ) then 
        Sprintf(msg,"SSO_Driver_UnInstalling();");
	    WriteToLogFile(msg,0,2); 
    	SSO_Driver_UnInstalling();
    endif;   
    Sprintf(msg,"gAgentUNInstall --- if (%d ) then",gAgentUNInstall);
	WriteToLogFile(msg,0,2);
	if (gAgentUNInstall ) then 
	    Sprintf(msg,"Agent_Driver_UnInstalling();");
	    WriteToLogFile(msg,0,2);  
		Agent_Driver_UnInstalling();
	endif;  
	Sprintf(msg,"gUsbUNInstall --- if (%d ) then",gUsbUNInstall);
	WriteToLogFile(msg,0,2);
	if (gUsbUNInstall ) then  
	    Sprintf(msg,"USB_Driver_UnInstalling();");
	    WriteToLogFile(msg,0,2);
		USB_Driver_UnInstalling();
	endif; 
	Sprintf(msg,"gNetworkUNInstall --- if (%d ) then",gNetworkUNInstall);
	WriteToLogFile(msg,0,2);
	if (gNetworkUNInstall ) then 
	    Sprintf(msg,"Network_Driver_UnInstalling();");
	    WriteToLogFile(msg,0,2);  
		Network_Driver_UnInstalling();
	endif;
	Sprintf(msg,"gSpiceUNInstall --- if (%d ) then",gSpiceUNInstall);
	WriteToLogFile(msg,0,2);
	if (gSpiceUNInstall ) then 
	    Sprintf(msg,"Spice_Driver_UnInstalling();");
	    WriteToLogFile(msg,0,2); 
		Spice_Driver_UnInstalling();
	endif; 
	Sprintf(msg,"gSpiceAgentUNInstall --- if (%d ) then",gSpiceAgentUNInstall);
	WriteToLogFile(msg,0,2);
	if (gSpiceAgentUNInstall ) then 
	    Sprintf(msg,"Spice_Agent_UnInstalling();");
	    WriteToLogFile(msg,0,2); 
		Spice_Agent_UnInstalling();
	endif;
	Sprintf(msg,"gVioserialUNInstall --- if (%d ) then",gVioserialUNInstall);
	WriteToLogFile(msg,0,2);
	if (gVioserialUNInstall ) then 
	    Sprintf(msg,"Vioseral_UnInstalling();");
	    WriteToLogFile(msg,0,2); 
		Vioseral_UnInstalling();
	endif;
end;  

function WhoAmI()
    string msg, szMsg;
    number OSVer;
begin  
   Sprintf(msg,"OS Major Ver = (%d), OS Minor Ver = (%d)", SYSINFO.nOSMajor, SYSINFO.nOSMinor);
   WriteToLogFile(msg,0,2);
   
   OSVer = (SYSINFO.nOSMajor * 100) + SYSINFO.nOSMinor;
   OSStr = "";
   
   switch (OSVer)
   		case 501:
   			OSStr = "XP_32";
			Sprintf(msg,"OSVer = (%d), OSStr = (%s)",OSVer ,OSStr);
            WriteToLogFile(msg,0,2);
            
   		case 502:
   			if (SYSINFO.nOSProductType = VER_NT_WORKSTATION) then
            	if (SYSINFO.bIsWow64) then
               		OSStr = "XP_64"; 
             	else
               		OSStr = "";
             	endif;
            else
             	if (SYSINFO.bIsWow64) then
               		OSStr = "2003_64"; 
             	else
               		OSStr = "2003_32"; 
             	endif;
         	endif;
         	
            Sprintf(msg,"OSVer = (%d), OSStr = (%s), Wow64 = (%d), ProductType = (%d)",OSVer ,OSStr, SYSINFO.bIsWow64, SYSINFO.nOSProductType);
            WriteToLogFile(msg,0,2);
            
   		case 600:
   			if (SYSINFO.nOSProductType = VER_NT_WORKSTATION) then
            	if (SYSINFO.bIsWow64) then
               		OSStr = "VISTA_64"; 
             	else
               		OSStr = "VISTA_32";
             	endif;
            else
             	if (SYSINFO.bIsWow64) then
               		OSStr = "2008_64"; 
             	else
               		OSStr = "2008_32"; 
             	endif;
         	endif;
         	
            Sprintf(msg,"OSVer = (%d), OSStr = (%s), Wow64 = (%d), ProductType = (%d)",OSVer ,OSStr, SYSINFO.bIsWow64, SYSINFO.nOSProductType);
            WriteToLogFile(msg,0,2);
            
    	case 601:
   			if (SYSINFO.nOSProductType = VER_NT_WORKSTATION) then
            	if (SYSINFO.bIsWow64) then
               		OSStr = "W7_64"; 
             	else
               		OSStr = "W7_32";
             	endif;
            else
             	if (SYSINFO.bIsWow64) then
               		OSStr = "2008R2_64"; 
             	else
               		OSStr = ""; 
             	endif;
         	endif;
         	
            Sprintf(msg,"OSVer = (%d), OSStr = (%s), Wow64 = (%d), ProductType = (%d)",OSVer ,OSStr, SYSINFO.bIsWow64, SYSINFO.nOSProductType);
            WriteToLogFile(msg,0,2);

            
 		case 602:
   			if (SYSINFO.nOSProductType = VER_NT_WORKSTATION) then
            	if (SYSINFO.bIsWow64) then
               		OSStr = "W8_64"; 
             	else
               		OSStr = "W8_32";
             	endif;
            else
             	if (SYSINFO.bIsWow64) then
               		OSStr = "2012_64"; 
             	else
               		OSStr = ""; 
             	endif;
         	endif;
         	
            Sprintf(msg,"OSVer = (%d), OSStr = (%s), Wow64 = (%d), ProductType = (%d)",OSVer ,OSStr);
            WriteToLogFile(msg,0,2);
            
      	default:
      	
      		Sprintf(msg,"OSVer = (%d), Wow64 = (%d), ProductType = (%d)", OSVer, SYSINFO.bIsWow64, SYSINFO.nOSProductType);
      		WriteToLogFile(msg,0,2);
   			
   endswitch;
   
   if (OSStr = "") then
   		Sprintf(msg, "No supported OS was detected. Aborting installation.");
   		WriteToLogFile(msg,0,2);
   		
   		szMsg = @ID_RHLV_OS_NOT_SUPPORTED;
		MessageBox( szMsg, SEVERE );
   		abort;
   endif;
end;  

function GetListOfDriversToInstall()
   LIST listKeyNames, listKeyValues; 
   string msg,svString,svValString;  
   number nResult,ret;
begin 
	listKeyNames = ListCreate(STRINGLIST);    
	listKeyValues = ListCreate(STRINGLIST);  
    
	GetProfStringList ( OSBEHAV_INI, OSStr, listKeyNames, listKeyValues ); 
	Sprintf(msg,"GetProfStringList ( %s, %s, %d, %d );",OSBEHAV_INI, OSStr, listKeyNames, listKeyValues);
	WriteToLogFile(msg,0,2);
   
	nResult = ListGetFirstString (listKeyNames, svString);
	Sprintf(msg,"%d = ListGetFirstString (%d, %s);",nResult, listKeyNames, svString);
	WriteToLogFile(msg,0,2);
 
	ret =   ListGetFirstString (listKeyValues, svValString);
	Sprintf(msg,"%d =  ListGetFirstString (%d, %s);;",ret, listKeyValues, svValString);
	WriteToLogFile(msg,0,2);

	//Loop while list items continue to be retrieved. 
	while (nResult != END_OF_LIST) 
            Sprintf(msg,"while (%d != %d)",nResult, END_OF_LIST);
            WriteToLogFile(msg,0,2);

            // Display the current element. 
            if (svValString = "1") then  
                Sprintf(msg,"SetDriverFlag(%s);",svString);
                WriteToLogFile(msg,0,2);
                SetDriverFlag(svString);
            endif;   

            // Get the next string in the list. 
            nResult = ListGetNextString (listKeyNames, svString); 
            Sprintf(msg,"%d = ListGetNextString (%d, %s);",nResult, listKeyNames, svString);
            WriteToLogFile(msg,0,2);
 
            ret =    ListGetNextString (listKeyValues, svValString); 
            Sprintf(msg,"%d =  ListGetNextString (%d, %s);;",ret, listKeyValues, svValString);
            WriteToLogFile(msg,0,2);
	endwhile;
end;   

function SetDriverFlag(str)
    string msg;  
    number ret;
begin    
     Sprintf(msg,"switch (%s)",str);
	 WriteToLogFile(msg,0,2);
     switch (str) 
   
       case "VIOSERIAL": 
            if(!MAINTENANCE) then
	            bIntstallVIOSERIAL = TRUE;
	            Sprintf(msg,"VIOSERIAL -  bIntstallVIOSERIAL = TRUE;",str);
		        WriteToLogFile(msg,0,2); 
		        ret = FeatureSelectItem ( MEDIA,"Vioserial", TRUE );
		        Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Vioserial\", TRUE );",ret);
		        WriteToLogFile(msg,ret,1);
	        endif;
	         
	        ret =  FeatureSetData ( MEDIA, "Vioserial", FEATURE_FIELD_VISIBLE, TRUE, "" );   
            Sprintf(msg,"%d =  FeatureSetData ( MEDIA, \"Vioserial\", FEATURE_FIELD_VISIBLE, TRUE, \"\" );",ret);
	        WriteToLogFile(msg,ret,1);  

        case "AGENT":
              if(!MAINTENANCE) then
	              bIntstallAgent = TRUE;
	              Sprintf(msg,"AGENT -  bIntstallAgent = TRUE;",str);
		          WriteToLogFile(msg,0,2);    
		          ret = FeatureSelectItem ( MEDIA,"Agent_Driver", TRUE );
		          Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Agent_Driver\", TRUE );",ret);
		          WriteToLogFile(msg,ret,1);
	          endif;
	           
	          ret =  FeatureSetData ( MEDIA, "Agent_Driver", FEATURE_FIELD_VISIBLE, TRUE, "" );   
              Sprintf(msg,"%d =  FeatureSetData ( MEDIA, \"Agent_Driver\", FEATURE_FIELD_VISIBLE, TRUE, \"\" );",ret);
	          WriteToLogFile(msg,ret,1); 
	   
        case "NETWORK": 
              if(!MAINTENANCE) then
	              bIntstallNetwork = TRUE; 
	              Sprintf(msg,"NETWORK -  bIntstallNetwork = TRUE;",str);
		          WriteToLogFile(msg,0,2);  
		          ret = FeatureSelectItem ( MEDIA,"Network_Driver", TRUE );
		          Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Network_Driver\", TRUE );",ret);
		          WriteToLogFile(msg,ret,1); 
	          endif;
	          
	          ret =  FeatureSetData ( MEDIA, "Network_Driver", FEATURE_FIELD_VISIBLE, TRUE, "" );   
              Sprintf(msg,"%d =  FeatureSetData ( MEDIA, \"Network_Driver\", FEATURE_FIELD_VISIBLE, TRUE, \"\" );",ret);
	          WriteToLogFile(msg,ret,1); 
	   
        case "SPICE": 
              if(!MAINTENANCE) then
	              bIntstallSpice = TRUE; 
	              Sprintf(msg,"SPICE -  bIntstallSpice = TRUE;",str);
		          WriteToLogFile(msg,0,2);
	              ret = FeatureSelectItem ( MEDIA,"Spice_Driver", TRUE );
		          Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Spice_Driver\", TRUE );",ret);
		          WriteToLogFile(msg,ret,1);
	          endif;
	           
	          ret =  FeatureSetData ( MEDIA, "Spice_Driver", FEATURE_FIELD_VISIBLE, TRUE, "" );   
              Sprintf(msg,"%d =  FeatureSetData ( MEDIA, \"Spice_Driver\", FEATURE_FIELD_VISIBLE, TRUE, \"\" );",ret);
	          WriteToLogFile(msg,ret,1);
	          
		case "SPICEAGENT": 
              if(!MAINTENANCE) then
	              bIntstallSpiceAgent = TRUE; 
	              Sprintf(msg,"SPICEAGENT -  bIntstallSpiceAgent = TRUE;",str);
		          WriteToLogFile(msg,0,2);
	              ret = FeatureSelectItem ( MEDIA,"Spice_Agent", TRUE );
		          Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Spice_Agent\", TRUE );",ret);
		          WriteToLogFile(msg,ret,1);
	          endif;
	           
	          ret =  FeatureSetData ( MEDIA, "Spice_Agent", FEATURE_FIELD_VISIBLE, TRUE, "" );   
              Sprintf(msg,"%d =  FeatureSetData ( MEDIA, \"Spice_Agent\", FEATURE_FIELD_VISIBLE, TRUE, \"\" );",ret);
	          WriteToLogFile(msg,ret,1);
        
        case "SSO":
            if(!MAINTENANCE) then
	            bIntstallSSO =  TRUE;  
	            Sprintf(msg,"SSO -  bIntstallSSO = TRUE;",str);
		        WriteToLogFile(msg,0,2);  
		        ret = FeatureSelectItem ( MEDIA,"SSO_Driver", TRUE );
		        Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"SSO_Driver\", TRUE );",ret);
		        WriteToLogFile(msg,ret,1);
	        endif;
	         
	        ret =  FeatureSetData ( MEDIA, "SSO_Driver", FEATURE_FIELD_VISIBLE, TRUE, "" );   
            Sprintf(msg,"%d =  FeatureSetData ( MEDIA, \"SSO_Driver\", FEATURE_FIELD_VISIBLE, TRUE, \"\" );",ret);
	        WriteToLogFile(msg,ret,1);
        
        case "USB": 
            if(!MAINTENANCE) then
	            bIntstallUSB = TRUE;
	            Sprintf(msg,"USB -  bIntstallUSB = TRUE;",str);
		        WriteToLogFile(msg,0,2);  
		        ret = FeatureSelectItem ( MEDIA,"USB_Driver", TRUE );
		        Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"USB_Driver\", TRUE );",ret);
		        WriteToLogFile(msg,ret,1);
	        endif;
	          
	        ret =  FeatureSetData ( MEDIA, "USB_Driver", FEATURE_FIELD_VISIBLE, TRUE, "" );   
            Sprintf(msg,"%d =  FeatureSetData ( MEDIA, \"USB_Driver\", FEATURE_FIELD_VISIBLE, TRUE, \"\" );",ret);
	        WriteToLogFile(msg,ret,1);
	    
	    case "VIOSTOR": 
            if(!MAINTENANCE) then
	            bIntstallVIOSTOR = TRUE;
	            Sprintf(msg,"VIOSTOR -  bIntstallVIOSTOR = TRUE;",str);
		        WriteToLogFile(msg,0,2); 
		        ret = FeatureSelectItem ( MEDIA,"Viostor", TRUE );
		        Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Viostor\", TRUE );",ret);
		        WriteToLogFile(msg,ret,1);
	        endif;
	         
	        ret =  FeatureSetData ( MEDIA, "Viostor", FEATURE_FIELD_VISIBLE, TRUE, "" );   
            Sprintf(msg,"%d =  FeatureSetData ( MEDIA, \"Viostor\", FEATURE_FIELD_VISIBLE, TRUE, \"\" );",ret);
	        WriteToLogFile(msg,ret,1);
	    
	    case "VIOSCSI": 
            if(!MAINTENANCE) then
	            bInstallVIOSCSI = TRUE;
	            Sprintf(msg,"VIOSCSI -  bIntstallVIOSCSI = TRUE;",str);
		        WriteToLogFile(msg,0,2); 
		        ret = FeatureSelectItem ( MEDIA,"Vioscsi", TRUE );
		        Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Vioscsi\", TRUE );",ret);
		        WriteToLogFile(msg,ret,1);
	        endif;
	         
	        ret =  FeatureSetData ( MEDIA, "Vioscsi", FEATURE_FIELD_VISIBLE, TRUE, "" );   
            Sprintf(msg,"%d =  FeatureSetData ( MEDIA, \"Vioscsi\", FEATURE_FIELD_VISIBLE, TRUE, \"\" );",ret);
	        WriteToLogFile(msg,ret,1);	    
	        
	    case "BALLOON": 
            if(!MAINTENANCE) then
	            bIntstallBALLOON = TRUE;
	            Sprintf(msg,"BALLOON -  bIntstallBALLOON = TRUE;",str);
		        WriteToLogFile(msg,0,2); 
		        ret = FeatureSelectItem ( MEDIA,"Balloon", TRUE );
		        Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Balloon\", TRUE );",ret);
		        WriteToLogFile(msg,ret,1);
	        endif;
	         
	        ret =  FeatureSetData ( MEDIA, "Balloon", FEATURE_FIELD_VISIBLE, TRUE, "" );   
            Sprintf(msg,"%d =  FeatureSetData ( MEDIA, \"Balloon\", FEATURE_FIELD_VISIBLE, TRUE, \"\" );",ret);
	        WriteToLogFile(msg,ret,1);
	        
	    case "QGA": 
            if(!MAINTENANCE) then
	            bInstallQemuGA = TRUE;
	            Sprintf(msg,"QGA -  bIntstallQemuGA = TRUE;",str);
		        WriteToLogFile(msg,0,2);  
		        ret = FeatureSelectItem ( MEDIA,"QemuGA", TRUE );
		        Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"QemuGA\", TRUE );",ret);
		        WriteToLogFile(msg,ret,1);
	        endif;
	          
	        ret =  FeatureSetData ( MEDIA, "QemuGA", FEATURE_FIELD_VISIBLE, TRUE, "" );   
            Sprintf(msg,"%d =  FeatureSetData ( MEDIA, \"QemuGA\", FEATURE_FIELD_VISIBLE, TRUE, \"\" );",ret);
	        WriteToLogFile(msg,ret,1);
	        
    endswitch; 
end; 

function   ClearListOfDriverToInstall()
   number ret;
   string msg;
begin
   if(!MAINTENANCE) then
   		ret =  FeatureSelectItem ( MEDIA,"Spice_Driver", FALSE );
   		ret =  FeatureSelectItem ( MEDIA,"Spice_Agent", FALSE );
      	ret =  FeatureSelectItem ( MEDIA,"Network_Driver", FALSE ); 
   		ret =  FeatureSelectItem ( MEDIA,"USB_Driver", FALSE );
   		ret =  FeatureSelectItem ( MEDIA,"Viostor", FALSE ); 
   		ret =  FeatureSelectItem ( MEDIA,"Vioscsi", FALSE );
   		ret =  FeatureSelectItem ( MEDIA,"Agent_Driver", FALSE ); 
   		ret =  FeatureSelectItem ( MEDIA,"Vioserial", FALSE ); 
   		ret =  FeatureSelectItem ( MEDIA,"SSO_Driver", FALSE );
   		ret =  FeatureSelectItem ( MEDIA,"Balloon", FALSE );
   		ret =  FeatureSelectItem ( MEDIA,"QemuGA", FALSE );
   endif;
   
   ret =  FeatureSetData ( MEDIA, "Spice_Driver", FEATURE_FIELD_VISIBLE, FALSE, "" ); 
   ret =  FeatureSetData ( MEDIA, "Spice_Agent", FEATURE_FIELD_VISIBLE, FALSE, "" );  
   ret =  FeatureSetData ( MEDIA, "Network_Driver", FEATURE_FIELD_VISIBLE, FALSE, "" );   
   ret =  FeatureSetData ( MEDIA, "USB_Driver", FEATURE_FIELD_VISIBLE, FALSE, "" );   
   ret =  FeatureSetData ( MEDIA, "Viostor", FEATURE_FIELD_VISIBLE, FALSE, "" );
   ret =  FeatureSetData ( MEDIA, "Vioscsi", FEATURE_FIELD_VISIBLE, FALSE, "" );
   ret =  FeatureSetData ( MEDIA, "Agent_Driver", FEATURE_FIELD_VISIBLE, FALSE, "" );   
   ret =  FeatureSetData ( MEDIA, "Vioserial", FEATURE_FIELD_VISIBLE, FALSE, "" ); 
   ret =  FeatureSetData ( MEDIA, "SSO_Driver", FEATURE_FIELD_VISIBLE, FALSE, "" );
   ret =  FeatureSetData ( MEDIA, "Balloon", FEATURE_FIELD_VISIBLE, FALSE, "" );
   ret =  FeatureSetData ( MEDIA, "QemuGA", FEATURE_FIELD_VISIBLE, FALSE, "" );
end;

function GetInstalledToolsList()
   string szKey,msg;
   number ret;
begin 
   RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ; 
   szKey = "SOFTWARE\\RedHat\\RHEV\\Tools\\" ;
    
   if (RegDBKeyExist( szKey ^ "RHEV-USB") < 0) then 
         ret = FeatureSelectItem ( MEDIA,"USB_Driver", FALSE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"USB_Driver\", FALSE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   else 
         ret = FeatureSelectItem ( MEDIA,"USB_Driver", TRUE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"USB_Driver\", TRUE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   endif; 
       
   if (RegDBKeyExist( szKey ^ "RHEV-SSO") < 0 && RegDBKeyExist( szKey ^ "RHEV-SSO64") < 0) then 
         ret = FeatureSelectItem ( MEDIA,"SSO_Driver", FALSE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"SSO_Driver\", FALSE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   else 
         ret = FeatureSelectItem ( MEDIA,"SSO_Driver", TRUE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"SSO_Driver\", TRUE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   endif;      
        
   if (RegDBKeyExist( szKey ^ "RHEV-Spice") < 0) then 
         ret = FeatureSelectItem ( MEDIA,"Spice_Driver", FALSE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Spice_Driver\", FALSE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   else 
         ret = FeatureSelectItem ( MEDIA,"Spice_Driver", TRUE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Spice_Driver\", TRUE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   endif;
   
   if (RegDBKeyExist( szKey ^ "RHEV-Spice-Agent") < 0) then 
         ret = FeatureSelectItem ( MEDIA,"Spice_Agent", FALSE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Spice_Agent\", FALSE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   else 
         ret = FeatureSelectItem ( MEDIA,"Spice_Agent", TRUE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Spice_Agent\", TRUE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   endif;      

    if (RegDBKeyExist( szKey ^ "RHEV-Block") < 0) then 
        if (RegDBKeyExist( szKey ^ "RHEV-Block64") < 0) then
        	ret = FeatureSelectItem ( MEDIA,"Viostor", FALSE );  
      	    Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Viostor\", FALSE ); ",ret);   
         	WriteToLogFile(msg ,ret,1);
        else 
            ret = FeatureSelectItem ( MEDIA,"Viostor", TRUE );  
      	    Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Viostor\", TRUE ); ",ret);   
            WriteToLogFile(msg ,ret,1); 
        endif;    	 
   else 
         ret = FeatureSelectItem ( MEDIA,"Viostor", TRUE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Viostor\", TRUE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   endif;

   if (RegDBKeyExist( szKey ^ "RHEV-SCSI") < 0) then
        if (RegDBKeyExist( szKey ^ "RHEV-SCSI64") < 0) then
        	ret = FeatureSelectItem ( MEDIA,"Vioscsi", FALSE );  
      	    Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Vioscsi\", FALSE ); ",ret);
         	WriteToLogFile(msg ,ret,1);
        else 
            ret = FeatureSelectItem ( MEDIA,"Vioscsi", TRUE );
      	    Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Vioscsi\", TRUE ); ",ret);
            WriteToLogFile(msg ,ret,1); 
        endif;    	 
   else 
         ret = FeatureSelectItem ( MEDIA,"Vioscsi", TRUE );
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Vioscsi\", TRUE ); ",ret);
         WriteToLogFile(msg ,ret,1);
   endif;

   if (RegDBKeyExist( szKey ^ "RHEV-Network") < 0) then 
        if (RegDBKeyExist( szKey ^ "RHEV-Network64") < 0) then
        	ret = FeatureSelectItem ( MEDIA,"Network_Driver", FALSE );  
      	    Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Network_Driver\", FALSE ); ",ret);   
         	WriteToLogFile(msg ,ret,1);
        else 
            ret = FeatureSelectItem ( MEDIA,"Network_Driver", TRUE );  
      	    Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Network_Driver\", TRUE ); ",ret);   
            WriteToLogFile(msg ,ret,1); 
        endif;    	 
   else 
         ret = FeatureSelectItem ( MEDIA,"Network_Driver", TRUE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Network_Driver\", TRUE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   endif; 
   
   if (RegDBKeyExist( szKey ^ "RHEV-Agent") < 0) then 
        if (RegDBKeyExist( szKey ^ "RHEV-Agent64") < 0) then
        	ret = FeatureSelectItem ( MEDIA,"Agent_Driver", FALSE );  
      	    Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Agent_Driver\", FALSE ); ",ret);   
         	WriteToLogFile(msg ,ret,1);
        else 
            ret = FeatureSelectItem ( MEDIA,"Agent_Driver", TRUE );  
      	    Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Agent_Driver\", TRUE ); ",ret);   
            WriteToLogFile(msg ,ret,1); 
        endif;    	 
   else 
         ret = FeatureSelectItem ( MEDIA,"Agent_Driver", TRUE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Agent_Driver\", TRUE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   endif; 
   
   if (RegDBKeyExist( szKey ^ "RHEV-Serial") < 0) then 
        if (RegDBKeyExist( szKey ^ "RHEV-Serial64") < 0) then
        	ret = FeatureSelectItem ( MEDIA,"Vioserial", FALSE );  
      	    Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Vioserial\", FALSE ); ",ret);   
         	WriteToLogFile(msg ,ret,1);
        else 
            ret = FeatureSelectItem ( MEDIA,"Vioserial", TRUE );  
      	    Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Vioserial\", TRUE ); ",ret);   
            WriteToLogFile(msg ,ret,1); 
        endif;    	 
   else 
         ret = FeatureSelectItem ( MEDIA,"Vioserial", TRUE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Vioserial\", TRUE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   endif;
   
   if (RegDBKeyExist( szKey ^ "RHEV-Balloon") < 0) then 
        if (RegDBKeyExist( szKey ^ "RHEV-Balloon64") < 0) then
        	ret = FeatureSelectItem ( MEDIA,"Balloon", FALSE );  
      	    Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Balloon\", FALSE ); ",ret);   
         	WriteToLogFile(msg ,ret,1);
        else 
            ret = FeatureSelectItem ( MEDIA,"Balloon", TRUE );  
      	    Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Balloon\", TRUE ); ",ret);   
            WriteToLogFile(msg ,ret,1); 
        endif;    	 
   else 
         ret = FeatureSelectItem ( MEDIA,"Balloon", TRUE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"Balloon\", TRUE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   endif;
   
   if (SYSINFO.bIsWow64)then
		REGDB_OPTIONS = REGDB_OPTIONS | REGDB_OPTION_WOW64_64KEY;
   endif;
   
   if (RegDBKeyExist( "SOFTWARE\\RedHat\\RHEL\\Tools\\" ^ "QemuGA") < 0) then 
         ret = FeatureSelectItem ( MEDIA,"QemuGA", FALSE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"QemuGA\", FALSE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   else
         ret = FeatureSelectItem ( MEDIA,"QemuGA", TRUE );  
      	 Sprintf(msg,"GetInstalledToolsList %d = FeatureSelectItem ( MEDIA,\"QemuGA\", TRUE ); ",ret);   
         WriteToLogFile(msg ,ret,1); 
   endif;

   if (SYSINFO.bIsWow64)then
		REGDB_OPTIONS = REGDB_OPTIONS & ~REGDB_OPTION_WOW64_64KEY;
   endif;

   
   ret = FeatureSelectItem ( MEDIA,"icon", TRUE );
end;   

function CopyInstalationLogs()
     number ret; 
     string msg;
begin    
    ret = CreateDir (TARGETDIR ^ "InstallLogs"); 
    Sprintf(msg,"%d = CreateDir (%s ^ \"InstallLogs\");",ret ,TARGETDIR); 
    WriteToLogFile( msg,ret,1);
    Disable(LOGGING);
        ret = XCopyFile ( FOLDER_TEMP + "\\*Install.*.log", TARGETDIR ^ "InstallLogs", COMP_NORMAL ); 
        Sprintf(msg,"%d = XCopyFile ( %s + \"\\*Install.*.log\", %s ^ \"InstallLogs\", COMP_NORMAL ); " ,ret ,FOLDER_TEMP,TARGETDIR); 
        WriteToLogFile( msg,ret,1);
      
    Enable(LOGGING);
    RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ;
        // setting log location to registry for log collection
    ret = RegDBSetKeyValueEx ( REDHAT_ROOT_KEY, "InstallLogLocation", REGDB_STRING, TARGETDIR ^ "InstallLogs", -1 ); 
	Sprintf(msg,"%d = RegDBSetKeyValueEx ( %s, \"InstallLogLocation\", REGDB_STRING, %s ^ \"InstallLogs\", -1 ); ",ret ,REDHAT_ROOT_KEY,TARGETDIR); 
    WriteToLogFile( msg,ret,1);
end; 

function RemoveUnWantedEntriesFromPendingFileRenameOperations()
    string Command;
    number ret;
begin
    //This function is hack to a problem we are facing in upgrade of
    // Spice from 2.1 to 2.2 
    // BZ# 572593
    // those three lines are found in registry - we want only two of them
    //   \??\C:\WINDOWS\system32\SET23.tmp
	//   !\??\C:\WINDOWS\system32\qxldd.dll
    //   \??\C:\WINDOWS\system32\qxldd.dll    
    
    Command =  "/c  CScript.exe " + SUPPORTDIR ^ "BPKPending.vbs" + " > " + FOLDER_TEMP ^"SpiceUpgradeBPKPending.log 2>&1";
	ret = LaunchAppAndWait   ("cmd.exe",Command,  LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN); 
	ret =  LAAW_PARAMETERS.nLaunchResult  ; 
	WriteToLogFile(" LaunchAppAndWait   (cmd.exe," + Command + ",  LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN); ",ret,1);
	Delay(5);
    Command =  "/c  CScript.exe " + SUPPORTDIR ^ "RestorePending.vbs" + " > " + FOLDER_TEMP ^"SpiceUpgradeBPKPending.log 2>&1";
	ret = LaunchAppAndWait   ("cmd.exe",Command,  LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN); 
	ret =  LAAW_PARAMETERS.nLaunchResult  ; 
	WriteToLogFile(" LaunchAppAndWait   (cmd.exe," + Command + ",  LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN); ",ret,1);
end;


function SetRestartBeforeUpgradeFlag()

    number ret;
begin    
    RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ;
    
    ret = RegDBSetKeyValueEx ( REDHAT_ROOT_KEY, "RestartBeforeUpgrade", REGDB_STRING, "1", -1 ); 
      
    ret = RegDBSetKeyValueEx ( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce", "RestartBeforeUpgrade", REGDB_STRING, "cmd.exe /c", -1 ); 

end;


function CheckRestartBeforeUpgradeFlag()

  string svValue;
  number nResult,ret,nvSize,nvType;
begin    
    RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ;
    

    ret = RegDBGetKeyValueEx ( REDHAT_ROOT_KEY, "RestartBeforeUpgrade", nvType, svValue, nvSize ); 
    
    if (svValue = "1") then
        //this is an upgrade from 4.5 to 4.6.x and up 
        svValue = "0";
    	ret = RegDBGetKeyValueEx ( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce", "RestartBeforeUpgrade", nvType, svValue, nvSize ); 
        if (svValue = "cmd.exe /c") then
              //a needed restart was not performed   
             if !(IsSilentMode) then     
             	MessageBox("In order to successfully upgrade the RHEV-Tools you need to reboot the Virtual Machine. After the Virtual Machine is restarted, proceed with the upgrade process.",SEVERE);
             	abort;   
             else
                  nResult = System( SYS_BOOTMACHINE );
                  exit;
             endif;	
              
        endif;
    else  
        SetRestartBeforeUpgradeFlag();
        if !(IsSilentMode) then     
             	MessageBox("In order to successfully upgrade the RHEV-Tools you need to reboot the Virtual Machine. After the Virtual Machine is restarted, proceed with the upgrade process.",SEVERE);
             	abort;   
             else
                 /*nResult = System( SYS_BOOTMACHINE );
                  exit; */
                 // to avoid restart in loops if no login made after restart and runonce key was not deleted
                        return 0;
             endif;	   	
    endif;
end;

function ProcessNewFeatures(str)
	LIST listFeatures;
begin
	switch(str)
		case '410':
		// VIOSCSI and QGA
				listFeatures = ListCreate(STRINGLIST);
				ListAddString(listFeatures, "VIOSCSI", AFTER);
				ListAddString(listFeatures, "QGA", AFTER);
				ProcessListOfDriversToUpdate(listFeatures);
				ListDestroy(listFeatures);
		default:
	endswitch;
end;

function ProcessListOfDriversToUpdate(listFeatures)
   LIST listKeyNames, listKeyValues; 
   string msg,svFeatureString,svString,svValString;  
   number nResult,ret;
begin
	listKeyNames = ListCreate(STRINGLIST);    
	listKeyValues = ListCreate(STRINGLIST);  
    
	GetProfStringList ( OSBEHAV_INI, OSStr, listKeyNames, listKeyValues ); 
	Sprintf(msg,"GetProfStringList ( %s, %s, %d, %d );",OSBEHAV_INI, OSStr, listKeyNames, listKeyValues);
	WriteToLogFile(msg,0,2);
	
	nResult = ListGetFirstString (listFeatures, svFeatureString);
	Sprintf(msg,"%d = ListGetFirstString (%d, %s);",nResult, listFeatures, svFeatureString);
	WriteToLogFile(msg,0,2);
 
	//Loop while list items continue to be retrieved. 
	while (nResult != END_OF_LIST) 
		Sprintf(msg,"while (%d != %d)",nResult, END_OF_LIST);
        WriteToLogFile(msg,0,2);
            
		ret = ListFindKeyValueString(listKeyNames, listKeyValues, svFeatureString, svValString);

		if (ret = END_OF_LIST) then
			Sprintf(msg,"RemoveDriverFlag(%s);",svFeatureString);
            WriteToLogFile(msg,0,2);
			RemoveDriverFlag(svFeatureString);
		elseif (ret = 0 && svValString = "0") then
			Sprintf(msg,"RemoveDriverFlag(%s);",svFeatureString);
            WriteToLogFile(msg,0,2);
			RemoveDriverFlag(svFeatureString);
		elseif (ret = 0 && svValString = "1") then
			Sprintf(msg,"AddDriverFlag(%s);",svFeatureString);
            WriteToLogFile(msg,0,2);
			AddDriverFlag(svFeatureString);
		endif;   

		// Get the next string in the list. 
		nResult = ListGetNextString (listFeatures, svFeatureString); 
		Sprintf(msg,"%d = ListGetNextString (%d, %s);",nResult, listFeatures, svFeatureString);
		WriteToLogFile(msg,0,2);
	endwhile;
end;

function AddDriverFlag(str)
    string msg;  
    number ret;
begin
	Sprintf(msg,"AddDriverFlag switch (%s)",str);
	WriteToLogFile(msg,0,2);
	switch (str) 

		case "VIOSERIAL":
			ret = FeatureSelectItem ( MEDIA,"Vioserial", TRUE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Vioserial\", TRUE );",ret);
			WriteToLogFile(msg,ret,1);

		case "AGENT":
			ret = FeatureSelectItem ( MEDIA,"Agent_Driver", TRUE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Agent_Driver\", TRUE );",ret);
			WriteToLogFile(msg,ret,1);

        case "NETWORK":
			ret = FeatureSelectItem ( MEDIA,"Network_Driver", TRUE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Network_Driver\", TRUE );",ret);
			WriteToLogFile(msg,ret,1);

        case "SPICE":
			ret = FeatureSelectItem ( MEDIA,"Spice_Driver", TRUE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Spice_Driver\", TRUE );",ret);
			WriteToLogFile(msg,ret,1);

		case "SPICEAGENT":
			ret = FeatureSelectItem ( MEDIA,"Spice_Agent", TRUE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Spice_Agent\", TRUE );",ret);
			WriteToLogFile(msg,ret,1);

        case "SSO":
			ret = FeatureSelectItem ( MEDIA,"SSO_Driver", TRUE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"SSO_Driver\", TRUE );",ret);
			WriteToLogFile(msg,ret,1);

        case "USB":
			ret = FeatureSelectItem ( MEDIA,"USB_Driver", TRUE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"USB_Driver\", TRUE );",ret);
			WriteToLogFile(msg,ret,1);

	    case "VIOSTOR":
			ret = FeatureSelectItem ( MEDIA,"Viostor", TRUE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Viostor\", TRUE );",ret);
			WriteToLogFile(msg,ret,1);

	    case "VIOSCSI":
			ret = FeatureSelectItem ( MEDIA,"Vioscsi", TRUE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Vioscsi\", TRUE );",ret);
			WriteToLogFile(msg,ret,1);

	    case "BALLOON":
			ret = FeatureSelectItem ( MEDIA,"Balloon", TRUE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Balloon\", TRUE );",ret);
			WriteToLogFile(msg,ret,1);

	    case "QGA":
			ret = FeatureSelectItem ( MEDIA,"QemuGA", TRUE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"QemuGA\", TRUE );",ret);
			WriteToLogFile(msg,ret,1);

	endswitch;
end;

function RemoveDriverFlag(str)
    string msg;  
    number ret;
begin
	Sprintf(msg,"RemoveDriverFlag switch (%s)",str);
	WriteToLogFile(msg,0,2);
	switch (str) 

		case "VIOSERIAL":
			ret = FeatureSelectItem ( MEDIA,"Vioserial", FALSE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Vioserial\", FALSE );",ret);
			WriteToLogFile(msg,ret,1);

		case "AGENT":
			ret = FeatureSelectItem ( MEDIA,"Agent_Driver", FALSE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Agent_Driver\", FALSE );",ret);
			WriteToLogFile(msg,ret,1);

        case "NETWORK":
			ret = FeatureSelectItem ( MEDIA,"Network_Driver", FALSE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Network_Driver\", FALSE );",ret);
			WriteToLogFile(msg,ret,1);

        case "SPICE":
			ret = FeatureSelectItem ( MEDIA,"Spice_Driver", FALSE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Spice_Driver\", FALSE );",ret);
			WriteToLogFile(msg,ret,1);

		case "SPICEAGENT":
			ret = FeatureSelectItem ( MEDIA,"Spice_Agent", FALSE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Spice_Agent\", FALSE );",ret);
			WriteToLogFile(msg,ret,1);

        case "SSO":
			ret = FeatureSelectItem ( MEDIA,"SSO_Driver", FALSE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"SSO_Driver\", FALSE );",ret);
			WriteToLogFile(msg,ret,1);

        case "USB":
			ret = FeatureSelectItem ( MEDIA,"USB_Driver", FALSE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"USB_Driver\", FALSE );",ret);
			WriteToLogFile(msg,ret,1);

	    case "VIOSTOR":
			ret = FeatureSelectItem ( MEDIA,"Viostor", FALSE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Viostor\", FALSE );",ret);
			WriteToLogFile(msg,ret,1);

	    case "VIOSCSI":
			ret = FeatureSelectItem ( MEDIA,"Vioscsi", FALSE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Vioscsi\", FALSE );",ret);
			WriteToLogFile(msg,ret,1);

	    case "BALLOON":
			ret = FeatureSelectItem ( MEDIA,"Balloon", FALSE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"Balloon\", FALSE );",ret);
			WriteToLogFile(msg,ret,1);

	    case "QGA":
			ret = FeatureSelectItem ( MEDIA,"QemuGA", FALSE );
			Sprintf(msg,"%d = FeatureSelectItem ( MEDIA,\"QemuGA\", FALSE );",ret);
			WriteToLogFile(msg,ret,1);

	endswitch;
end;

function SaveWdfCoInst()
    NUMBER nResult;
    STRING svSrc, svDest;
begin
    WriteToLogFile("SaveWdfCoInst ENTER.",0,2);
    
    Disable(LOGGING);
    if (OSStr = "XP_32" || OSStr = "2003_32") then
        svSrc = WINSYSDIR ^ WDFCOINST19DLLNAME;
        svDest = FOLDER_TEMP;
        if (Is(FILE_EXISTS, svDest ^ WDFCOINST19DLLNAME)) then
            nResult = DeleteFile (svDest ^ WDFCOINST19DLLNAME);
            if (nResult < 0) then
                WriteToLogFile("Error deleting "+WDFCOINST19DLLNAME+" from "+svDest+".",nResult,2);
            endif;
        endif;
        if (Is(FILE_EXISTS, svSrc)) then
            nResult = XCopyFile (svSrc, svDest, COMP_NORMAL);
            WriteToLogFile("XCopyFile("+svSrc+","+svDest+")",nResult,2);
            ProcessXCopyResult(nResult);
        else
            WriteToLogFile(svSrc + " doesn't exist.",0,2);
        endif;
    elseif (OSStr = "2003_64") then
        Disable(WOW64FSREDIRECTION);
        svSrc = WINSYSDIR64 ^ WDFCOINST19DLLNAME;
        svDest = FOLDER_TEMP;
        if (Is(FILE_EXISTS, svDest ^ WDFCOINST19DLLNAME)) then
            nResult = DeleteFile (svDest ^ WDFCOINST19DLLNAME);
            if (nResult < 0) then
                WriteToLogFile("Error deleting "+WDFCOINST19DLLNAME+" from "+svDest+".",nResult,2);
            endif;
        endif;
        if (Is(FILE_EXISTS, svSrc)) then
            nResult = XCopyFile (svSrc, svDest, COMP_NORMAL);
            WriteToLogFile("XCopyFile("+svSrc+","+svDest+")",nResult,2);
            ProcessXCopyResult(nResult);
        else
            WriteToLogFile(svSrc + " doesn't exist.",0,2);
        endif;
        Enable(WOW64FSREDIRECTION);
    else
        WriteToLogFile("Shouldn't copy the WdfCoinstaller. OSStr="+OSStr+".",0,2);
    endif;
    Enable(LOGGING);
	    
    WriteToLogFile("SaveWdfCoInst RETURN.",0,2);
end;

function RestoreWdfCoInst()
    NUMBER nResult;
    STRING svSrc, svDest;
begin
    WriteToLogFile("RestoreWdfCoInst ENTER.",0,2);
  	
  	Disable(LOGGING);
  	svSrc = FOLDER_TEMP ^ WDFCOINST19DLLNAME;
    if (Is (FILE_EXISTS, svSrc)) then
        if (OSStr = "XP_32" || OSStr = "2003_32") then
            svDest = WINSYSDIR;
	        nResult = XCopyFile (svSrc, svDest, COMP_NORMAL);
	        WriteToLogFile("XCopyFile("+svSrc+","+svDest+")",nResult,2);
	        ProcessXCopyResult(nResult);
	    elseif (OSStr = "2003_64") then
	        Disable(WOW64FSREDIRECTION);
	        svDest = WINSYSDIR64;
	        nResult = XCopyFile (svSrc, svDest, COMP_NORMAL);
	        Enable(WOW64FSREDIRECTION);
	        WriteToLogFile("XCopyFile("+svSrc+","+svDest+")",nResult,2);
	        ProcessXCopyResult(nResult);
	    else
	        WriteToLogFile("Shouldn't copy the WdfCoinstaller, OSStr="+OSStr+".",0,2);
	    endif;
	else
	    WriteToLogFile("The WdfCoinstaller is not in TEMP ("+svSrc+" doesn't exist).",0,2);
    endif;
    Enable(LOGGING);
    
    WriteToLogFile("RestoreWdfCoInst RETURN.",0,2);
end;

function ProcessXCopyResult(nResult)
begin
    switch (nResult) 

    case 0: 

        WriteToLogFile("Files successfully copied.",nResult,2); 

    case COPY_ERR_CREATEDIR: 

        WriteToLogFile("A target directory could not be created.",nResult,2); 

    case COPY_ERR_MEMORY: 

        WriteToLogFile("Insufficient memory.",nResult,2);

    case COPY_ERR_NODISKSPACE: 

        WriteToLogFile("Insufficint disk space.",nResult,2);

    case COPY_ERR_OPENINPUT: 

        WriteToLogFile("Unable to open the input files.",nResult,2);

    case COPY_ERR_OPENOUTPUT: 

        WriteToLogFile("Unable to copy the source files.",nResult,2);

    case COPY_ERR_TARGETREADONLY: 

        WriteToLogFile("A target file already exists and cannot be overwritten.",nResult,2);

    default: 

        WriteToLogFile("An unspecified error occurred.",nResult,2);

    endswitch;
end;