 //           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"
    // The keyword export identifies MyFunction() as an entry-point function.
    // The argument it accepts must be a handle to the Installer database.
#define SERVICE_NAME	"QEMU Guest Agent"
export prototype RegisterTool(string,string,string);
export prototype DriverNeedReboot  (HWND);        
export prototype ChangeDisplayName( HWND);   
export prototype StopService(HWND) ; 
export prototype StartService(HWND) ;
export prototype SetPlatformProperty(HWND) ;
export prototype InstallService(HWND) ;
export prototype UninstallService(HWND) ;    

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




    
  
   function DriverNeedReboot(hMSI)   
   NUMBER  ret ,nBuffer ;
    string szKey ,DoINeedReboot,MYOS;
  
  begin 

  		nBuffer = 256;
  		
 		RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ;          
  		szKey  = "SOFTWARE\\Redhat";
        
        
        MsiGetProperty (hMSI, "ISSCHEDULEREBOOT", DoINeedReboot, nBuffer);   
       
    
        if !(DoINeedReboot = "") then
        
        	ret = RegDBSetKeyValueEx (szKey , "DriverNeedToReboot", REGDB_STRING, "1", -1 ); 
        else
         
            
	    endif;
	  
  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, "DISPLAYED_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 StopService(hMSI)   
  string szProgram,szCmdLine;    
  number ret;
begin    
        szProgram = "NET.exe";
        szCmdLine = "stop \""+SERVICE_NAME+"\"";
    	ret =  LaunchAppAndWait (szProgram,szCmdLine, LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN );

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("ISCRIPT %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("ISCRIPT %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("ISCRIPT %d = RegDBDeleteKey ( %s ); ",ret,szKey);

end;  

function StartService(hMSI)   
	string szProgram,szCmdLine;    
	number ret;
begin    
        
        szProgram = "NET.exe";
        szCmdLine = "start \""+SERVICE_NAME+"\"";
    	ret =  LaunchAppAndWait (szProgram,szCmdLine, LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN );
    	ret = LAAW_PARAMETERS.nLaunchResult ;
       	SprintfMsiLog("%d = LaunchAppAndWait   (%s,%s ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);",ret,szProgram,szCmdLine);

end;   
 

function SetPlatformProperty(hMSI)   

    string szMsg,val,MYOS;       
    number nBuffer;
    
begin    
   SprintfMsiLog ( "ISCRIPT SetPlatformProperty" ); 

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

   if (val  = "602" ) then    
      SprintfMsiLog ( "ISCRIPT Platform is 2012/W8 os id %s" ,val );
      MsiGetProperty(hMSI,"MsiNTProductType",val,nBuffer);  
      SprintfMsiLog ( "ISCRIPT Platform is 2012/W8 MsiNTProductType %s" ,val );  
      // MessageBox(val,1);
      if  ( val = "1") then 
        SprintfMsiLog ( "ISCRIPT Platform is W8 " );
       	MYOS = "W8";
      else  
         SprintfMsiLog ( "ISCRIPT Platform is 2012 " );
        MYOS = "2012";
      endif;   	
   endif;   
   
 //  MessageBox(MYOS,1);    
     MsiSetProperty(hMSI,"MYOS",MYOS);          
     SprintfMsiLog ( "ISCRIPT End SetPlatformProperty  MYOS = %s ",MYOS ); 
     
end; 
function InstallService(hMSI)   
  string MyPath,szProgram,szCmdLine;    
  number ret;
begin 
        szProgram = INSTALLDIR ^ "qemu-ga.exe";
        LongPathToShortPath (szProgram);
        
      
        szCmdLine =" -s install "; 
        
    	ret =  LaunchAppAndWait ( szProgram ,szCmdLine, LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN );
    	ret = LAAW_PARAMETERS.nLaunchResult ;
       	SprintfMsiLog("%d = LaunchAppAndWait   (%s,%s ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);",ret,szProgram,szCmdLine);
        
        ret = LaunchAppAndWait("sc.exe","config \"" + SERVICE_NAME+ "\"  start= auto depend= VirtioSerial" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	    
        ret = LAAW_PARAMETERS.nLaunchResult ;
	    SprintfMsiLog("%d = LaunchAppAndWait   (sc.exe,config   %s  start= auto depend= VirtioSerial ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);",ret,SERVICE_NAME);

        
end;

function UninstallService(hMSI)   
  string szProgram,szCmdLine;    
  number ret;
begin    
        szProgram = INSTALLDIR ^ "qemu-ga.exe";
        szCmdLine = "-s uninstall ";    
       
    	ret =  LaunchAppAndWait ("\"" + szProgram + "\"",szCmdLine, LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN );
    	ret = LAAW_PARAMETERS.nLaunchResult ;
       	SprintfMsiLog("%d = LaunchAppAndWait   (%s,%s ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);",ret,szProgram,szCmdLine);

end;   
 

  



