////////////////////////////////////////////////////////////////////////////////
//                                                                            
//  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"

    // The keyword export identifies MyFunction() as an entry-point function.
    // The argument it accepts must be a handle to the Installer database.
    export prototype SetDriverNeedToReboot(HWND);  
    export prototype DriverNeedReboot (HWND);   
    export prototype RemoveOldInstallerFromAddRemove(HWND);  
    export prototype ChangeDisplayName( HWND); 
    export prototype ChainGinaSOO( HWND);  
    export prototype UnChainGinaSOO( HWND);  
    export prototype RegisterTool(string,string,string);        
    export prototype SetPlatformProperty( HWND);   
    export prototype GetOldProductCode(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).
//                                                                           
///////////////////////////////////////////////////////////////////////////////
export prototype SetSASGeneration(HWND); 
export prototype UnsetSASGeneration(HWND);
export prototype SetSASGenerationForUpgrade(HWND); 
export prototype SetSASGenerationLogic(HWND); 


function SetSASGenerationLogic(hMSI)
	string MicrosoftKey, szPath, szPath2, szKey, szKey2, szVal,szUser,szUserSID,MYOS;
	number ret, nvSize, nvType,nBuffer;
	LIST lstRes;
begin	
		
	Disable(LOGGING);
	
	RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ;
	//reset DisableCAD if it was set before             
	MicrosoftKey = "Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon";
    ret = RegDBSetKeyValueEx(MicrosoftKey, "DisableCAD",REGDB_NUMBER , "0", -1 ); 
	SprintfMsiLog("%d = RegDBSetKeyValueEx(%s, \"DisableCAD\",REGDB_STRING , %s, -1 );",ret,MicrosoftKey,"0");
	
	MicrosoftKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
    szKey = "SoftwareSASGeneration";
	szVal = "1";
    ret = RegDBSetKeyValueEx(MicrosoftKey, szKey,REGDB_NUMBER , szVal, -1 ); 
	SprintfMsiLog("%d = RegDBSetKeyValueEx(%s, %s,REGDB_STRING , %s, -1 );",ret,MicrosoftKey,szKey,szVal);
		 
	Enable(LOGGING);
end;

function SetSASGenerationForUpgrade(hMSI)
	string sUpgrade,MicrosoftKey, szPath, szPath2, szKey, szKey2, szVal,szUser,szUserSID,MYOS;
	number ret, nvSize, nvType,nBuffer;
	LIST lstRes;
begin	 

    nBuffer = MAX_PATH + 1;    
    MsiGetProperty (ISMSI_HANDLE, "MYOS", MYOS, nBuffer);
	if(MYOS != "W7") then
		SprintfMsiLog ( "SetSASGenerationForUpgrade: platform is not W7 - %s ",MYOS);
		return 0;
	endif; 
	
    MsiGetProperty(hMSI, "IS_MAJOR_UPGRADE", sUpgrade, nBuffer); 
    SprintfMsiLog("SetSASGenerationForUpgrade - IS_MAJOR_UPGRADE %s - re setting SoftwareSASGeneration to 1 ",sUpgrade);
    
    if (sUpgrade = "Yes") then
		SetSASGenerationLogic (hMSI);
	endif;	
end;


function SetSASGeneration(hMSI)
	string MicrosoftKey, szPath, szPath2, szKey, szKey2, szVal,szUser,szUserSID,MYOS;
	number ret, nvSize, nvType,nBuffer;
	LIST lstRes;
begin	
	nBuffer = 10;
	MsiGetProperty (ISMSI_HANDLE, "MYOS", MYOS, nBuffer);
	if(MYOS != "W7") then
		SprintfMsiLog ( "SetSASGeneration: platform is not W7 - %s ",MYOS);
		return 0;
	endif; 
	
	SetSASGenerationLogic (hMSI);
end;

function UnsetSASGeneration(hMSI)
	string MicrosoftKey, szPath, szKey, szPath2, szKey2, szVal,szUser,szUserSID,MYOS;
	number ret, nvSize, nvType,nBuffer;
	LIST lstRes;
begin
	nBuffer = 10;
	MsiGetProperty (ISMSI_HANDLE, "MYOS", MYOS, nBuffer);
	if(MYOS != "W7") then
		SprintfMsiLog ( "SetSASGeneration: platform is not W7 - %s ",MYOS);
		return 0;
	endif; 
	
	Disable(LOGGING);
	
	RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ;
	MicrosoftKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
    szKey = "SoftwareSASGeneration";
	RegDBDeleteValue(MicrosoftKey,szKey); 
	SprintfMsiLog("%d = RegDBDeleteValue(%s, %s);",ret,MicrosoftKey,szKey);
		 
	Enable(LOGGING);
end;

function SetDriverNeedToReboot(hMSI)   
       NUMBER  ret ,nBuffer ; 
          string szKey ,DoINeedReboot;
begin             
         MsiSetProperty(ISMSI_HANDLE, "ISSCHEDULEREBOOT", "1");
       	 // MsiGetProperty (ISMSI_HANDLE, "ISSCHEDULEREBOOT", DoINeedReboot, nBuffer);
     	
     	//MessageBox ("This is a Test "+ DoINeedReboot,1);
       // MsiGetProperty (ISMSI_HANDLE, "ISSCHEDULEREBOOT", DoINeedReboot, nBuffer);
    // To Do:  Write script that will be executed when MyFunction is called.
     
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);
    
        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 = "9BDEA0108918EF843333000000104814"; 
   RegDBSetDefaultRoot ( HKEY_CLASSES_ROOT ) ;  
   RegDBDeleteKey ( ProductKey ^ OldKey); 
   
end;   
function ChangeDisplayName( hMSI)
   string OldKey,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, "OLDPRODUCTKEY", OldKey, nLength); 
   
   RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ; 
  // deleteing old entry   
   sKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" ^  OldKey;
   if !(OldKey = "") then 
    	if (RegDBKeyExist(sKey)= 1) then
  			ret = RegDBDeleteKey ( sKey) ;  
  			SprintfMsiLog(" %d = RegDBDeleteKey ( %s ); ",ret,sKey); 
  		endif;	
   endif;	
    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 ChainGinaSOO(hMSI)  
   string MicrosoftKey,RedHatKey,svValue,OrigGina,sUpgrade,strMYOS; 
   number nLength,nvType, nvSize,ret;
begin   
        
       nLength = MAX_PATH + 1; 

         
       OrigGina = ""; 
       SprintfMsiLog( "Enter ChainGinaSOO ");
       //SOFTWARE\\MICROSOFT\\Windows NT\\CurrentVersion\\Winlogon   
       MicrosoftKey = "SOFTWARE\\MICROSOFT\\Windows NT\\CurrentVersion\\Winlogon";
       RedHatKey = "SOFTWARE\\RedHat\\SSO";   
       
       MsiGetProperty(hMSI, "IS_MAJOR_UPGRADE", sUpgrade, nLength); 
       //MessageBox(sUpgrade,1); 
       nvSize = 50;
    MsiGetProperty(hMSI,"MYOS",strMYOS,nvSize);
    RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );
    
    if(strMYOS = "XP") then
       if  (sUpgrade = "Yes") then
           ret = RegDBSetKeyValueEx ( RedHatKey, "Upgrade", REGDB_STRING, "Yes", -1 ); 
       endif;
       ret = RegDBGetKeyValueEx ( MicrosoftKey, "GinaDLL", nvType, svValue, nvSize ); 
       SprintfMsiLog( "%d = RegDBGetKeyValueEx ( %s, \"GinaDLL\", %d, %s, %d ); ",ret,MicrosoftKey,nvType, svValue, nvSize);
       if (ret = 0 ) then
              
            SprintfMsiLog( "Found Orig GinaDLL  - %s", svValue);
            if (svValue = "GinaSSO.dll") then 
               SprintfMsiLog( " Orig GinaDLL  - GinaSSO.dll dont need to save OrigGina = \"\"");
            else 
               SprintfMsiLog( " Orig GinaDLL  - %s dont needed to be save OrigGina = %s",svValue,svValue);
               OrigGina = svValue;
               ret = RegDBSetKeyValueEx(RedHatKey, "OriginalCustomGina",REGDB_STRING , OrigGina, -1 ); 
               SprintfMsiLog("%d = RegDBSetKeyValueEx(%s, \"OriginalCustomGina\",REGDB_STRING , %s, -1 );",ret,RedHatKey,OrigGina);
            endif;   
       endif;     
       ret = RegDBSetKeyValueEx(MicrosoftKey, "GinaDLL",REGDB_STRING , "GinaSSO.dll", -1 ); 
       SprintfMsiLog("%d = RegDBSetKeyValueEx(%s, \"GinaDLL\",REGDB_STRING , \"GinaSSO.dll\", -1 );",ret,MicrosoftKey);
	 else
     	 //ret = RegDBSetKeyValueEx(MicrosoftKey, "DisableCAD",REGDB_NUMBER , "1", -1 ); 
	     //SprintfMsiLog("%d = RegDBSetKeyValueEx(%s, \"DisableCAD\",REGDB_STRING , %s, -1 );",ret,MicrosoftKey,"1");
     endif;   

end;


function UnChainGinaSOO(hMSI)
    string sUpgrade,MicrosoftKey,RedHatKey,svValue,strMYOS; 
    number nLength,nvType, nvSize,ret;
begin 
    MicrosoftKey = "SOFTWARE\\MICROSOFT\\Windows NT\\CurrentVersion\\Winlogon";
    RedHatKey = "SOFTWARE\\RedHat\\SSO"; 
     
    RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );  
    
    nvSize = 50;
    MsiGetProperty(hMSI,"MYOS",strMYOS,nvSize);
    
    if(strMYOS = "XP") then
	    //ret = RegDBSetKeyValueEx ( RedHatKey, "Upgrade", REGDB_STRING, "Yes", -1 ); 
	    ret = RegDBGetKeyValueEx ( RedHatKey, "Upgrade", nvType, svValue, nvSize );
	    SprintfMsiLog( "%d = RegDBGetKeyValueEx ( %s, \"Upgrade\", %d, %s, %d ); ",ret,RedHatKey,nvType, svValue, nvSize);
	    RegDBDeleteValue ( RedHatKey, "Upgrade" ); 
	
	    if (  svValue = "Yes") then   
	         SprintfMsiLog( "We are in upgrade dont unchain );");
	         
	
	         return 0;
	    endif;
	
	    nLength = MAX_PATH + 1; 
	
	    
	     ret = RegDBGetKeyValueEx ( RedHatKey, "OriginalCustomGina", nvType, svValue, nvSize ); 
	     SprintfMsiLog( "%d = RegDBGetKeyValueEx ( %s, \"OriginalCustomGina\", %d, %s, %d ); ",ret,RedHatKey,nvType, svValue, nvSize);
	     if (ret = 0 ) then
	        if (svValue = "") then  
	            ret = RegDBDeleteValue(MicrosoftKey,"GinaDLL");
	            SprintfMsiLog("%d = RegDBDeleteValue(%s, \"GinaDLL\");",ret,MicrosoftKey);
	        else
	           ret = RegDBSetKeyValueEx(MicrosoftKey, "GinaDLL",REGDB_STRING , svValue, -1 ); 
	           SprintfMsiLog("%d = RegDBSetKeyValueEx(%s, \"GinaDLL\",REGDB_STRING , %s, -1 );",ret,MicrosoftKey,svValue);
	        endif; 
	     else
	         ret = RegDBDeleteValue(MicrosoftKey,"GinaDLL");
	         SprintfMsiLog("%d = RegDBDeleteValue(%s, \"GinaDLL\");",ret,MicrosoftKey);
	   
	     endif;
     else
     	 //ret = RegDBSetKeyValueEx(MicrosoftKey, "DisableCAD",REGDB_NUMBER , "0", -1 ); 
	     //SprintfMsiLog("%d = RegDBSetKeyValueEx(%s, \"DisableCAD\",REGDB_STRING , %s, -1 );",ret,MicrosoftKey,"0");
     endif;   
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);
 // WriteToLogFile(MsgLog,ret,1);
   
   
  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);
 // WriteToLogFile(MsgLog,ret,1);

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;      
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 = "XP";
   endif; 
   
   if (val  = "600" ) then 
      SprintfMsiLog ( "Platform is Vista/2008 os id %s",val ); 
      MsiGetProperty(hMSI,"MsiNTProductType",val,nBuffer);  
      SprintfMsiLog ( "Platform is Vista/2008 MsiNTProductType %s" ,val );  

      if  ( val = "1") then 
       	SprintfMsiLog ( "Platform is Vista " );
       	MYOS = "W7";
      else  
        SprintfMsiLog ( "Platform is 2008 " );
        MYOS = "W7";
      endif;   	
   endif; 
   
   if (val  = "601" ) then    
   		SprintfMsiLog ( "Platform is W7 os id %s" ,val );
      	MYOS = "W7";
   endif;
 
   if (val = "602") then
   		SprintfMsiLog ( "Platform is W8 os id %s" ,val );
   		MYOS = "W7";
   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 GetOldProductCode(hMSI)
    string  Product_name,MsgLog,szKey, szName,szValue; 
    number nLength,ret,nType,nSize;
begin 
  nLength = MAX_PATH + 1;
  MsiGetProperty(hMSI, "ProductName", Product_name, nLength); 
  RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE ) ; 
  szKey = "SOFTWARE\\RedHat\\RHEV\\Tools\\" + Product_name; 
  szName = "Key" ;
  ret = RegDBGetKeyValueEx ( szKey, szName, nType, szValue, nSize ); 
  SprintfMsiLog(" %d = RegDBSetKeyValueEx ( %s, %s, %d, %s, %d ); ",ret,szKey, szName, nType, szValue, nSize);
  MsiSetProperty(hMSI, "OLDPRODUCTKEY", szValue);   
  //MessageBox(szValue,1);
end;


  
   
  

  
