
////////////////////////////////////////////////////////////////////////////////
//                                                                            
//   File Name:   Setup.rul                                                   
//                                                                            
//   Description: InstallShield script                                        
//                                                                            
//   Comments:    This script was generated based on the selections you made in
//                the Project Wizard.  Refer to the help topic entitled "Modify      
//                the script that the Project Wizard generates" for information
//                on possible next steps.
//
////////////////////////////////////////////////////////////////////////////////


// Include header files
    
#include "ifx.h"
#include "utils.h"
    
////////////////////// string defines ////////////////////////////

//////////////////// installation declarations ///////////////////

// ----- DLL function prototypes -----


    // your DLL function prototypes
    prototype BOOL Setupapi.SetupCopyOEMInfA( BYREF STRING, POINTER, LONG, LONG, BYREF STRING, LONG, POINTER, POINTER );
    prototype LONG Kernel.OpenEvent( LONG, BOOL, BYREF STRING );
    prototype BOOL Kernel.SetEvent( LONG );
    prototype BOOL Kernel.MoveFileExA( BYREF STRING, POINTER, LONG ); //2004-04-08

// ---- script function prototypes -----


    // your script function prototypes

    // your global variables
    typedef CUSTOMINFO
    begin
    	BOOL	bInitialized;
    	STRING	szTwainDir[MAX_PATH];
    	STRING	szDriverPackageDir[MAX_PATH];
    	STRING	szUninstParam[MAX_PATH];
    	STRING	szSoftRootKey[MAX_PATH];
    	STRING	szProductType[MAX_PATH];
    	STRING	szDriverServiceOld[MAX_PATH];
    	STRING	szDriverService[MAX_PATH];
    	STRING	szHardwareId[MAX_PATH];
    	STRING	szDefInfPath[MAX_PATH];
    	STRING	szUninstToolPath[MAX_PATH];
    end;
    
    CUSTOMINFO	gCustomInfo;


//////////////////////////////////////////////////////////////////////////////////////////
//                                                                           
//  FUNCTION:   OnFirstUIBefore                                            
//                                                                           
//  EVENT:      The OnFirstUIBefore event is called by OnShowUI when the setup is
//              running in first install mode. By default this event displays UI allowing
//              the end user to specify installation parameters.
//              
//              Note: This event will not be called automatically in a
//              program...endprogram style setup.                                                             
//
//////////////////////////////////////////////////////////////////////////////////////////
function OnFirstUIBefore()
    number  nResult;
    number  nLevel;
    number  nvSize, nSetupType;
    number  nLoop;
    string  szTitle, szMsg;
    string  szTargetPath;
    string  svDir;
    string  szComponents, szTargetdir;
    string  szTargetDirAppendix;  
begin	
    nSetupType = COMPLETE;

	InitializeCustomTarget();
	
    if ( ALLUSERS ) then   
        TARGETDIR = PROGRAMFILES ^ IFX_COMPANY_NAME ^ IFX_PRODUCT_NAME;
    else
        TARGETDIR = FOLDER_APPDATA ^ IFX_COMPANY_NAME ^ IFX_PRODUCT_NAME; 
    endif;

    // Customize the default TARGETDIR for multi-instance application.
    // TODO: If you want something different customize the code below.	
    if( MAINT_OPTION = MAINT_OPTION_MULTI_INSTANCE  && MULTI_INSTANCE_COUNT > 0) then
        nLoop = 1;
        svDir = TARGETDIR;
        while(ExistsDir(TARGETDIR) = EXISTS)			
            NumToStr(szTargetDirAppendix,nLoop);
            TARGETDIR = svDir + szTargetDirAppendix;
            nLoop = nLoop + 1;
        endwhile;						
    endif;

    svDir = TARGETDIR;

// Beginning of UI Sequence
Dlg_Start:
	Disable ( LOGGING );
	RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );
	RegDBCreateKeyEx ( "SYSTEM\\SONIX\\PCCAM" , "" ); 
	Enable ( LOGGING );

Dlg_SdWelcome:
    szTitle = "";
    szMsg = "";
    nResult = SdWelcome( szTitle, szMsg );
    if (nResult = BACK) goto Dlg_Start;

Dlg_SetupType2:   
    szTitle = "";
    szMsg = "";
    nResult = SetupType2( szTitle, szMsg, "", nSetupType, 0 );
    if (nResult = BACK) then
        goto Dlg_SdWelcome;
    else
        nSetupType = nResult;
        if (nSetupType != CUSTOM) then
            szTargetPath = TARGETDIR;
            nvSize = 0;
            ComponentCompareSizeRequired( MEDIA, szTargetPath, nvSize );
            if (nvSize != 0) then      
                MessageBox( szSdStr_NotEnoughSpace, WARNING );
                goto Dlg_SetupType2;
            endif;
        endif;   
    endif;

/*
Dlg_SdAskDestPath2:
    if ((nResult = BACK) && (nSetupType != CUSTOM)) goto Dlg_SetupType2;
    szTitle = "";
    szMsg = "";
    if (nSetupType = CUSTOM) then
        nResult = SdAskDestPath2( szTitle, szMsg, svDir );
        TARGETDIR = svDir;
    endif;
    if (nResult = BACK) goto Dlg_SetupType2;    
*/	

Dlg_SdComponentTree: 
    //if ((nResult = BACK) && (nSetupType != CUSTOM)) goto Dlg_SdAskDestPath2;
    if ((nResult = BACK) && (nSetupType != CUSTOM)) goto Dlg_SetupType2;
    szTitle = "";
    szMsg = "";
    szTargetdir = TARGETDIR;
    szComponents = "";
    nLevel = 2;
    if (nSetupType = CUSTOM) then
        //nResult = SdComponentTree( szTitle, szMsg, szTargetdir, szComponents, nLevel );
        nResult = SdComponentDialog2 ( szTitle , szMsg , szTargetdir , szComponents );
        //if (nResult = BACK) goto Dlg_SdAskDestPath2;  
        if (nResult = BACK) goto Dlg_SetupType2;  
    endif;

Dlg_ObjDialogs:
    nResult = ShowObjWizardPages( nResult );
    if (nResult = BACK) goto Dlg_SdComponentTree;   
    
Dlg_SdStartCopy2:
    szTitle = "";
    szMsg = "";
    nResult = SdStartCopy2( szTitle, szMsg );	
    if (nResult = BACK) goto Dlg_ObjDialogs;

    return 0;
end;
 
//---------------------------------------------------------------------------
// OnFirstUIAfter
//
// First Install UI Sequence - After Move Data
//
// The OnFirstUIAfter event called by OnShowUI after the file transfer
// of the setup when the setup is running in first install mode. By default
// this event displays UI that informs the end user that the setup has been
// completed successfully.
//
// Note: This event will not be called automatically in a
// program...endprogram style setup.
//---------------------------------------------------------------------------
function OnFirstUIAfter()
    STRING szTitle, szMsg1, szMsg2, szOpt1, szOpt2;
    NUMBER bvOpt1, bvOpt2;
begin

    ShowObjWizardPages(NEXT);
    
//++++++++++++++++++++++++++++++++++++++
Post_installation:
	PostInstallation();

Dlg_SdFinish:
//--------------------------------------
    szTitle = "";
    szMsg1 = ""; 
    szMsg2 = "";
    szOpt1 = "";
    szOpt2 = "";
	bvOpt1   = FALSE;
    bvOpt2   = FALSE;
    
    if ( BATCH_INSTALL ) then
    	SdFinishReboot ( szTitle , szMsg1 , SYS_BOOTMACHINE , szMsg2 , 0 );
    else
		
		// If the update service is enabled, show finish dialog that includes
		// update check option.
		if( ENABLED_ISERVICES & SERVICE_ISUPDATE ) then

			if( SdFinishUpdateEx( szTitle, szMsg1, szMsg2, szOpt1, szOpt2, TRUE ) ) then

				// Don't check for updates in silent mode.
				if( MODE != SILENTMODE ) then
					UpdateServiceCheckForUpdates( "", FALSE );
				endif;

			endif;

		else
			SdFinish ( szTitle , szMsg1 , szMsg2 , szOpt1 , szOpt2 , bvOpt1 , bvOpt2 );
		endif;

    endif;
end;
 
//---------------------------------------------------------------------------
// OnMaintUIBefore
//
// Maintenance UI Sequence - Before Move Data
//
// The OnMaintUIBefore event is called by OnShowUI when the setup is
// running in maintenance mode. By default this event displays UI that
// allows the end user to add or remove components, repair currently
// installed components or uninstall the application.
//
// Note: This event will not be called automatically in a
// program...endprogram style setup.
//---------------------------------------------------------------------------
function OnMaintUIBefore()
    number	nResult, nType, nMediaFlags;
    string	szTitle, szMsg, szIgnore;
begin
	
//++++++++++++++++++++++++++++++++++++++
	// nType defaults to MODIFY.
	//nType = MODIFY;
	// nType defaults to REMOVEALL.
	nType = REMOVEALL;
//--------------------------------------

// Beginning of UI Sequence
Dlg_Start:

	// Show Standard Maintenance Dialog
	Disable( BACKBUTTON );
	nType = SdWelcomeMaint( szTitle, szMsg, nType );
	Enable( BACKBUTTON );
    nResult = NEXT;

	// Show Uninstall Confirmation Dialog
    if ( nType = REMOVEALL ) then
		nResult = MessageBox( SdLoadString( IFX_MAINTUI_MSG ), MB_YESNO );
		if (nResult != IDYES ) then
			goto Dlg_Start;
		endif;
	endif;

Dlg_SdComponentTree:
	if ( nType = MODIFY ) then
		szTitle = "";
		szMsg = SdLoadString( SD_STR_COMPONENT_MAINT_MSG );
		nResult = SdComponentTree( szTitle, szMsg, TARGETDIR, "", -1 );
		if ( nResult = BACK ) goto Dlg_Start;
    endif;

Dlg_ObjDialogs:
    nResult = ShowObjWizardPages( nResult );
    if ( ( nResult = BACK ) && ( nType != MODIFY ) ) goto Dlg_Start;
    if ( ( nResult = BACK ) && ( nType = MODIFY ) ) goto Dlg_SdComponentTree;

	switch(nType)

        case REMOVEALL:
						
			// Ensure that all previously installed components are removed
			// for media that supports updating.
			MediaGetData( MEDIA, MEDIA_FIELD_MEDIA_FLAGS, nMediaFlags, szIgnore );
			
			if( nMediaFlags & MEDIA_FLAG_UPDATEMODE_SUPPORTED ) then
				ComponentRemoveAllInMediaAndLog();
			else
				ComponentRemoveAllInMedia();
			endif;

        case REPAIR:
			
			// Reinstall all previously installed components.
			ComponentReinstall();

    endswitch;
 
end;
 
//---------------------------------------------------------------------------
// OnShowUI
//
// This function drives the UI sequence and file transfer of the setup.
// 
// The OnShowUI event is called directly by the framework to initiate
// the UI sequence and file transfer of the setup. By default this event
// displays UI that informs the end user that the maintenance setup has been
// completed successfully.
// 
// Note: This event will not be called automatically in a
// program...endprogram style setup.
//---------------------------------------------------------------------------
function OnShowUI()
BOOL	bMaintenanceMode, bUpdateMode;
string	szIgnore, szTitle;
HWND	hMain;  
HWND	hViewer;
begin
		
		// Enable dialog caching
		Enable( DIALOGCACHE );
		
		// Determine what events to show.
		bUpdateMode	= FALSE;
		bMaintenanceMode = FALSE;
	
		// Remove this to disabled update mode.
		if( UPDATEMODE ) then
			bUpdateMode = TRUE;
		endif;

		// Remove this to disable maintenance mode.
		if ( MAINTENANCE ) then
			bMaintenanceMode = TRUE;
		endif;

		// Show appropriate UI

		// TODO: Enable if you want to enable background etc.
		if ( LoadStringFromStringTable( "TITLE_MAIN", szTitle ) < ISERR_SUCCESS ) then // Load the title string.
			szTitle = IFX_SETUP_TITLE;
		endif;
		//SetTitle( szTitle, 24, WHITE );
		//Enable( FULLWINDOWMODE );						   
		//Enable( BACKGROUND );
		//SetColor( BACKGROUND, RGB( 0, 128, 128 ) );

//++++++++++++++++++++++++++++++++++++++
		Sprintf ( szTitle , szTitle , IFX_PRODUCT_NAME, IFX_PRODUCT_VERSION );
		SetTitle( szTitle, 0, BACKGROUNDCAPTION ); 
		InitializeCustomInfo();

		// set window to top most		
		hMain = GetWindowHandle(HWND_INSTALL);
		SetWindowPos(hMain, -1, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
//--------------------------------------

		if( bUpdateMode ) then
			OnUpdateUIBefore();
		else
			if ( bMaintenanceMode ) then
				OnMaintUIBefore();
			else
				OnFirstUIBefore();
			endif;
		endif;

//++++++++++++++++++++++++++++++++++++++
		hViewer = FindWindow ( "" , "CameraMonitor(SN9C102)" );   
		SendMessage ( hViewer , WM_CLOSE , 0 , 0 ); 
	    RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );    
	    RegDBDeleteValue ( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run" , "SNPSTD" );
//--------------------------------------

		// Move Data
		OnMoveData();
		
		// Show appropriate UI after move data
		if( bUpdateMode ) then
			OnUpdateUIAfter();
		else
			if ( bMaintenanceMode ) then
				OnMaintUIAfter();
			else
				OnFirstUIAfter();
			endif;
		endif;

		// Disable dialog caching
		Disable(DIALOGCACHE);

end;
 
// --- include script file section ---
#include "ComponentEvents.rul"
#include "utils.rul"