 //           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 SetW2K (HWND);
export prototype Init  (HWND);  
export prototype DriverNeedReboot  (HWND);        
export prototype RemoveOldInstallerFromAddRemove(HWND);  
export prototype ChangeDisplayName( HWND);   
export prototype StopVdservice(HWND) ; 
export prototype StartVdservice(HWND) ;
export prototype SetPlatformProperty(HWND) ;
//-------------------------------------------------------     
 
function SetW2K(hMSI)
      BOOL   gbIsOSWin2000 ;
begin 
	if (SYSINFO.nISOSL = ISOSL_WIN2000) then
	    gbIsOSWin2000 = TRUE;  
	    MsiSetProperty( ISMSI_HANDLE, "W2K", "1" );           
	  
	else 
	    MsiSetProperty( ISMSI_HANDLE, "W2K", "0" ); 
	  
	endif;
      
end;
    
 function Init(hMSI)    
		  number  nvFileHandle;  
		  string  LogFileName; 
		  number ret;
  begin    
         FuncName =  "Init()"; 
       
		     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;
	    
  end;
    
function WriteToLogFile(LogFileLine,returnValue,Trans)   
   string sSucsess;
  begin                  
  		 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 ;
      
	     FileInsertLine (LOG_FILE_GLOBAL_VAR, LogFileLine + " Status: " + sSucsess, LOG_FILE_LINE_NUMBER_GLOBAL_VAR, AFTER);  
	     LOG_FILE_LINE_NUMBER_GLOBAL_VAR++;  
	  
  end;    
  
function DriverNeedReboot(hMSI)   
	NUMBER  ret ,nBuffer ;
    string szKey ,DoINeedReboot;
begin 
  		nBuffer = 256;
  		
 		RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ;          
  		szKey  = "SOFTWARE\\Redhat";

        MsiGetProperty (ISMSI_HANDLE, "ISSCHEDULEREBOOT", DoINeedReboot, nBuffer);
        SprintfMsiLog ( " Do i need reboot --- MsiGetProperty (ISMSI_HANDLE, \"ISSCHEDULEREBOOT\", %s, %d);",DoINeedReboot, nBuffer ); 
        
        if !(DoINeedReboot = "") then
        	ret = RegDBSetKeyValueEx (szKey , "DriverNeedToReboot", REGDB_STRING, "1", -1 ); 
            SprintfMsiLog ( "%d = RegDBSetKeyValueEx (%s , \"DriverNeedToReboot\", REGDB_STRING, \"1\", -1 ); ",ret, szKey ); 
	    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 = "9BDEA0108918EF847777000000104814"; 
   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 StopVdservice(hMSI)   
  string szProgram,szCmdLine;    
  number ret;
begin    
        szProgram = "NET.exe";
        szCmdLine = "stop "+DRIVER_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 StartVdservice(hMSI)   
	string szProgram,szCmdLine;    
	number ret;
begin    
	ret = LaunchAppAndWait("sc.exe","config " + DRIVER_SERVICE_NAME+ "  start= auto depend= VirtioSerial" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	    
	SprintfMsiLog("ret = LaunchAppAndWait   (sc.exe,config   %s  start= auto depend= VirtioSerial ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);",DRIVER_SERVICE_NAME);
        
        szProgram = "NET.exe";
        szCmdLine = "start "+DRIVER_SERVICE_NAME;
    	ret =  LaunchAppAndWait (szProgram,szCmdLine, LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN );

end;   
 

function SetPlatformProperty(hMSI)   
    // To Do:  Declare local variables.
    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;
 //  MessageBox(MYOS,1);    
     MsiSetProperty(hMSI,"MYOS",MYOS);          
     SprintfMsiLog ( "ISCRIPT End SetPlatformProperty  MYOS = %s ",MYOS ); 
     
end;  

  



