
////////////////////////////////////////////////////////////////////////////////
//                                                                            
//  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 Init(HWND);
   
    export prototype RunUSBInstall(HWND);  
    export prototype RunUSBUninstall (HWND);  
    export prototype AddPortException(HWND);
    export prototype RemovePortException(HWND);   
    export prototype ChangeDisplayName( HWND);    
   	export prototype BOOL IsSpesificServiceExists(string) ; 
   	export prototype SetDependencies(HWND);
   	export prototype UnSetDependencies(HWND);    
   	 export prototype SetPlatformProperty( 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 RunUSBInstall(hMSI)   
     number ret,nBuffer;   
     string szProgram,val,szCmdLine,sret;
begin             
   nBuffer = 256;    
  MsiGetProperty(hMSI,"CustomActionData",val,nBuffer); 
  szProgram  = val ^ "usbrdr_oem_install.exe ";  
   szCmdLine ="";                           
  ret = LaunchAppAndWait (szProgram,szCmdLine, LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN );
  ret = LAAW_PARAMETERS.nLaunchResult;
  SprintfMsiLog("%d = LaunchAppAndWait (%s,%s, LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN );" ,ret ,szProgram,szCmdLine);
  //ret = LAAW_PARAMETERS.nLaunchResult;
   NumToStr( sret,ret);
 //  MessageBox (sret,1);
  // NumToStr(TimerStr,INSTALLER_TIMER_GLOBAL_VAR);   
    WriteToLogFile("RunUSBInstall :szCmdLine =" + sret +"",1,1);
end;  

function RunUSBUninstall(hMSI)   
	number ret,nBuffer;                       
	string szProgram,val,szCmdLine,sret;
begin             
	//nBuffer = 256;    
	//MsiGetProperty(hMSI,"CustomActionData",val,nBuffer); 
	szProgram  = INSTALLDIR ^"usbrdr_oem_uninstall.exe";   
	//MessageBox ( szProgram,1);    
	//LaunchAppAndWait ("cmd.exe","/c echo " + szProgram +"  > C:\\setup.txt", LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN );

	szCmdLine ="";                            
	LaunchAppAndWait (szProgram,szCmdLine, LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN );
	ret = LAAW_PARAMETERS.nLaunchResult;
	//MessageBox (FormatMessage(ret),1);
	  SprintfMsiLog("%d = LaunchAppAndWait (%s,%s, LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN );" ,ret ,szProgram,szCmdLine);
   NumToStr( sret,ret);
 //  MessageBox (sret,1);
  // NumToStr(TimerStr,INSTALLER_TIMER_GLOBAL_VAR);   
    WriteToLogFile("RunUSBUnInstall :szCmdLine =" + sret +"",1,1);
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 ;
        //if(DEBUG_MODE = 1) then  
       //  if (returnValue = 1) then
            // sSucsess = "SUCCESS";
         //else
         //   sSucsess = "FAIL"; 
         //NumToStr(sSucsess,returnValue); 
          //sSucsess =  FormatMessage ( returnValue );
        // endif;    
	     FileInsertLine (LOG_FILE_GLOBAL_VAR, LogFileLine + " Status: " + sSucsess, LOG_FILE_LINE_NUMBER_GLOBAL_VAR, AFTER);  
	     LOG_FILE_LINE_NUMBER_GLOBAL_VAR++;  
	  // endif;
  end;
  
  function AddPortException(hMSI)
      string   szProgram,szCmdLine;  
      number ret;
  begin
      // netsh firewall set portopening TCP 32023 "USB Redirector"
        szProgram =  "cmd.exe";
        szCmdLine =  "/c netsh firewall set portopening TCP 32023 \"USB Redirector\"";

        LaunchAppAndWait (szProgram,szCmdLine, LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN );
        ret = LAAW_PARAMETERS.nLaunchResult;   

        WriteToLogFile( "LaunchAppAndWait (" + szProgram+ "," + szCmdLine+ ",  LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN )",ret,1);
  end; 
  
function RemovePortException(hMSI)
      string   szProgram,szCmdLine;  
      number ret;
begin
      // netsh firewall set portopening TCP 32023 "USB Redirector"
        szProgram =  "cmd.exe";
        szCmdLine =  "/c netsh firewall delete portopening TCP 32023";

        LaunchAppAndWait (szProgram,szCmdLine, LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN );
        ret = LAAW_PARAMETERS.nLaunchResult;   

        WriteToLogFile( "LaunchAppAndWait (" + szProgram+ "," + szCmdLine+ ",  LAAW_OPTION_WAIT |LAAW_OPTION_HIDDEN )",ret,1);
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 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;  

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  FixAddRemoveForClient(HWND);
function FixAddRemoveForClient(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 , "0", -1 ); 

end; 

function BOOL IsSpesificServiceExists(ServicName)
   number ret, nvSize, nvType;  
   string szKey, szName, sStart;
begin
      ret = ServiceExistsService( ServicName);  
      if !(ret) then
          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 SetDependencies(hMSI)
    string DRIVER_SERVICE_NAME;
    number ret;
begin
       DRIVER_SERVICE_NAME =  "RHEV-Agent";
	     if ( IsSpesificServiceExists("TermService")) then
	       	  //ret = LaunchAppAndWait   ("sc.exe","config " + DRIVER_SERVICE_NAME+ "  start= auto depend= TermService" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	
	       	  SprintfMsiLog ( "TermService exist and not disabled");
	       	  if (IsSpesificServiceExists("RHEV-Agent")) then  
	       	       SprintfMsiLog ( "TermService exists and not disabled AND RHEV-Agent exists and not disabled");
	       	       ret = LaunchAppAndWait   ("sc.exe","config " + DRIVER_SERVICE_NAME+ "  start= auto depend= spiceusbredirector/TermService" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	
              else 
                   SprintfMsiLog ( "TermService exists and not disabled BUT RHEV-Agent does not exists OR it is disabled - NO need to do anything");
	       	    
                  
	       	  endif;
	     else 
	          if (IsSpesificServiceExists("RHEV-Agent")) then 
	               SprintfMsiLog ( "RHEV-Agent exists and not disabled BUT TermService does not exists OR it is disabled");
	       	    
	       	       ret = LaunchAppAndWait   ("sc.exe","config " + DRIVER_SERVICE_NAME+ "  start= auto depend= spiceusbredirector" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	
	       	  else 
	       	       SprintfMsiLog ( "RHEV-Agent does not exists OR it is disabled AND TermService does not exists OR it is disabled - NO need to do anything");
	       	    
	       	       //ret = LaunchAppAndWait   ("sc.exe","config " + DRIVER_SERVICE_NAME+ "  start= auto" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	    
	       	  endif;
	     	  
	   	 endif;
	   

end;       


function UnSetDependencies(hMSI)

   string command,DRIVER_SERVICE_NAME;
    number ret;
begin
       DRIVER_SERVICE_NAME =  "RHEV-Agent";
	     if ( IsSpesificServiceExists("TermService")) then
	       	  //ret = LaunchAppAndWait   ("sc.exe","config " + DRIVER_SERVICE_NAME+ "  start= auto depend= TermService" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	
	       	  SprintfMsiLog ( "TermService exist and not disabled");
	       	  if (IsSpesificServiceExists("RHEV-Agent")) then  
	       	       SprintfMsiLog ( "TermService exists and not disabled AND RHEV-Agent exists and not disabled");
	       	       ret = LaunchAppAndWait   ("sc.exe","config " + DRIVER_SERVICE_NAME+ "  start= auto depend= TermService" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	
              else 
                   SprintfMsiLog ( "TermService exists and not disabled BUT RHEV-Agent does not exists OR it is disabled - NO need to do anything");
	       	    
                  
	       	  endif;
	     else 
	          if (IsSpesificServiceExists("RHEV-Agent")) then 
	               SprintfMsiLog ( "RHEV-Agent exists and not disabled BUT TermService does not exists OR it is disabled");
	       	       command = "/c sc config " + DRIVER_SERVICE_NAME + " start= auto depend= \"\"  " ;
	       	       //ret = LaunchAppAndWait   ("sc.exe","config " + DRIVER_SERVICE_NAME+ "  start= auto depend= \"\"" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);
	       	       ret = LaunchAppAndWait   ("cmd.exe",command,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);		
	       	       SprintfMsiLog ( "%s",command);
	       	  else 
	       	       SprintfMsiLog ( "RHEV-Agent does not exists OR it is disabled AND TermService does not exists OR it is disabled - NO need to do anything");
	       	    
	       	       //ret = LaunchAppAndWait   ("sc.exe","config " + DRIVER_SERVICE_NAME+ "  start= auto" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);	    
	       	  endif;
	     	  
	   	 endif;
	   

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

   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 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 2008 " );
        MYOS = "2008";
      endif;   	
   endif;
 //  MessageBox(MYOS,1);    
     MsiSetProperty(hMSI,"MYOS",MYOS);          
     SprintfMsiLog ( "End SetPlatformProperty  MYOS = %s ",MYOS ); 
     
     
   MsiGetProperty(hMSI,"VersionNT64",val,nBuffer);  
   if ((val = "") || (val = "0")) then
   		 
   	    MsiSetProperty(hMSI,"MYBIT","32");
   else
        MsiSetProperty(hMSI,"MYBIT","64");
   endif;
end;  

  
   
  

  
       

  
  