
////////////////////////////////////////////////////////////////////////////////
//                                                                            
//  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.
   // #define WXP "1" 
    export prototype WriteToLogFile(string,number,BOOL); 
    export prototype SetW2K (HWND);
    export prototype DisableDriver(HWND); 
    export prototype EnableDriver (HWND);  
    export prototype FindOemFile (HWND); 
    export prototype DriverNeedReboot(HWND);     
    export prototype RemoveOldInstallerFromAddRemove(HWND);  
    export prototype ChangeDisplayName( HWND);    
    export prototype SetPlatformProperty( HWND);    
    export prototype cdecl BOOL DevRule.IsDeviceExist( BYREF string );   
    export prototype cdecl BOOL DevRule.DisableDevice( BYREF string  );  
    export prototype cdecl BOOL DevRule.EnableDevice( BYREF string  );   
    export prototype cdecl BOOL DevRule.RescanDevices  (  ); 


//////////////////////////////////////////////////////////////////
//
//        Create Msi for Windows XP 
//
/////////////////////////////////////////////////////////////////////

function DisableDriver(hMSI)   
    // To Do:  Declare local variables. 
     number nvFileHandle,ret,nBuffer,nvLineNumber;
     string log,app,svReturnLine,command,szProgram,val,szCmdLine;
begin  
   
    //WriteToLogFile("DisableDriver() : start"  ,0,2);  
    SprintfMsiLog ( "DisableDriver() : start" ); 

    nBuffer = 256;    
    MsiGetProperty(hMSI,"SUPPORTDIR",val,nBuffer);
   // WriteToLogFile("DisableDriver() : MsiGetProperty," + val ,0,1);  
    SprintfMsiLog ( "MsiGetProperty(%d,\"CustomActionData\",%s,%d);",hMSI,val,nBuffer ); 
    app = val ^ "DevRule.DLL";  
    //app = "C:\\Benny_dev\\4.4_integ\\winBuild\\DevRule\\Debug\\DevRule.DLL";
    LongPathToShortPath(app); 
    SprintfMsiLog(" Loading DevRule: %s", app ); 
    ret = UseDLL(app);
    if (ret = 0) then
	    SprintfMsiLog ( "%d = UseDLL(%s);",ret,app );     
	    try
		ret = IsDeviceExist("PCI\\VEN_1AF4&DEV_1000&SUBSYS_00011AF4&REV_00");  
		catch
		 SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
		endcatch;
		SprintfMsiLog("%d = IsDeviceExist(VEN_1AF4&DEV_1000&SUBSYS_00011AF4&REV_00)", ret);   
	    if (ret ) then
		    szCmdLine = "PCI\\VEN_10EC&DEV_8139&SUBSYS_00000000&REV_20";     
		    try
		    ret = DisableDevice(szCmdLine);
            SprintfMsiLog(" DisableDevice(%s) = %d ",szCmdLine, ret);
		    catch
		       SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
		    endcatch;
			szCmdLine = "PCI\\VEN_10EC&DEV_8139&SUBSYS_11001AF4&REV_20";  
			try 
			ret = DisableDevice(szCmdLine);
            SprintfMsiLog(" DisableDevice(%s) = %d ",szCmdLine, ret);
		    catch
		 		SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
			endcatch; 
		  // NO NEED TO DISABLE E1000
		   /* szCmdLine = "PCI\\VEN_8086&DEV_100E&SUBSYS_00000000&REV_03";   
		    try
		    ret = DisableDevice(szCmdLine);
            SprintfMsiLog(" DisableDevice(%s) = %d ",szCmdLine, ret);
		    catch
		 		SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
			endcatch;  
			szCmdLine = "PCI\\VEN_8086&DEV_100E&SUBSYS_11001AF4&REV_03";  
			 try
		    ret = DisableDevice(szCmdLine);
            SprintfMsiLog(" DisableDevice(%s) = %d ",szCmdLine, ret);
		    catch
		 		SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
			endcatch;     
			*/
		else
		   SprintfMsiLog ( "DisableDriver() : Did not disable Network - NetKVM PCI ID was not found on system" ); 
	    endif; 
	     try
		 ret = RescanDevices();  
		 catch
		 	SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
		 endcatch; 
		 UnUseDLL(app);
	else
	     SprintfMsiLog ( "DisableDriver() : Did not disable Network - DevRule.dll could not be loaded " ); 
	
	endif;    
    SprintfMsiLog ( "DisableDriver() : End" ); 

end;
  
function EnableDriver(hMSI)   
    // To Do:  Declare local variables. 
    number n,n1,ret,nBuffer;   
    
     string sKey,s,app,key,val,szCmdLine;
begin             
     SprintfMsiLog ( "EnableDriver() : Start" ); 
  
	  nBuffer = 256;
	
	  sKey = "SOFTWARE\\Redhat\\UpgradeData" ;
	  RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ); 
	  ret = RegDBGetKeyValueEx(sKey, "UpgradeFlag",n ,s, n1 ); 
	  SprintfMsiLog ("%d = RegDBGetKeyValueEx(sKey, \"UpgradeFlag\",n,%s, n1 );",ret,s);
	  MsiGetProperty(hMSI,"ProductCode",key,nBuffer);
	  if !(s = key) then  
	      SprintfMsiLog("%s string is not = %s orig product key this is not the original remove skipping enable",s,key);
	      return 0;
	  endif;  
      MsiGetProperty(hMSI,"SUPPORTDIR",val,nBuffer);
     app = val ^ "DevRule.DLL";
     LongPathToShortPath(app); 
     SprintfMsiLog("Loading DevRule: %s", app ); 
     ret = UseDLL(app); 
     if (ret = 0) then
	    SprintfMsiLog ( "%d = UseDLL(%s);",ret,app ); 
       
        szCmdLine = "PCI\\VEN_5002&DEV_1234&SUBSYS_00000000&REV_00";  
        try 
		ret = DisableDevice(szCmdLine);
        SprintfMsiLog(" DisableDevice(%s) = %d ",szCmdLine, ret);
		catch
		 SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
		endcatch;   
        // PCI\\VEN_1AF4&DEV_1000&SUBSYS_00000000  
        
        try
        szCmdLine = "PCI\\VEN_1AF4&DEV_1000&SUBSYS_00000000";
        ret = DisableDevice(szCmdLine);
        SprintfMsiLog(" DisableDevice(%s) = %d ",szCmdLine, ret);
	    catch
		 SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
		endcatch;
        //PCI\\VEN_1AF4&DEV_1000&SUBSYS_00010000 
        szCmdLine = "PCI\\VEN_1AF4&DEV_1000&SUBSYS_00010000";  
        try
        ret = DisableDevice(szCmdLine);
        SprintfMsiLog(" DisableDevice(%s) = %d ",szCmdLine, ret);
	    catch
		 SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
		endcatch;
         //PCI\\VEN_1AF4&DEV_1000&SUBSYS_00011AF4 
    
         szCmdLine = "PCI\\VEN_1AF4&DEV_1000&SUBSYS_00011AF4\"";   
         try
         ret = DisableDevice(szCmdLine);
         SprintfMsiLog(" DisableDevice(%s) = %d ",szCmdLine, ret);
	     catch
		 	SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
		 endcatch;

	     ///ENABLE 
	     try
	     szCmdLine = "PCI\\VEN_10EC&DEV_8139&SUBSYS_00000000&REV_20"; 
	 	 ret = EnableDevice(szCmdLine); 
	 	 SprintfMsiLog(" EnableDevice(%s) = %d ",szCmdLine,ret );  
	 	 catch
		 	SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
		 endcatch;
	     szCmdLine = "PCI\\VEN_8086&DEV_100E&SUBSYS_00000000&REV_03"; 
	     try
		 ret = EnableDevice(szCmdLine); 
	 	 SprintfMsiLog(" EnableDevice(%s) = %d ",szCmdLine, ret);  
	 	 catch
		 	SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
		 endcatch;
		//PCI\\VEN_10EC&DEV_8139&SUBSYS_11001AF4&REV_20\ 
		 szCmdLine = "PCI\\VEN_10EC&DEV_8139&SUBSYS_11001AF4&REV_20";
		 try
	     ret = EnableDevice(szCmdLine); 
	 	 SprintfMsiLog(" EnableDevice(%s) = %d ",szCmdLine, ret);
	 	 catch
		 	SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
		 endcatch;  
		 
		 szCmdLine = "PCI\\VEN_8086&DEV_100E&SUBSYS_11001AF4&REV_03";  
		 try
	     ret = EnableDevice(szCmdLine); 
	 	 SprintfMsiLog(" EnableDevice(%s) = %d ",szCmdLine, ret);
	 	 catch
		 	SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
		 endcatch;  
		
		 try
		 ret = RescanDevices();  
		 catch
		 	SprintfMsiLog ( "Error %s   %d ",Err.Description,Err.Number );  
		 endcatch; 
		 UnUseDLL(app);
	else
	     SprintfMsiLog ( "EnableDevice() : Did not disable Network - DevRule.dll could not be loaded " ); 

	endif; 	
	 
     SprintfMsiLog ( "EnableDriver() : End" );
     
	
end;     



function SetW2K(hMSI)
      BOOL   gbIsOSWin2000 ;
begin 
	if (SYSINFO.nISOSL = ISOSL_WIN2000) then
	    gbIsOSWin2000 = TRUE;  
	    MsiSetProperty( ISMSI_HANDLE, "WXP", "0" ); 
	  //  %WXP = "0" ;          
	   //MessageBox ("Exist ",INFORMATION);
	else 
	  MsiSetProperty( ISMSI_HANDLE, "WXP", "1" ); 
	  
	   
	endif;
    
    
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="ParaVirtualized Ethernet Adapter";
     
     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 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;  
    number ret;
    
begin             

    // To Do:  Write script that will be executed when MyFunction is called. 
   ProductKey = "Installer\\Products";
   OldKey = "9BDEA0108918EF843333000000104814"; 
   RegDBSetDefaultRoot ( HKEY_CLASSES_ROOT ) ;  
   ret = RegDBDeleteKey ( ProductKey ^ OldKey);  
   SprintfMsiLog ( " %d = RegDBDeleteKey ( %s ^ %s); ",ret, 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  = "501" ) then   
       SprintfMsiLog ( "Platform is XP os id %s",val ); 
       MYOS = "XP";
   endif;    
   if (val  = "502" ) then 
       SprintfMsiLog ( "Platform is 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 2008R2/W7 os id %s" ,val );
      MsiGetProperty(hMSI,"MsiNTProductType",val,nBuffer);  
      SprintfMsiLog ( "Platform is 2008R2/W7 MsiNTProductType %s" ,val );  
      // MessageBox(val,1);
      if  ( val = "1") then 
        SprintfMsiLog ( "Platform is W7 " );
       	MYOS = "W7";
      else  
         SprintfMsiLog ( "Platform is 2008R2 " );
        MYOS = "W7";
      endif;   	
   endif;
   
   if (val = "602") then
   		SprintfMsiLog ( "Platform is W8 os id %s" ,val );
   		MYOS = "W8";
   endif;
   
   if (MYOS = "") then
		SprintfMsiLog ( "Unable to detect supported OS. Terminating.");
		abort;
   endif;
   
 //  MessageBox(MYOS,1);    
     MsiSetProperty(hMSI,"MYOS",MYOS);          
     SprintfMsiLog ( "End SetPlatformProperty  MYOS = %s ",MYOS ); 
     
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);

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;   
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;  

export prototype  SetUpgradeFlag(HWND);

function  SetUpgradeFlag(hMSI)

     number ret,nBuffer;   
    
     string sKey,key,app,val,szCmdLine;
begin    
     nBuffer = 256;         
     SprintfMsiLog ( "SetUpgradeFlag() : Start" ); 
     MsiGetProperty(hMSI,"ProductCode",key,nBuffer);  
    
	 sKey = "SOFTWARE\\Redhat\\UpgradeData" ;
	 RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ); 
	 ret = RegDBSetKeyValueEx(sKey, "UpgradeFlag",REGDB_STRING , key, -1 );
	 SprintfMsiLog ( "SetUpgradeFlag() : %d = RegDBSetKeyValueEx(%s, UpgradeFlag,REGDB_STRING , %s, -1 ); ",ret ,sKey,key); 


end;
  
    
   


