
////////////////////////////////////////////////////////////////////////////////
//                                                                            
//  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" 
    prototype     lockvol.lock_viostor(); 
    prototype     lockvol.unlock_viostor(); 

    // 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  FindOemFile  (HWND);   
	export prototype  DriverNeedReboot (HWND);   
	export prototype RemoveOldInstallerFromAddRemove(HWND);   
	export prototype ChangeDisplayName( HWND);    
	export prototype SetPlatformProperty( HWND);
    export prototype LockVol( HWND);  
    export prototype UnLockVol( 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 LockVol(hMSI)
   string myinstdir,mydll;
   number ret;    
   number nBuf;
begin   
   nBuf = 255;
   ret = MsiGetProperty(hMSI,"SUPPORTDIR",myinstdir,nBuf);
   mydll=myinstdir ^"lockvol.dll";
   ret = UseDLL (mydll);   
   SprintfMsiLog ( "LockVol %d = UseDLL (%s);" ,ret,mydll); 
   
   lock_viostor();  
   SprintfMsiLog ( "LockVol lock_viostor();" ,ret,mydll); 
   ret = UnUseDLL (mydll);   
   SprintfMsiLog ( "LockVol %d = UnUseDLL (%s);" ,ret,mydll); 
 
end;

function UnLockVol(hMSI)
   string myinstdir,mydll;
   number ret;    
   number nBuf;
begin   
   nBuf = 255;
   ret = MsiGetProperty(hMSI,"SUPPORTDIR",myinstdir,nBuf);
   mydll=myinstdir ^"lockvol.dll";
   ret = UseDLL (mydll);   
   SprintfMsiLog ( "UnLockVol %d = UseDLL (%s);" ,ret,mydll); 
   
   unlock_viostor();  
   SprintfMsiLog ( "UnLockVol unlock_viostor();" ,ret,mydll); 
   ret = UnUseDLL (mydll);   
   SprintfMsiLog ( "UnLockVol %d = UnUseDLL (%s);" ,ret,mydll); 
 
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="CopyFiles=viostor_Files_Driver";
     
     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 Init(hMSI)    
		  number  nvFileHandle;  
		  string  LogFileName;
		  string val; 
		  number ret,nBuffer;
  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; 
		     MsiSetProperty( ISMSI_HANDLE, "LOG_FILE_LINE_NUMBER_GLOBAL_VAR", "0" );
	         MsiSetProperty( ISMSI_HANDLE, "LOG_FILE_GLOBAL_VAR", FOLDER_TEMP^LogFileName );
	        
	         
  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;
  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" ); 

   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 = "W7";
      endif;   	
   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; 
    
  

  


  
  
 