
////////////////////////////////////////////////////////////////////////////////
//                                                                            
//  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  DriverNeedReboot (HWND);   
	export prototype RemoveOldInstallerFromAddRemove(HWND);   
	export prototype ChangeDisplayName( HWND);    
	export prototype SetPlatformProperty( HWND);
	export prototype InstallBalloonService(HWND);
	export prototype RemoveBalloonService(HWND);
  //  export prototype CloseWriteToLogFile (HWND);
 
   // export prototype DisableDriver(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 InstallBalloonService(hMSI)
	string Installparameter;
	number nBuffer,ret;
begin
        SprintfMsiLog ( "InstallBalloonService : start" );   
        nBuffer = 256;  
	    MsiGetProperty(hMSI,"CustomActionData",Installparameter,nBuffer);     
	    LongPathToShortPath(Installparameter);
	    SprintfMsiLog ( "InstallBalloonService ---- MsiGetProperty(hMSI,\"CustomActionData\",%s,nBuffer);",Installparameter );                                                                                                                
	    ret = LaunchAppAndWait("cmd.exe","/C "+Installparameter^"balloon\\blnsvr.exe -i" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);
	    ret = LAAW_PARAMETERS.nLaunchResult  ;
	    //ret = ServiceAddService(DRIVER_SERVICE_NAME,DRIVER_SERVICE_NAME,DRIVER_SERVICE_NAME,Installparameter^"balloon\\blnsvr.exe",TRUE,"");
	    SprintfMsiLog ( "%d = ServiceAddService ( %s\\balloon\\blnsvr.exe, TRUE,  );",ret,Installparameter  );
	   // LaunchAppAndWait("sc.exe","failure " + DRIVER_SERVICE_NAME+ "  reset= 100000  actions= restart/60000/restart/300000/no/300000" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);
	    ret = LaunchAppAndWait("cmd.exe"," /C sc.exe start " + DRIVER_SERVICE_NAME ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);
	    ret = LAAW_PARAMETERS.nLaunchResult;
	    SprintfMsiLog ( "%d = sc.exe start %s",ret,DRIVER_SERVICE_NAME);
	    SprintfMsiLog ( "InstallBalloonService : end" );                                                                                                                   
end;

function RemoveBalloonService(hMSI)
	number ret;
begin  

       /*"blnsvr.exe -s" - to stop the service;
"blnsvr.exe -u" - to uninstall it from the system.
*/
        SprintfMsiLog ( "RemoveBalloonService : start" );
        //ret = ServiceStopService(DRIVER_SERVICE_NAME);   
        
	   /*  ret = LaunchAppAndWait("cmd.exe","/C \""+INSTALLDIR^"balloon\\blnsvr.exe\" -s" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);
	    ret = LAAW_PARAMETERS.nLaunchResult  ; 
	    if (ret = 0) then
	    	SprintfMsiLog ( "%d = LaunchAppAndWait(cmd.exe /C \"%s\\balloon\\blnsvr.exe\" -s ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);",ret,INSTALLDIR  );
	    	SprintfMsiLog ( "Ballon service sttoped"  );
	    else
	        SprintfMsiLog ( "%d = LaunchAppAndWait(cmd.exe /C \"%s\\balloon\\blnsvr.exe\" -s ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);",ret,INSTALLDIR  );
	        ret = ServiceStopService(DRIVER_SERVICE_NAME);  
	       if (ret = 0)  then
	    		SprintfMsiLog ( "%d =  ServiceStopService(%s);",ret,DRIVER_SERVICE_NAME  );
	    		SprintfMsiLog ( "Ballon service sttoped"  );  
	    	else
	    		SprintfMsiLog ( "%d =  ServiceStopService(%s);",ret,DRIVER_SERVICE_NAME  );
	    		SprintfMsiLog ( "%s service failed to stop setup abort",DRIVER_SERVICE_NAME  ); 
	    		//return -1;
	    	endif;	
	    endif;	     */
	    
	    ret = LaunchAppAndWait("cmd.exe","/C \""+INSTALLDIR^"balloon\\blnsvr.exe\" -u" ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);
	    ret = LAAW_PARAMETERS.nLaunchResult  ; 
	    SprintfMsiLog ( "%d = LaunchAppAndWait(cmd.exe /C \"%s\\balloon\\blnsvr.exe\" -u ,  LAAW_OPTION_HIDDEN |LAAW_OPTION_WAIT);",ret,INSTALLDIR  );
	   
	    SprintfMsiLog ( "RemoveBalloonService : 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 ;
        //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 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 = "9BDEA0108918EF841111000000104814"; 
   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;
   ret = RegDBSetKeyValueEx(sKey, "DisplayName",REGDB_STRING , New_Display_name, -1 ); 
   RegisterTool(Product_name,Product_ver,Product_key);  
 
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 XP/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/2012 os id %s" ,val );
   		MYOS = "2012";
   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;   
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; 
    
  

  


  
  
 