
////////////////////////////////////////////////////////////////////////////////
//                                                                            
//  IIIIIII SSSSSS                                                            
//    II    SS                          InstallShield (R)                     
//    II    SSSSSS      (c) 1996-2002, InstallShield Software Corporation     
//    II        SS                     All rights reserved.             
//  IIIIIII SSSSSS                  
//                                                                            
//                                                                            
//  This template script provides the code necessary to build an entry-point 
//  function to be called in an InstallScript custom action. 
//                                                                            
//                                                                            
//    File Name:  Setup.rul                                                   
//                                                                            
//  Description:  InstallShield script                                        
//
////////////////////////////////////////////////////////////////////////////////

// Include Ifx.h for built-in InstallScript function prototypes, for Windows 
// Installer API function prototypes and constants, and to declare code for 
// the OnBegin and OnEnd events.
#include "ifx.h"
#include "Decl.h" 

    // The keyword export identifies MyFunction() as an entry-point function.
    // The argument it accepts must be a handle to the Installer database.
    export prototype WriteToLogFile(string,string,string,number,BOOL); 
    export prototype Init(HWND);    
    export prototype  InstallAgentService (HWND); 
	export prototype  RemoveAgentService  (HWND);
	export prototype  FindOemFile  (HWND);   
	export prototype  DriverNeedReboot (HWND);   
	export prototype RemoveOldInstallerFromAddRemove(HWND);   
	export prototype ChangeDisplayName( HWND);    
	export prototype SetPlatformProperty( HWND);   
	export prototype StartAgent(HWND); 
	export prototype StopAgent(HWND);    
	export prototype BOOL IsSpesificServiceExists(string) ;  
	export prototype HcCalls(HWND) ;
	export prototype GetDeviceFileName( HWND);

	prototype CopyFromLogToLogFile(); 
	
    prototype  cdecl  int HypercallLib.hcInit(); 
    prototype  cdecl  int HypercallLib.hcTerm(); 
    prototype cdecl  int HypercallLib.hcSend(int,POINTER,int); 
//-----------------------------------------------------------------    
export prototype RestoreOemFile(HWND);
export prototype BackupOemFile(HWND);
export prototype CopyOemFile(HWND);
prototype string SearchOemFile(string);

function CopyOemFile(hMSI)
begin
	if(!Is(FILE_EXISTS,WINDIR^"inf\\oem1.inf") && Is(FILE_EXISTS,WINDIR^"inf\\oem1.pnf")) then
		CopyFile(INSTALLDIR^"qxl"^"qxl.inf",WINDIR^"inf\\oem1.inf");
	endif;
	if(!Is(FILE_EXISTS,WINDIR^"inf\\oem2.inf") && Is(FILE_EXISTS,WINDIR^"inf\\oem2.pnf")) then
		CopyFile(INSTALLDIR^"qxl"^"qxl.inf",WINDIR^"inf\\oem2.inf");
	endif;
	if(!Is(FILE_EXISTS,WINDIR^"inf\\oem3.inf") && Is(FILE_EXISTS,WINDIR^"inf\\oem3.pnf")) then
		CopyFile(INSTALLDIR^"qxl"^"qxl.inf",WINDIR^"inf\\oem3.inf");
	endif;
end;     

function RestoreOemFile(hMSI)
    string Product_name,szStringB,svFileSpec,svDir,svMatchingFileName; 
    string svString,svReturnString,svReturnLine,svLine,svSubStr; 
    LIST listID;
    NUMBER ret , nvLineNumber, nResult,nvFileHandle,nNumFiles,nLength;    
    NUMBER nBuffer;
    string szKey,sStart,szName;
    number nvType,nvSize;
begin             
     SprintfMsiLog ( "ISCRIPT RestoreOemFile() : start" ); 
     nBuffer = 256;
      
     svSubStr = SearchOemFile(WINDIR^"inf");
     if(svSubStr = "") then
     	nLength = 256; 
    	MsiGetProperty(hMSI, "ProductName", Product_name, nLength);
    	
    	RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );   
      	szKey = "SOFTWARE\\RedHat\\RHEV\\Tools\\" + Product_name; 
      	szName = "OEM" ;
      	ret = RegDBGetKeyValueEx(szKey, szName, nvType, svSubStr, nvSize );  
      	SprintfMsiLog ( "%d = RegDBGetKeyValueEx(%s, %s, %d, %s, %n );",ret,szKey, szName, nvType, svSubStr, nvSize  );
      
     	if(svSubStr != "") then
          	CopyFile(INSTALLDIR^"Hypercall"^"Hypercall.inf",WINDIR^"inf"^svSubStr);
          	SprintfMsiLog ( "ISCRIPT RestoreOemFile() : CopyFile(%s,%s)", INSTALLDIR^"Hypercall"^"Hypercall.inf",WINDIR^"inf"^svSubStr);
        else
            SprintfMsiLog ( "ISCRIPT RestoreOemFile() : INF file name not found in store." );
        endif;
	 endif;
	 
    SprintfMsiLog ( "ISCRIPT RestoreOemFile() : End"); 
end;

function string SearchOemFile(svDir)
    string szStringB,svFileSpec,svMatchingFileName; 
    string svString,svReturnString,svReturnLine,svLine,svSubStr; 
    LIST listID;
    NUMBER ret , nvLineNumber, nResult,nvFileHandle,nNumFiles,nLength;    
    NUMBER nBuffer;
begin             
     szStringB="Install information file for Hypercall Driver";
     svFileSpec ="oem*.inf" ;
     
     // To Do:  Write script that will be executed when MyFunction is called.
	 nResult = FindAllFiles (svDir, svFileSpec, svMatchingFileName, RESET);
	 SprintfMsiLog ( "ISCRIPT %d = FindAllFiles (%s, %s, %s, RESET);",nResult,svDir, svFileSpec, svMatchingFileName ); 
	 OpenFileMode (FILE_MODE_NORMAL); 
	 while(nResult = 0)     
			 SprintfMsiLog ( "ISCRIPT loop to find all files while(%d = 0)",nResult ); 
		     ret = FileGrep ( svMatchingFileName, szStringB, svReturnLine, nvLineNumber, RESTART ); 
		
		     if (ret = 0) then
		        ParsePath ( svReturnString, svMatchingFileName, FILENAME_ONLY );
		        SprintfMsiLog ( "ISCRIPT ParsePath (%s,%s);", svReturnString, svMatchingFileName );
                return svReturnString;
	         endif;
		      nResult = FindAllFiles(svDir, svFileSpec, svMatchingFileName, CONTINUE); 
		      SprintfMsiLog ( "ISCRIPT %d = FindAllFiles (%s, %s, %s, CONTINUE);",nResult,svDir, svFileSpec, svMatchingFileName ); 
    endwhile;
    
    return "";
end;

function BackupOemFile(hMSI)   
    // To Do:  Declare local variables.  .
    string szStringB,svFileSpec,svDir,svMatchingFileName; 
    string svString,svReturnString,svReturnLine,svLine,svSubStr; 
    LIST listID;
    NUMBER ret , nvLineNumber, nResult,nvFileHandle,nNumFiles,nLength;    
    NUMBER nBuffer;
begin             
     //WriteToLogFile("FindOemFile() : start"  ,0,1);
     SprintfMsiLog ( "ISCRIPT FindOemFile() : start" ); 
     
     svSubStr = SearchOemFile(WINDIR^"inf");
     if(svSubStr != "") then 
     	MsiSetProperty(hMSI, "FILENAME", svSubStr);
	 endif;
     
     SprintfMsiLog ( "ISCRIPT FindOemFile() : End"); 
end; 

  //  export prototype CloseWriteToLogFile (HWND);
 
   // export prototype DisableDriver(HWND);
    // To Do:  Declare global variables, define constants, and prototype user-
    //         defined and DLL functions here.
 

// To Do:  Create a custom action for this entry-point function:
// 1.  Right-click on "Custom Actions" in the Sequences/Actions view.
// 2.  Select "Custom Action Wizard" from the context menu.
// 3.  Proceed through the wizard and give the custom action a unique name.
// 4.  Select "Run InstallScript code" for the custom action type, and in
//     the next panel select "MyFunction" (or the new name of the entry-
//     point function) for the source. 
// 5.  Click Next, accepting the default selections until the wizard 
//     creates the custom action.
//
// Once you have made a custom action, you must execute it in your setup by 
// inserting it into a sequence or making it the result of a dialog's 
// control event.

///////////////////////////////////////////////////////////////////////////////
//                                                                           
// Function:  MyFunction
//                                                                           
//  Purpose:  This function will be called by the script engine when 
//            Windows(TM) Installer executes your custom action (see the "To 
//            Do," above).
//                                                                           
///////////////////////////////////////////////////////////////////////////////
function InstallAgentService(hMSI)   
    // To Do:  Declare local variables. 
     number ret,nBuffer,nLineNum;
     string szVal,szProgram,val,szCmdLine,szRes,szLogPath;  
     string  LogFileName,LogFileRowNumber,Installparameter,gRedirectLog,szCmd;
     number  nLogFileRowNumber;
     LIST list;
begin  

        SprintfMsiLog ( "InstallAgentService : start" );   
        nBuffer = 256;  
	    //MsiGetProperty(hMSI, "VersionNT", szVal, nBuffer);
	    
	    gRedirectLog = SUPPORTDIR ^ "tmplog.txt";
    	LongPathToShortPath(gRedirectLog);
		gRedirectLog = " > "+gRedirectLog+" 2>&1"; 

	    
	    list = ListCreate(STRINGLIST);   
	     
	    MsiGetProperty(hMSI,"CustomActionData",Installparameter,nBuffer);     
	    val = Installparameter;
	    SprintfMsiLog ( "InstallAgentService ---- MsiGetProperty(hMSI,\"CustomActionData\",%s,nBuffer);",Installparameter );                                                                                                                
	    
	    szLogPath = PROGRAMFILES^"Redhat\\RHEV\\Tools\\InstallLogs";
	    CreateDir(szLogPath);
		ret = FileGrep(val^"rhev-agent.ini","rhev-agent.log",szRes,nLineNum,RESTART);
	    SprintfMsiLog ( "InstallAgentService ----> %d = FileGrep("+val^"rhev-agent.ini,rhev-agent.log,%s,...);",ret,szRes );                                                                                                                
		StrReplace(szRes,"rhev-agent.log",szLogPath + "\\Install.rhev-agent-service.log",0); 
	   	ret = FileInsertLine(val^"rhev-agent.ini",szRes,nLineNum,REPLACE);
	    SprintfMsiLog ( "InstallAgentService ----> %d = FileInsertLine("+val^"rhev-agent.ini,%s,...);",ret,szRes );                                                                                                                
	     
	    ret = ServiceAddService ( DRIVER_SERVICE_NAME, "RHEV Agent", " Manages events detection and alerts","\""+val^"guestVdsAgentService.exe"+"\"", FALSE, "" ); 
	    SprintfMsiLog ( "%d = ServiceAddService ( %s, \"RHEV Agent\", \" Manages events detection and alerts\",%s^\"guestVdsAgentService.exe\", FALSE,  );",ret,DRIVER_SERVICE_NAME,val  );                                                                                                                
	    
	    
	     if ( IsSpesificServiceExists("TermService")) then
	       	  SprintfMsiLog ( "TermService exist and not disabled");
	       	  if (IsSpesificServiceExists("spiceusbredirector")) then  
	       	       SprintfMsiLog ( "TermService exists and not disabled AND spiceusbredirector exists and not disabled");
	       	       
	       	       szCmd = "/C sc.exe config " + DRIVER_SERVICE_NAME+ "  start= auto depend= spiceusbredirector/TermService/VirtioSerial";
	       	       ret = LaunchAppAndWait   ("cmd.exe", szCmd+gRedirectLog,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	
	       	       ret =  LAAW_PARAMETERS.nLaunchResult  ; 
	       	       SprintfMsiLog ( "%d = LaunchAppAndWait   (%s ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);",ret,szCmd);
	       	       CopyFromLogToLogFile();
              else 
                   SprintfMsiLog ( "TermService exists and not disabled BUT spiceusbredirector does not exists OR it is disabled");
	       	       
	       	       szCmd = "/C sc.exe config " + DRIVER_SERVICE_NAME+ "  start= auto depend= TermService/VirtioSerial";
                   ret = LaunchAppAndWait   ("cmd.exe", szCmd+gRedirectLog,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	
                   ret =  LAAW_PARAMETERS.nLaunchResult  ; 
                   SprintfMsiLog ( "%d = LaunchAppAndWait   (%s ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);",ret,szCmd);
	       	       CopyFromLogToLogFile();
	       	  endif;
	     else 
	          if (IsSpesificServiceExists("spiceusbredirector")) then 
	               SprintfMsiLog ( "spiceusbredirector exists and not disabled BUT TermService does not exists OR it is disabled");
	       	       
	       	       szCmd = "/C sc.exe config " + DRIVER_SERVICE_NAME+ "  start= auto depend= spiceusbredirector/VirtioSerial";
	       	       ret = LaunchAppAndWait   ("cmd.exe", szCmd+gRedirectLog,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	
	       	       ret =  LAAW_PARAMETERS.nLaunchResult  ; 
	       	       SprintfMsiLog ( "%d = LaunchAppAndWait   (%s ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);",ret,szCmd);
	       	       CopyFromLogToLogFile();
	       	  else 
	       	       SprintfMsiLog ( "spiceusbredirector does not exists OR it is disabled AND TermService does not exists OR it is disabled");
	       	       
	       	       szCmd = "/C sc.exe config " + DRIVER_SERVICE_NAME+ "  start= auto depend= VirtioSerial";
	       	       ret = LaunchAppAndWait   ("cmd.exe", szCmd+gRedirectLog,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	    
	       	       ret =  LAAW_PARAMETERS.nLaunchResult  ; 
	       	       SprintfMsiLog ( "%d = LaunchAppAndWait   (%s,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);",ret,szCmd);
	       	       CopyFromLogToLogFile();
	       	  endif;
	     	  
	   	 endif;
	   	 
	   	 szCmd = "/C sc.exe failure " + DRIVER_SERVICE_NAME+ "  reset= 100000  actions= restart/60000/restart/300000/no/300000";   	
	     ret = LaunchAppAndWait   ("cmd.exe", szCmd+gRedirectLog,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	
	     ret =  LAAW_PARAMETERS.nLaunchResult  ; 
	     SprintfMsiLog ( "%d = LaunchAppAndWait   (%s,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);",ret,szCmd);
	     CopyFromLogToLogFile();
end;

function CopyFromLogToLogFile()
   string svLine, svLineBuff; 
   string Today,Now;
   number nvResult,ret,nvFileHandle;
   object oFSO,oInputFile;
begin
		Disable(LOGGING);
		
		ret = OpenFileMode (FILE_MODE_NORMAL);
		ret = OpenFile ( nvFileHandle, SUPPORTDIR, "tmplog.txt");
		if(ret<0) then
			SprintfMsiLog("CopyFromLogToLogFile: error accessing log file ");
		endif;
		
		while (GetLine (nvFileHandle, svLine) = 0)
			  	StrTrim(svLine);
			  	if(svLine != "") then
			  		SprintfMsiLog("CopyFromLogToLogFile: "+svLine);
	    		endif;   
    	endwhile;
    	CloseFile(nvFileHandle);
    	
    	Enable(LOGGING);
end;        

function RemoveAgentService(hMSI)   
    // To Do:  Declare local variables. 
     number ret,nBuffer;
     string szProgram,val,szCmdLine,LogFileRowNumber,LogFileName;  
     number  nLogFileRowNumber;
begin       
         nBuffer = 256;
         
    	 MsiGetProperty(hMSI,"CustomActionData",val,nBuffer); 
     
     	 ret = ServiceRemoveService(DRIVER_SERVICE_NAME); 
	    
end; 

 
function FindOemFile(hMSI)   
    // To Do:  Declare local variables.  .
    string szStringB,svFileSpec,svDir,svMatchingFileName; 
    string svString,svReturnString,svReturnLine,svLine,svSubStr; 
    LIST listID;
    NUMBER ret , nvLineNumber, nResult,nvFileHandle,nNumFiles,nLength;    
    NUMBER nBuffer;                      

begin             
     //WriteToLogFile("FindOemFile() : start"  ,0,1);
     SprintfMsiLog ( "FindOemFile() : start" ); 

     szStringB="Install information file for Hypercall Driver";
     
     svFileSpec ="oem*.inf" ;
     
     svDir = WINDIR^"inf";
      
     // To Do:  Write script that will be executed when MyFunction is called.
	 nResult = FindAllFiles (svDir, svFileSpec, svMatchingFileName, RESET);
	 SprintfMsiLog ( "%d = FindAllFiles (%s, %s, %s, RESET);",nResult,svDir, svFileSpec, svMatchingFileName ); 
	 OpenFileMode (FILE_MODE_NORMAL); 
	 listID = ListCreate (STRINGLIST);
	 while(nResult = 0)     
			 SprintfMsiLog ( "loop to find all files while(%d = 0)",nResult ); 
		
			 
			
		    // MessageBox (svSubStr, WARNING);  
			 
			 
		     ret = FileGrep ( svMatchingFileName, szStringB, svReturnLine, nvLineNumber, RESTART ); 
		
		    // Create an empty string list. 
		     if (ret = 0) then
		        ListAddString (listID, svMatchingFileName, AFTER); 
		        SprintfMsiLog ( " ListAddString (%d, %s, AFTER);", listID, svLine );
		        nLength = StrLength (svDir); 
			    StrSub ( svSubStr, svMatchingFileName,nLength +1 , StrLength(svMatchingFileName) ); 
       
		        ParsePath ( svReturnString, svMatchingFileName, FILENAME_ONLY );
		        ListAddString (listID, svReturnString ^".PNF", AFTER); 
	         endif;
		    
		        // Find the next matching file name. 
		
		      
		      nResult = FindAllFiles(svDir, svFileSpec, svMatchingFileName, CONTINUE); 
		      SprintfMsiLog ( "%d = FindAllFiles (%s, %s, %s, CONTINUE);",nResult,svDir, svFileSpec, svMatchingFileName ); 
		 
		      
    endwhile;
  //showmatches: 

  
    //MsiSetProperty(hMSI, "FILENAME", svSubStr); 
    //SprintfMsiLog ( " MsiSetProperty(%d, \"FILENAME\", %s);",hMSI,svSubStr); 
    nResult = ListGetFirstString (listID, svString); 


        // Loop while list items continue to be retrieved. 

        while (nResult != END_OF_LIST) 

            // Display the current element. 

            DeleteFile( svString);


            // Get the next string in the list. 

             nResult = ListGetNextString (listID, svString); 

        endwhile; 

    MsiSetProperty(hMSI, "FILENAME", svSubStr); 
    SprintfMsiLog ( "FindOemFile() : End"); 
    
end; 

function Init(hMSI)    
		  number  nvFileHandle;  
		  string  LogFileName;
		  string val; 
		  number ret,nBuffer;
  begin    
         FuncName =  "Init()"; 
         
          Disable(LOGGING);
		     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; 
		     MsiSetProperty( ISMSI_HANDLE, "LOG_FILE_LINE_NUMBER_GLOBAL_VAR", "0" );
	         MsiSetProperty( ISMSI_HANDLE, "LOG_FILE_GLOBAL_VAR", FOLDER_TEMP^LogFileName );
	     Enable(LOGGING);   
	         
  end; 

     
  function WriteToLogFile(LogFileName,LogFileRowNumber,LogFileLine,returnValue,Trans)   
   string sSucsess,val; 
       number nBuffer,nVal;   
       number nLogFileRowNumber;
  begin   
  		 nBuffer = 255;               
  		 
  		 MsiSetProperty(ISMSI_HANDLE,"LOG_FILE_GLOBAL_VAR",LogFileName);
  		 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 ;
                                                                                                           
         LOG_FILE_GLOBAL_VAR = LogFileName;
         StrToNum(nLogFileRowNumber,LogFileRowNumber); 
        
         LOG_FILE_LINE_NUMBER_GLOBAL_VAR =nLogFileRowNumber;
	     FileInsertLine (LOG_FILE_GLOBAL_VAR, LogFileLine + " Status: " + sSucsess, LOG_FILE_LINE_NUMBER_GLOBAL_VAR, AFTER);  
	     LOG_FILE_LINE_NUMBER_GLOBAL_VAR++; 
	     
	     nVal = LOG_FILE_LINE_NUMBER_GLOBAL_VAR;
	     
	     NumToStr(LogFileRowNumber,nVal); 
        
	     MsiSetProperty(ISMSI_HANDLE,"LOG_FILE_LINE_NUMBER_GLOBAL_VAR",LogFileRowNumber);                                                                                                    
      
  end;   
  
 
 function DriverNeedReboot(hMSI)   
		NUMBER  ret ,nBuffer ;
		string myo,szKey ,DoINeedReboot;
		
 begin 
  		nBuffer = 256;
  		RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ;          
  	
  		szKey  = "SOFTWARE\\Redhat";

        MsiGetProperty (ISMSI_HANDLE, "ISSCHEDULEREBOOT", DoINeedReboot, nBuffer);
        MsiGetProperty (ISMSI_HANDLE, "MYOS", myo, nBuffer); 
        if (myo = "2008") then
            DoINeedReboot = "1";
        endif;
        if !(DoINeedReboot = "") then
        
        	ret = RegDBSetKeyValueEx (szKey , "DriverNeedToReboot", REGDB_STRING, "1", -1 ); 
            
	    endif;
	    
  
  end;   

  
function RemoveOldInstallerFromAddRemove(hMSI)   
    // To Do:  Declare local variables.
    string OldKey,ProductKey;  
    
begin             

    // To Do:  Write script that will be executed when MyFunction is called. 
   ProductKey = "Installer\\Products";
   OldKey = "9BDEA0108918EF841111000000104814"; 
   RegDBSetDefaultRoot ( HKEY_CLASSES_ROOT ) ;  
   RegDBDeleteKey ( ProductKey ^ OldKey); 
   
end; 

function ChangeDisplayName( hMSI)
   string sKey,sVersionBuild,sVersionMajor,sVersionMinor,Product_name,Product_key,Product_ver,New_Display_name; 
   number  ret, nLength,nvVersionResult,nvVersionMajor, nvVersionMinor, nvVersionBuild;
begin
   
    nLength = MAX_PATH + 1; 

    MsiGetProperty(hMSI, "ProductCode", Product_key, nLength); 

    
    // reset buffer-size variable 

    nLength = MAX_PATH + 1; 

    MsiGetProperty(hMSI, "DISPAYED_VERSION", Product_ver, nLength); 

    
    if (Product_ver = "0") then
          MsiGetProperty(hMSI, "ProductVersion", Product_ver, nLength); 
    endif;
    nLength = MAX_PATH + 1; 

   MsiGetProperty(hMSI, "ProductName", Product_name, nLength); 
   
   VerProductStrToNum ( nvVersionResult, Product_ver ); 
   VerProductVerPartsFromVer ( nvVersionResult,nvVersionMajor, nvVersionMinor, nvVersionBuild);
   // gVersionMajor, gVersionMinor, gVersionBuild ); 
   NumToStr ( sVersionMajor, nvVersionMajor ); 
   NumToStr ( sVersionMinor, nvVersionMinor );
   NumToStr ( sVersionBuild, nvVersionBuild );   
   
   New_Display_name = Product_name + " " + sVersionMajor + "." +  sVersionMinor + "." + sVersionBuild;
   RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ); 
   sKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" ^  Product_key;
   if ( RegDBKeyExist( sKey ) = 1)  then
   		ret = RegDBSetKeyValueEx(sKey, "DisplayName",REGDB_STRING , New_Display_name, -1 ); 
   		RegisterTool(Product_name,Product_ver,Product_key);
   endif;		
     
   
end;

function SetPlatformProperty(hMSI)   
    // To Do:  Declare local variables.
    string szMsg,val,MYOS;       
    number nBuffer;
    
begin    
   SprintfMsiLog ( "SetPlatformProperty" ); 

   MYOS = "";
   nBuffer = 256; 
   MsiGetProperty(hMSI,"VersionNT",val,nBuffer); 

   if (val  = "502" ) then 
       SprintfMsiLog ( "Platform is XP/2003 os id %s",val ); 
       MYOS = "2003";
   endif; 
   
   if (val  = "600" ) then 
       SprintfMsiLog ( "Platform is Vista/2008 os id %s",val );
       MYOS = "2008";
   endif; 
   
   if (val  = "601" ) then    
      SprintfMsiLog ( "Platform is W7/2008R2 os id %s" ,val );
      MsiGetProperty(hMSI,"MsiNTProductType",val,nBuffer);  
      SprintfMsiLog ( "Platform is 2008R2/W7 MsiNTProductType %s" ,val );  
      if  ( val = "1") then 
        SprintfMsiLog ( "Platform is W7 " );
       	MYOS = "W7";
      else  
         SprintfMsiLog ( "Platform is 2008R2 " );
        MYOS = "2008";
      endif;   	
   endif;

   if (val = "602") then
   		SprintfMsiLog ( "Platform is W8/2012 os id %s" ,val );
   		MYOS = "2012";
   endif;
   
   if (MYOS = "") then
		SprintfMsiLog ( "Unable to detect supported OS. Terminating.");
		abort;
   endif;

   MsiSetProperty(hMSI,"MYOS",MYOS);          
   SprintfMsiLog ( "End SetPlatformProperty  MYOS = %s ",MYOS );   
end;  

  
function StartAgent(hMSI)

begin
   ServiceStartService(DRIVER_SERVICE_NAME,"");
end;         


function RegisterTool(Product_name,Product_ver,Product_key)
   string  MsgLog,szKey, szName,szValue; 
   number ret,nType,nSize;
begin
  RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ; 
  
  szKey = "SOFTWARE\\RedHat\\RHEV\\Tools\\" + Product_name; 
  szName = "Version" ;
  szValue = Product_ver;       
  nSize = -1;
  nType = REGDB_STRING;
  ret = RegDBSetKeyValueEx ( szKey, szName, nType, szValue, nSize ); 
 SprintfMsiLog(" %d = RegDBSetKeyValueEx ( %s, %s, %d, %s, %d ); ",ret,szKey, szName, nType, szValue, nSize);
   
   
  szKey = "SOFTWARE\\RedHat\\RHEV\\Tools\\" + Product_name; 
  szName = "Key" ;
  szValue = Product_key;       
  nSize = -1;
  nType = REGDB_STRING;
  ret = RegDBSetKeyValueEx ( szKey, szName, nType, szValue, nSize ); 
 SprintfMsiLog(" %d = RegDBSetKeyValueEx ( %s, %s, %d, %s, %d ); ",ret,szKey, szName, nType, szValue, nSize);
  
  szKey = "SOFTWARE\\RedHat\\RHEV\\Tools\\" + Product_name; 
  szName = "InstallDir" ;
  szValue = INSTALLDIR;
  nSize = -1;
  nType = REGDB_STRING;
  StrRemoveLastSlash(szValue);       
  ret = RegDBSetKeyValueEx ( szKey, szName, nType, szValue, nSize ); 
 SprintfMsiLog(" %d = RegDBSetKeyValueEx ( %s, %s, %d, %s, %d ); ",ret,szKey, szName, nType, szValue, nSize);
  
  szKey = "SOFTWARE\\RedHat\\RHEV\\Tools\\" + Product_name32; 
  szName = "InstallDir" ;
  szValue = INSTALLDIR;
  nSize = -1;
  nType = REGDB_STRING;
  StrRemoveLastSlash(szValue);       
  ret = RegDBSetKeyValueEx ( szKey, szName, nType, szValue, nSize ); 
 SprintfMsiLog(" %d = RegDBSetKeyValueEx ( %s, %s, %d, %s, %d ); ",ret,szKey, szName, nType, szValue, nSize);

end;     
    
  
export prototype UnregisterTool (HWND) ;  
function UnregisterTool (hMSI)
   number  ret, nLength;   
   string szKey,Product_name;
begin    
      nLength = MAX_PATH + 1;
      MsiGetProperty(hMSI, "ProductName", Product_name, nLength); 
      RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ; 
      szKey = "SOFTWARE\\RedHat\\RHEV\\Tools\\" + Product_name;    
      ret = RegDBDeleteKey ( szKey) ;  
      SprintfMsiLog(" %d = RegDBDeleteKey ( %s ); ",ret,szKey);

end;   
function StopAgent(hMSI)

begin
   ServiceStopService(DRIVER_SERVICE_NAME);
end;  

export prototype  DontShowOnAddRemove(HWND);
function DontShowOnAddRemove(hMSI)
   string sKey,Product_key; 
   number  ret, nLength;

begin
    nLength = MAX_PATH + 1; 
    
    MsiGetProperty(hMSI, "ProductCode", Product_key, nLength); 
    RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ); 
    sKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" ^  Product_key;
    ret = RegDBSetKeyValueEx(sKey, "SystemComponent",REGDB_NUMBER , "1", -1 ); 

end;     



function BOOL IsSpesificServiceExists(ServicName)
   number ret, nvSize, nvType;  
   string szKey, szName, sStart;
begin
      ret = ServiceExistsService( ServicName);  
      if !(ret) then
          SprintfMsiLog ( "The service %s does NOT Exist",ServicName);
          return 0;
      endif;
      SprintfMsiLog ( "%d = ServiceExistsService( %s);",ret ,ServicName );
      RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );   
      szKey = "SYSTEM\\CurrentControlSet\\Services\\"+ServicName; 
      szName = "Start" ;
      ret = RegDBGetKeyValueEx(szKey, szName, nvType, sStart, nvSize );  
      SprintfMsiLog ( "%d = RegDBGetKeyValueEx(%s, %s, %d, %s, %n );",ret,szKey, szName, nvType, sStart, nvSize  );
      if (sStart = "4") then
          
          SprintfMsiLog ( "The service %s is Disabled",ServicName);                                                                                                                
          return 0;
      else
          SprintfMsiLog ( "The service %s is NOT Disabled",ServicName);                                                                                                                
          return 1;
          
      endif;
end;  

function HcCalls(hMsi)
   string mydll,myinstdir,myArray[16]; 
   POINTER pMyArray ; 
   number nBuf,ret;  
   BOOL b;
   HWND nvFileHandle;
   string   MyStr,msg,szPath, szFileName; 

begin 
//0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x11
    nBuf = 256;
    MsiGetProperty(hMsi, "VIRTDEVICENAME", szFileName,nBuf);
    //szFileName = "\\\\.\\Global\\com.redhat.rhevm.vdsm"  ;
    SprintfMsiLog ("MsiGetProperty(%d, VIRTDEVICENAME, %s,%d);",hMsi,szFileName,nBuf);

    //CreateFileA(byval string, number, number, pointer, number, number, number);  
    nvFileHandle  = CreateFileA( szFileName,GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);
   // ret = OpenFile ( nvFileHandle, szPath, szFileName ); 
    MyStr = "{ \"__name__\" : \"uninstalled\" }\n";
    SprintfMsiLog ("%d = CreateFileA ( %s, GENERIC_WRITE,0,0,OPEN_EXISTING,0,0)",nvFileHandle,szFileName);
    b =  WriteFile( nvFileHandle ,MyStr,StrLength(MyStr),ret,0);
    SprintfMsiLog ("%d = WriteFile ( %d, %s, 16,%d,0 )",b,nvFileHandle, "{ \"__name__\" : \"uninstalled\" }",ret);

       
end;  

function GetDeviceFileName( hMsi)
   string file,section,name,value;   
   number ret;
begin

   file = INSTALLDIR^"rhev-agent.ini";
   section = "virtio";  
   name = "device";
   ret = GetProfString(file,section,name,value); 
   SprintfMsiLog (" %d = GetProfString(%s,%s,%s,%s);",ret,file,section,name,value); 
   
   MsiSetProperty(hMsi, "VIRTDEVICENAME", value);
   
end;     


  

 