::
:: Batch file to install CNA drivers.
::
@echo off
setlocal

:: Folders
set TOOLS=util

:: Applicaitons
set installer=bfa_installer.exe
set BNI_CONFIG_VBS=bni_config.vbs

:: options and variables
set params=
set install_op=INSTALL
set im_drv_used=true
set agent_only=false
set driver_only=false
set save_bni_cfg=true
set bni_cfg_saved=false
set dummy_vlan_create=false
set install_snmp=false
set snmp_only=false
set driver_type=

:: Parse command line arguments
:loop
if /I (%1)==(/?) goto _USAGE
if /I (%1)==(HELP) goto _USAGE

::
set valid_param=false
if /I (%1)==(INSTALL_OP) (
	set params=%params% %1=%2
	set install_op=%2
	set valid_param=true
)
if /I (%1)==(DRIVER_TYPE) (
rem	set params=%params% %1=%2
	set driver_type=%2
	set valid_param=true
)
if /I (%1)==(DRIVER_TYPE) if /I (%2)==(HBA) set im_drv_used=false

if /I (%1)==(FORCED_INSTALL) (
	set params=%params% %1=%2
	set valid_param=true
)
if /I (%1)==(SILENT_INSTALL) (
	set params=%params% %1=%2
	set valid_param=true
)
if /I (%1)==(LOG_FILE_PATH) (
	set params=%params% %1=%2
	set valid_param=true
)
if /I (%1)==(W2K8R2_HOTFIX) (
	set params=%params% %1=%2
	set valid_param=true
)
if /I (%1)==(W2K8_HOTFIX) (
	set params=%params% %1=%2
	set valid_param=true
)
if /I (%1)==(W2K3_HOTFIX) (
	set params=%params% %1=%2
	set valid_param=true
)
if /I (%1)==(FC_INF_PATH) (
	set params=%params% %1=%2
	set valid_param=true
)
if /I (%1)==(FCOE_INF_PATH) (
	set params=%params% %1=%2
	set valid_param=true
)
if /I (%1)==(BNAD_INF_PATH) (
	set params=%params% %1=%2
	set valid_param=true
)
if /I (%1)==(IM_INF_PATH) (
	set params=%params% %1=%2
	set valid_param=true
)
if /I (%1)==(OEM_TYPE) (
	set params=%params% %1=%2
	set valid_param=true
)
if /I (%1)==(AGENT_ONLY) (
	set agent_only=%2
	set valid_param=true
)
if /I (%1)==(DRIVER_ONLY) (
	set driver_only=%2
	set valid_param=true
)
if /I (%1)==(SNMP_ONLY) (
	set snmp_only=%2
    set install_snmp=true
	set valid_param=true
)
if /I (%1)==(SNMP) (
	set install_snmp=%2
	set valid_param=true
)
if /I (%1)==(SAVE_BNI_CONFIG) (
	set save_bni_cfg=%2
	set valid_param=true
)

if (%valid_param%)==(false) if not (%1)==() (
		ECHO.Invalid parameters %1 %2
		goto _USAGE
)

shift
shift
if not (%1)==() goto loop

:: goto _EXIT
:: Change to utils directory. Everything begins here.
pushd.
cd %TOOLS%

set install_status=0
:: Creating the BCU desktop icon whenever Agent is installed
if /I (%agent_only%)==(true) goto _INSTALL_BCU
if /I (%snmp_only%)==(true) goto _SNMP

	::::::::::::::::::::::::::::::::::
	:: Driver installations         ::
	::::::::::::::::::::::::::::::::::

	:: Check if the tools are present
	if not exist %installer%  (
		ECHO.
		ECHO.Missing %INSTALLER%
		ECHO.
		GOTO _EXIT
	)
	
	:: Check if this is W2K3.
	FOR /F " delims=[] tokens=1,2,3" %%i IN ('ver') DO set vers=%%j
	FOR /F "tokens=1,2" %%a IN ("%vers%") DO set osver=%%b

	if /i "%osver%" geq "6.0.0000" goto _vista
	echo.Windows 2003 detected.
	
	bfa_installer.exe install_op=version driver_type=cna | find "Currently">nul

	if (%ERRORLEVEL%) neq (1) (
		ECHO.Existing QLogic BR-series CNA drivers detected.
		ECHO.This driver package is incompatible with QLogic BR-series CNA drivers.
		ECHO.Please contact QLogic support for compatible driver package.
		ECHO.To use this driver package to manage FC HBAs,
		ECHO.please uninstall CNA drivers and retry installation.
		ECHO.Exiting the installation...
		goto _EXIT
	)

	if /i (%driver_type%)==() (
		ECHO.Setting the default driver type to HBA.
		set driver_type=HBA
	)

	if /i (%driver_type%)==(hba) goto _INSTALL_DRIVER

	ECHO.
	ECHO.This driver package does not yet support CNA adapters on W2K3; exiting installation...
	ECHO.
	goto _EXIT

:_vista
	if /i (%driver_type%)==(hba) goto _INSTALL_DRIVER

	if /i "%osver%" geq "6.2.0000" goto _INSTALL_DRIVER

	::check whether the BNI(IM) driver is already installed
	::by service query. If it is a fresh installation case
	::and not the upgrade scenario, then the service query will fail
	::so if sc query "bnip" fails,then dont go for creating dummy VLANS
	sc query "bnip" | find /i "bnip" > tmp1.txt 2>NUL
	if not ERRORLEVEL 1 (
		set dummy_vlan_create=true
	)
	del tmp1.txt 1>nul 2>nul

	:: If im_drv_used==true, save the bni configuration
	if /i (%PROCESSOR_ARCHITECTURE%)==(ia64) goto _INSTALL_DRIVER
	if /i (%im_drv_used%)==(false) goto _INSTALL_DRIVER
	if /i (%save_bni_cfg%)==(false) goto _INSTALL_DRIVER
	if /i (%install_op%)==(INSTALL) (
		ECHO.
		ECHO.Saving Intermediate Driver Configuration...
		ECHO.
		call cscript %BNI_CONFIG_VBS% /a:save
		
		IF ERRORLEVEL 1 (
			ECHO.Bni_ConfigSave = FALSE
		) ELSE (
			ECHO.Bni_ConfigSave = TRUE
			set bni_cfg_saved=true
		)
	)

:_INSTALL_DRIVER

	set params=%params% DRIVER_TYPE=%driver_type%

	:: Ignore the error as this is just a precautionary step.
	ECHO.
	ECHO.Stopping hcmagent ...
	ECHO.
       	call sc stop hcmagent

	ECHO.
	if /I (%install_op%)==(INSTALL)	ECHO.Installing the drivers...
	if /I (%install_op%)==(PREINSTALL)	ECHO.Pre-installing the drivers...
	if /I (%install_op%)==(UNINSTALL)	ECHO.Uninstalling the drivers...
	ECHO.
	echo.%installer% %params%
	%installer% %params%

	:: Check if this is a reboot required or device not found case.
	if (%ERRORLEVEL%)==(-2147021886) (
		set ERRORLEVEL=0
	) 
	if (%ERRORLEVEL%)==(-2146500085) (
		set ERRORLEVEL=0
	)

	set install_status=%ERRORLEVEL%

	if (%install_status%)==(0) if /I (%install_op%)==(UNINSTALL)	(
		del %SystemRoot%\system32\libdbfaj.dll
		del %SystemRoot%\syswow64\libdbfaj.dll >0 2>1
	)

	:: If the driver type is for CNA, then report no Network drivers for IA64
	if /I (%im_drv_used%)==(false) goto _INSTALL_BCU
	if /I (%install_op%)==(INSTALL) goto _INSTALL_BCU
	if /I (%PROCESSOR_ARCHITECTURE%)==(x86) goto _INSTALL_BCU
	if /I (%PROCESSOR_ARCHITECTURE%)==(AMD64) goto _INSTALL_BCU

	ECHO.
	ECHO.--------------------------------------------------------
	ECHO.No Ethernet/Intermediate drivers for Itanium platform...
	ECHO.--------------------------------------------------------
	ECHO.

:_INSTALL_BCU
	:: Update BCU shortcuts
	ECHO.
	echo.del %userprofile%\Desktop\bcu.lnk
	ECHO.
	del "%userprofile%\Desktop\bcu.lnk"

	if /I (%install_op%)==(INSTALL)	(
		call cscript setutil.vbs
	)

	if /I (%install_op%)==(PREINSTALL) (
        call cscript setutil.vbs
    )

	if /i (%install_op%)==(UNINSTALL) (
		ECHO.
		ECHO.#########################################################
		ECHO.#bcu shortcut has been removed from the Desktop.        #
		ECHO.#Can not roll back to previously installed bcu shortcut.#
		ECHO.#########################################################
		ECHO.
	)

if /I (%agent_only%)==(true) goto _AGENT
if /I (%driver_only%)==(true) goto _EXIT
if /I (%install_op%)==(CHKPREREQUISITE) goto _SNMP
if /I (%install_op%)==(VERSION) goto _EXIT
if /I (%install_op%)==(LOCATEINF) goto _EXIT

:_AGENT
if not %install_status%==0 goto _EXIT
	:: Agent installation actions

	for /F "tokens=*" %%A in ('cd') DO set hcm_agent_root=%%A

	set hcm_agent_root=%hcm_agent_root%\hbaagent

	if not exist "%hcm_agent_root%\bin\hcmagent.exe" (
		ECHO.
		ECHO. Missing agent files
		ECHO.
		goto _EXIT
	)

	set hcm_conf=%hcm_agent_root%\conf\abyss.conf

	:: Install agent
	if /i (%install_op%)==(INSTALL) (
		ECHO.
		ECHO. Deleting previously installed agent if any ...
		ECHO.
		call hbaagent\bin\hcmagent.exe -u >nul
		ECHO.
		ECHO.Installing agent...
		ECHO.
		ECHO Updating the configuration path in abyss.conf
		if exist "hbaagent\conf\win_abyss.conf" (
			move hbaagent\conf\win_abyss.conf hbaagent\conf\abyss.conf > nul
		)
		call hbaagent\bin\hcmagent.exe -r
		call hbaagent\bin\hcmagent.exe -i >nul
		echo.Install Status :%ERRORLEVEL%
		set install_status=%ERRORLEVEL%
		ECHO.
		ECHO.To start HCM Agent service
		ECHO.run "sc start hcmagent"
		ECHO.
		ECHO.To set HCM Agent service to automatically start on reboot.
		ECHO.run "sc config hcmagent start= delayed-auto"
		ECHO.
	)

	:: Pre-install agent
	if /i (%install_op%)==(PREINSTALL) (
		ECHO.
		ECHO.Pre-installing agent...
		call hbaagent\bin\hcmagent.exe -i >nul
		echo.Pre-install Status :%ERRORLEVEL%
		set install_status=%ERRORLEVEL%
		ECHO.
		ECHO.To start the QLogic BR-series HCM Agent service,
		ECHO.run "sc start hcmagent"
		ECHO.
	)

	:: Uninstall agent
	if /i (%install_op%)==(UNINSTALL) (
		ECHO.
		ECHO. Stopping the agent ...
		ECHO.
		call sc stop hcmagent >nul
		ECHO.
		ECHO.Uninstalling the agent...
		ECHO.
		call hbaagent\bin\hcmagent.exe -u >nul
		echo.Uninstall Status :%ERRORLEVEL%
		set install_status=%ERRORLEVEL%
		ECHO.
	)

:_SNMP
if /I (%install_snmp%)==(false) goto _EXIT

if /i (%install_op%)==(INSTALL) (
    call brcd_snmp_install.bat %install_op% 
    set install_status=%ERRORLEVEL%
)

if /i (%install_op%)==(CHKPREREQUISITE) (
    call brcd_snmp_install.bat %install_op% 
    set install_status=%ERRORLEVEL%
)

if /i (%install_op%)==(PREINSTALL) (
    call brcd_snmp_install.bat %install_op% 
    set install_status=%ERRORLEVEL%
)

if /i (%install_op%)==(UNINSTALL) (
    call brcd_snmp_install.bat %install_op% 
    set install_status=%ERRORLEVEL%
)

goto _EXIT

:_USAGE
ECHO.
ECHO.Usage:
ECHO.brocade_install.bat [INSTALL_OP={INSTALL/UNINSTALL/PREINSTALL}]
ECHO.	[DRIVER_TYPE={HBA/CNA/AUTO}][LOG_FILE_PATH={path to installer log}]
ECHO.	[FORCED_INSTALL={TRUE/FALSE}][SILENT_INSTALL={TRUE/FALSE}]
ECHO.	[SNMP={TRUE/FALSE}][SNMP_ONLY={TRUE/FALSE}]
ECHO.	[W2K8R2_HOTFIX={[""]/[{KBnnnnnn}:{Required/Optional}{Description}]]
ECHO.	[W2K8_HOTFIX={[""]/[{KBnnnnnn}:{Required/Optional}{Description}]]
ECHO.	[W2K3_HOTFIX={[""]/[{KBnnnnnn}:{Required/Optional}{Description}]]
ECHO.		
ECHO.INSTALL_OP: (Default: INSTALL)
ECHO.    INSTALL    : Installs all the drivers.
ECHO.                 To install specific driver, use this along with DRIVER_TYPE.
ECHO.    UNINSTALL  : Uninstalls all the drivers.
ECHO.                 To uninstall specific driver, use this along with DRIVER_TYPE.
ECHO.    PREINSTALL : Pre-installs all the drivers.
ECHO.                 To preinstall specific driver, use this along with DRIVER_TYPE.
ECHO.
ECHO.DRIVER_TYPE: (Default: AUTO)
ECHO.    HBA    : Performs install operations on QLogic BR-series HBA drivers only
ECHO.    CNA    : Performs install operations on QLogic BR-series CNA drivers only
ECHO.    AUTO   : Performs install operations on all QLogic BR-series adapter drivers
ECHO.
ECHO.LOG_FILE_PATH: (Default: bfa_install.log)
ECHO.    Full path to log file. The path must be quoted when it contains a space.
ECHO.    The user can specify system environmental variables for the path component.
ECHO.    Example: 
ECHO.    LOG_FILE_PATH="%%ProgramFiles%%\Brocade\Adapter\Driver\util"
ECHO.
ECHO.FORCED_INSTALL: (Default: FALSE; applies only for Installation)
ECHO.    TRUE  : Installs the driver even if an equal or better driver exists.
ECHO.    FALSE : Fails the installation if a driver with better rank is already
ECHO.            installed.
ECHO.
ECHO.SILENT_INSTALL: (Default: FALSE; only for installation and pre-installation)
ECHO.    TRUE  : Suppresses all the error recovery user prompts. Use this option if
ECHO.            non-interactive installation is required. 
ECHO.    FALSE : Prompts user if a recoverable error occurs. For example, if driver
ECHO.            file(.sys) is not found, user will be prompted to locate the same.
ECHO.
ECHO.SNMP: (Default: FALSE)
ECHO.    TRUE  : Performs install operations QLogic BR-series SNMP subagent along with the
ECHO.            drivers.
ECHO.    FALSE : Skips install operations on QLogic BR-series SNMP subagent.
ECHO.
ECHO.SNMP_ONLY: (Default: FALSE)
ECHO.    TRUE  : Performs install operations QLogic BR-series SNMP subagent only.
ECHO.            Should not be used with SNMP option.
ECHO.    FALSE : Skips install operations on QLogic BR-series SNMP subagent.
ECHO.
ECHO.
ECHO.W2K3_HOTFIX, W2K8_HOTFIX and W2K8R2_HOTFIX:
ECHO.    Default   : Recommended hotfix from QLogic
ECHO.    Blank     : Avoids hotfix check. Must be specified as ""
ECHO.    Non-blank : Overrides QLogic recommended hotfix. Specify in below format:
ECHO.                "KBnnnnnn:{Required/Optional}:Description".
ECHO.

:_EXIT
popd
endlocal
EXIT /b %install_status%
