@echo off
:: EnableDelayedExpansion
:: Setting EnabledDelayedExpansion will cause each variable to be expanded at execution time rather than at parse time.
Setlocal EnableDelayedExpansion

ECHO IM 7.6 Limited Release 4
ECHO.
ECHO Note1: This patch can be applied on HPE Insight Management 7.6 or later
ECHO.
ECHO Note2: Don't click or double click or interrupt command prompt while installing Limited Release
ECHO.

:: Creating LR1 Registry for controlling the multiple installations.
:: If user launch installation in multiple sessions, will block the installation using this registry entry.
:: While installing Limited Release if user relaunch the installation, User will get blocking message saying that your installation of this is already running.

:CHECKLRREGISTRY

rem echo Checking Limited Release Registry created or not.  
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\ALM\LR1" /v LRRunning >NUL 2>&1 
if %ERRORLEVEL%==0 (
	rem echo Limited Release Registry Installing process aborted
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\ALM\LR1" /v LRRunning') DO set LRRunning=%%B

) else (
	rem echo Limited Release registry not created.    
	rem echo Creating Limited Release Registry.       
	REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\ALM\LR1 /v "LRRunning" /t REG_SZ /d "Installing"  >NUL 2>&1
	        
)
if "!LRRunning!" EQU "Installing" goto LRINSTALLED

:: Creating Log file with time stamp 
:: Generating unique file name with timestamp
:: I use this to create a unique file name for the execution of the batch file

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
set mytime=%mytime: =0%

if not exist "%SystemDrive%\HPIC\logs\LimitedRelease4" mkdir "C:\HPIC\logs\LimitedRelease4"
set LogDirectory=%SystemDrive%\HPIC\logs\LimitedRelease4
set LogDirectory3=%SystemDrive%\HPIC\logs\LimitedRelease3
set LogDirectory2=%SystemDrive%\HPIC\logs\LimitedRelease2
set LogDirectory1=%SystemDrive%\HPIC\logs\LimitedRelease1

set LOGFILE=%SystemDrive%\HPIC\logs\LimitedRelease4\IMLR4_%mydate%_%mytime%.log
echo [%DATE% %TIME%] Log file folder created                                                      >> %LOGFILE%
echo [%DATE% %TIME%] Log file path: %LOGFILE%                                                     >> %LOGFILE%
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\ALM\Settings" /v "LRLogDirectory" /t REG_SZ /d "%LogDirectory%" /f   >NUL 2>&1
echo [%DATE% %TIME%] Created registry for LogFile path.                                           >> %LOGFILE%

:: Check for user access controls (UAC). If this script doesn't have full
:: Administrative rights, then the service management and file copy
:: operations may fail.

net localgroup Administrators | findstr %username%  >NUL 2>&1
echo [%DATE% %TIME% Current Loggedin UserName: %username%                                         >> %LOGFILE%
if %errorlevel% == 1 (
echo [%DATE% %TIME%] This is not an Admin account, installation terminated.                       >> %LOGFILE%
echo This is not an Admin account, installation terminated.
pause
EXIT /B %ERRORLEVEL%
goto :EOF
) else (
echo [%DATE% %TIME%] This is an Admin account, continuing with installation.                      >> %LOGFILE%
echo This is an Admin account, continuing with installation.
echo.
goto :CHECKDVDVERSION
)

:: This Label checks the current version of the Insight Management
:: If user found minimum supported version of the Insight Management, installation going forward other wise installation blocks her.
 
:CHECKDVDVERSION

REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\ALM\Settings" /v Version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] Insight Management installed on the CMS.                >> %LOGFILE%          
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\ALM\Settings" /v Version') DO (set IM_VERSION=%%B) 
	echo [%DATE% %TIME%] Found Insight Management version !IM_VERSION!           >> %LOGFILE%
) else (
	echo [%DATE% %TIME%] HPE Insight Management is not present on the CMS.        >> %LOGFILE%                          
)

if "!IM_VERSION!" NEQ "7.6.0" goto DVDVERSIONNOTFOUND

:: READING COMPONENTS REGISTRIES:
:: Checking whether Component installed or not
:: Reading Components Current Versions from registries 
:: If component installed it will display the current version of the Component
rem echo Checking for installed Insight Management components.
rem echo [%DATE% %TIME%] Checking for installed Insight Management components.       >> %LOGFILE%

:: Checking OpenSSH version
echo [%DATE% %TIME%] Checking OpenSSH registry present on the system or not.     >> %LOGFILE%
set OpenSSHVERCOMP=7.9p1-1
REG QUERY "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\OpenSSH Services_is1" /v DisplayVersion >NUL 2>&1
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\OpenSSH Services_is1" /v DisplayVersion') DO set OpenSSHVersion=%%B
	echo [%DATE% %TIME%] OpenSSH Service Version !OpenSSHVersion!	             >> %LOGFILE%
) else (
    echo [%DATE% %TIME%] OpenSSH Service is not installed                        >> %LOGFILE%
)

ECHO [%DATE% %TIME%] Reading Component's Registries:                             >> %LOGFILE%
set SIMVERCOMP=C.07.06.00 
echo [%DATE% %TIME%] Checking SIM Registry present on the system or not.         >> %LOGFILE%
REG QUERY "HKLM\Software\Hewlett-Packard\Systems Insight Manager\Settings" /v Version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] SIM Registry Found                                      >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Hewlett-Packard\Systems Insight Manager\Settings" /v Version') DO set SIM_VERSION=%%B 
	echo [%DATE% %TIME%] !SIM_VERSION!                                           >> %LOGFILE%	
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking VCEM Registry present on the system or not.        >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\Virtual Connect Enterprise Manager\Settings" /v Version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] VCEM Registry Found                                     >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\Virtual Connect Enterprise Manager\Settings" /v Version') DO set VCEM_VERSION=%%B
	echo [%DATE% %TIME%] !VCEM_VERSION:~0,-3!                                    >> %LOGFILE%
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking SMP Registry present on the system or not.         >> %LOGFILE%
REG QUERY "HKLM\Software\Wow6432Node\HP\HP Server Migration Pack" /v Version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] SMP Registry Found                                      >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Wow6432Node\HP\HP Server Migration Pack" /v Version') DO set SMP_VERSION=%%B
	echo [%DATE% %TIME%] !SMP_VERSION!                                           >> %LOGFILE%
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking PMP Registry present on the system or not.         >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\HP\Insight Control performance management" /v Version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] PMP Registry Found                                      >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\HP\Insight Control performance management" /v Version') DO set PMP_VERSION=%%B
	echo [%DATE% %TIME%] !PMP_VERSION!	                                         >> %LOGFILE%
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking IPM Registry present on the system or not.         >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\IPM\Settings" /v Version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] IPM Registry Found                                      >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\IPM\Settings" /v Version') DO set IPM_VERSION=%%B 
	rem echo [%DATE% %TIME%] !IPM_VERSION!	                                        >> %LOGFILE%
	echo [%DATE% %TIME%] !IPM_VERSION:~0,-1!                                     >> %LOGFILE%
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking VMM Registry present on the system or not.         >> %LOGFILE%  
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\HP\Insight Control virtual machine management" /v Version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] VMM Registry Found                                      >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\HP\Insight Control virtual machine management" /v Version') DO set VMM_VERSION=%%B
	echo [%DATE% %TIME%] !VMM_VERSION!	                                         >> %LOGFILE%
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking MatrixIO Registry present on the system or not.    >> %LOGFILE%
REG QUERY "HKLM\Software\Hewlett-Packard\Insight Orchestration" /v version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] MatrixIO Registry Found                                 >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Hewlett-Packard\Insight Orchestration" /v version') DO set IO_VERSION=%%B 
	echo [%DATE% %TIME%] !IO_VERSION:~0,-1!                                      >> %LOGFILE%
) else (
   echo [%DATE% %TIME%] Component not installed                                  >> %LOGFILE% 
)

echo [%DATE% %TIME%] Checking MatrixOE Registry present on the system or not.    >> %LOGFILE%  
REG QUERY "HKLM\Software\Hewlett-Packard\Virtual Server Environment" /v CurrentVersion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] MatrixOE Registry Found                                 >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Hewlett-Packard\Virtual Server Environment" /v CurrentVersion') DO set MOE_VERSION=%%B
	echo [%DATE% %TIME%] !MOE_VERSION:~0,-6!	                                 >> %LOGFILE%
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking MatrixRM Registry present on the system or not.    >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\Insight Recovery" /v Version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] MatrixRM Registry Found                                 >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\Insight Recovery" /v Version') DO set RM_VERSION=%%B 
	echo [%DATE% %TIME%] !RM_VERSION:~0,-6!	                                     >> %LOGFILE%
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking MSSW Registry present on the system or not.        >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\Managed System Setup Wizard" /v CurrentVersion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] MSSW Registry Found                                     >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\Managed System Setup Wizard" /v CurrentVersion') DO set MSSW_VERSION=%%B 
	echo [%DATE% %TIME%] !MSSW_VERSION:~0,-6!	                                 >> %LOGFILE%
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking Mxsync Registry present on the system or not.      >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\HEWLETT-PACKARD\SYSTEMS INSIGHT MANAGER\mxsync" /v Version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] Mxsync Registry Found                                   >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\HEWLETT-PACKARD\SYSTEMS INSIGHT MANAGER\mxsync" /v Version') DO set Mxsync_VERSION=%%B 
	echo [%DATE% %TIME%] !Mxsync_VERSION:~0,-3!	                                 >> %LOGFILE% 
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking SPM Registry present on the system or not.         >> %LOGFILE%  
REG QUERY "HKLM\Software\HP\SPM" /v Version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] SPM Registry Found                                      >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\HP\SPM" /v Version') DO set SPM_VERSION=%%B 
	echo [%DATE% %TIME%] !SPM_VERSION:~0,-4!	                                 >> %LOGFILE%
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking SMH Registry present on the system or not.         >> %LOGFILE%
REG QUERY "HKLM\Software\Wow6432Node\Hewlett-Packard\System Management Homepage" /v Version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] SMH Registry Found                                      >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Wow6432Node\Hewlett-Packard\System Management Homepage" /v Version') DO set SMH_VERSION=%%B 
	echo [%DATE% %TIME%] !SMH_VERSION!	                                         >> %LOGFILE%
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking VCRM Registry present on the system or not.        >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\Version Control Repository Manager" /v Version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] VCRM Registry Found                                     >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\Version Control Repository Manager" /v Version') DO set VCRM_VERSION=%%B 
	echo [%DATE% %TIME%] !VCRM_VERSION:~0,-3!                                    >> %LOGFILE% 
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking WMIMAPPER Registry present on the system or not.   >> %LOGFILE%
REG QUERY "HKLM\Software\Wow6432Node\The Open Group\WMI Mapper\Settings" /v Version >NUL 2>&1 
if %ERRORLEVEL%==0 (
	echo [%DATE% %TIME%] WMIMAPPER Registry Found                                >> %LOGFILE%
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Wow6432Node\The Open Group\WMI Mapper\Settings" /v Version') DO set WMIMAP_VERSION=%%B 
	echo [%DATE% %TIME%] !WMIMAP_VERSION!	                                     >> %LOGFILE%
) else (
	echo [%DATE% %TIME%] Component not installed                                 >> %LOGFILE%
)

:: CHECKING COMPONENTS FOLDER EXIST OR NOT:
:: Checking whether the Component participating in the Limited Release or not
:: Participated components will be shown or listed

ECHO [%DATE% %TIME%] CHECKING COMPONENTS FOLDER EXIST OR NOT:                    >> %LOGFILE%   

:: Checking OpenSSH bundled in HPSIM or not
:: OpenSSH bundled in latest hotfix which means Participated in current SIM hotfix
:: IM Installer takes OpenSSH from SIM, So installer will check in HPSIM file folder

echo [%DATE% %TIME%] Checking for OpenSSH version                                >> %LOGFILE% 
IF EXIST "%~dp0\hpsim\file\cp017273.exe" (
echo [%DATE% %TIME%] OpnenSSH.exe Bundled in HPSIM                               >> %LOGFILE% 
set OpenSSHBundled=true
) ELSE (
echo [%DATE% %TIME%] OpenSSH.exe not Bundled in HPSIM                            >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking for hpsim folder                                   >> %LOGFILE%
IF EXIST "%~dp0\hpsim\install.bat" (
echo [%DATE% %TIME%] SIM folder found                                            >> %LOGFILE% 
set SimBundled=true
) ELSE (
echo [%DATE% %TIME%] SIM folder not found                                        >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking for ICmigr folder                                  >> %LOGFILE%
IF EXIST "%~dp0\ICmigr\install.bat" (
echo [%DATE% %TIME%] ICmigr folder found                                         >> %LOGFILE%
set ICmigrBundled=true
) ELSE (
echo [%DATE% %TIME%] ICmigr folder not found                                     >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking for ICvirt folder                                  >> %LOGFILE% 
IF EXIST "%~dp0\ICvirt\install.bat" (
echo [%DATE% %TIME%] ICvirt folder found                                         >> %LOGFILE%
set ICvirtBundled=true
) ELSE (
echo [%DATE% %TIME%] ICvirt folder not found                                     >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking for MatrixIO folder                                >> %LOGFILE%  
IF EXIST "%~dp0\MatrixIO\install.bat" (
echo [%DATE% %TIME%] MatrixIO folder found                                       >> %LOGFILE%
set MatrixIOBundled=true
) ELSE (
echo [%DATE% %TIME%] MatrixIO folder not found                                   >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking for MatrixOE folder                                >> %LOGFILE%
IF EXIST "%~dp0\MatrixOE\install.bat" (
echo [%DATE% %TIME%] MatrixOE folder found                                       >> %LOGFILE%
set MatrixOEBundled=true
) ELSE (
echo [%DATE% %TIME%] MatrixOE folder not found                                   >> %LOGFILE%   
)

echo [%DATE% %TIME%] Checking for MatrixRM folder                                >> %LOGFILE%  
IF EXIST "%~dp0\MatrixRM\install.bat" (
echo [%DATE% %TIME%] MatrixRM folder found                                       >> %LOGFILE%
set MatrixRMBundled=true
) ELSE (
echo [%DATE% %TIME%] MatrixRM folder not found                                   >> %LOGFILE% 
)

echo [%DATE% %TIME%] Checking for smh folder                                     >> %LOGFILE%
IF EXIST "%~dp0\smh\install.bat" (
echo [%DATE% %TIME%] smh folder found                                            >> %LOGFILE%
set smhBundled=true
) ELSE (
echo [%DATE% %TIME%] smh folder not found                                        >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking for spm folder                                     >> %LOGFILE%
IF EXIST "%~dp0\spm\install.bat" (
echo [%DATE% %TIME%] spm folder found                                            >> %LOGFILE% 
set spmBundled=true
) ELSE (
echo [%DATE% %TIME%] spm folder not found                                        >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking for vcem folder                                    >> %LOGFILE%
IF EXIST "%~dp0\vcem\install.bat" (
echo [%DATE% %TIME%] vcem folder found                                           >> %LOGFILE%    
set vcemBundled=true
) ELSE (
echo [%DATE% %TIME%] vcem folder not found                                       >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking for vcrep folder                                   >> %LOGFILE%
IF EXIST "%~dp0\vcrep\install.bat" (
echo [%DATE% %TIME%] vcrep folder found                                          >> %LOGFILE%  
set vcrepBundled=true
) ELSE (
echo [%DATE% %TIME%] vcrep folder not found                                      >> %LOGFILE% 
)

echo [%DATE% %TIME%] Checking for wmimapper folder                               >> %LOGFILE%
IF EXIST "%~dp0\wmimapper\install.bat" (
echo [%DATE% %TIME%] wmimapper folder found                                      >> %LOGFILE%
set wmimapperBundled=true
) ELSE (
echo [%DATE% %TIME%] wmimapper folder not found                                  >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking for ICperf folder                                  >> %LOGFILE%   
IF EXIST "%~dp0\ICperf\install.bat" (
echo [%DATE% %TIME%] ICperf folder found                                         >> %LOGFILE% 
set ICperfBundled=true
) ELSE (
echo [%DATE% %TIME%] ICperf folder not found                                     >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking for ICpwr folder                                   >> %LOGFILE%
IF EXIST "%~dp0\ICpwr\install.bat" (
echo [%DATE% %TIME%] ICpwr folder found                                          >> %LOGFILE%  
set ICpwrBundled=true
) ELSE (
echo [%DATE% %TIME%] ICpwr folder not found                                      >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking for mssw folder                                    >> %LOGFILE% 
IF EXIST "%~dp0\mssw\install.bat" (
echo [%DATE% %TIME%] mssw folder found                                           >> %LOGFILE%
set msswBundled=true
) ELSE (
echo [%DATE% %TIME%] mssw folder not found                                       >> %LOGFILE%
)

echo [%DATE% %TIME%] Checking for mxsync folder                                  >> %LOGFILE%  
IF EXIST "%~dp0\mxsync\install.bat" (
echo [%DATE% %TIME%] mxsync folder found                                         >> %LOGFILE%
set mxsyncBundled=true
) ELSE (
echo [%DATE% %TIME%] mxsync folder not found                                     >> %LOGFILE%
)

:: READING LimitedRelease REGISTRIES:
:: Checking whether LR1 installed or not
:: Reading Components Current Versions from registries 
:: If component installed it will display the current version of the Component
ECHO [%DATE% %TIME%] Reading Limited Release Registries:                         >> %LOGFILE%
:: Checking SMH LR1 Applied or not
echo [%DATE% %TIME%] Checking SMH Patch installed on the system or not.          >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\System Management Homepage" /v LRversion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\System Management Homepage" /v LRversion') DO set SMHLR1=%%B
	echo [%DATE% %TIME%] !SMHLR1!	                                             >> %LOGFILE%
) else (
goto :SMHx64
:SMHx64
	REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\System Management Homepage" /v LRversion >NUL 2>&1 
	if %ERRORLEVEL%==0 (
		FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\System Management Homepage" /v LRversion') DO set SMHLR1=%%B
		echo [%DATE% %TIME%] !SMHLR1!	                                         >> %LOGFILE%
	) else (
		echo [%DATE% %TIME%] SMH patch not installed                             >> %LOGFILE%
	)
)

:: Checking WMIMAPPER LR1 Applied or not
echo [%DATE% %TIME%] Checking WMIMAPPER Patch installed on the system or not.    >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\Wow6432Node\The Open Group\WMI Mapper\Settings" /v LRversion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\The Open Group\WMI Mapper\Settings" /v LRversion') DO set WMIMAPPERLR1=%%B
	echo [%DATE% %TIME%] !WMIMAPPERLR1!	                                         >> %LOGFILE%
) else (
goto :WMIMAPPERx64
:WMIMAPPERx64
	REG QUERY "HKLM\SOFTWARE\The Open Group\WMI Mapper\Settings" /v LRversion >NUL 2>&1 
	if %ERRORLEVEL%==0 (
		FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\The Open Group\WMI Mapper\Settings" /v LRversion') DO set WMIMAPPERLR1=%%B
		echo [%DATE% %TIME%] !WMIMAPPERLR1!	                                     >> %LOGFILE%
	) else (
		echo [%DATE% %TIME%] WMIMAPPER patch not installed                       >> %LOGFILE%
	)
)

:: Checking SIM LR1 Applied or not
echo [%DATE% %TIME%] Checking SIM Patch installed on the system or not.          >> %LOGFILE%
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\Systems Insight Manager\Settings" /v LRversion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\Systems Insight Manager\Settings" /v LRversion') DO set SIMLR1=%%B
	echo [%DATE% %TIME%] !SIMLR1!	                                             >> %LOGFILE%
) else (
goto :SIMx64
:SIMx64
	REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Hewlett-Packard\Systems Insight Manager\Settings" /v LRversion >NUL 2>&1 
	if %ERRORLEVEL%==0 (
		FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Hewlett-Packard\Systems Insight Manager\Settings" /v LRversion') DO set SIMLR1=%%B
		echo [%DATE% %TIME%] !SIMLR1!	                                         >> %LOGFILE%
	) else (
		echo [%DATE% %TIME%] SIM patch not installed                             >> %LOGFILE%
	)
)

:: Checking VCRM LR1 Applied or not
:: echo [%DATE% %TIME%] Checking VCRM Patch installed on the system or not.      >> %LOGFILE%
:: REG QUERY "HKLM\Software\Wow6432Node\Hewlett-Packard\LimitedRelease\Settings" /v VCRMInstalled >NUL 2>&1 
:: if %ERRORLEVEL%==0 (
::	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Wow6432Node\Hewlett-Packard\LimitedRelease\Settings" /v VCRMInstalled') DO set VCRMLR1=%%B
::	echo [%DATE% %TIME%] !VCRMLR1!	                                             >> %LOGFILE%
:: ) else (
::	echo [%DATE% %TIME%] VCRM patch not installed                                >> %LOGFILE%
:: )

:: Checking MSSW LR1 Applied or not
echo [%DATE% %TIME%] Checking MSSW Patch installed on the system or not.         >> %LOGFILE%
REG QUERY "HKLM\Software\Wow6432Node\Hewlett-Packard\Managed System Setup Wizard" /v LRversion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Wow6432Node\Hewlett-Packard\Managed System Setup Wizard" /v LRversion') DO set MSSWLR1=%%B
	echo [%DATE% %TIME%] !MSSWLR1!	                                             >> %LOGFILE% 
) else (
	goto :MSSWx64
:MSSWx64
	REG QUERY "HKLM\Software\Hewlett-Packard\Managed System Setup Wizard" /v LRversion  >NUL 2>&1
	if %ERRORLEVEL%==0 (
		FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Hewlett-Packard\Managed System Setup Wizard" /v LRversion') DO set MSSWLR1=%%B
		echo [%DATE% %TIME%] !MSSWLR1!	                                         >> %LOGFILE%
	) else (
		echo [%DATE% %TIME%] MSSW patch not installed                            >> %LOGFILE%
	)
)

:: Checking IPM LR1 Applied or not
echo [%DATE% %TIME%] Checking IPM Patch installed on the system or not.          >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\IPM\Settings" /v LRversion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\IPM\Settings" /v LRversion') DO set IPMLR1=%%B
	echo [%DATE% %TIME%] !IPMLR1!	                                             >> %LOGFILE%
) else (
goto :IPMx64
:IPMx64
	REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\IPM\Settings" /v LRversion >NUL 2>&1 
	if %ERRORLEVEL%==0 (
		FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\IPM\Settings" /v LRversion') DO set IPMLR1=%%B
		echo [%DATE% %TIME%] !IPMLR1!	                                         >> %LOGFILE%
	) else (
		echo [%DATE% %TIME%] IPM patch not installed                             >> %LOGFILE%
	)
)


:: Checking PMP LR1 Applied or not
echo [%DATE% %TIME%] Checking PMP Patch installed on the system or not.          >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{C9FD7662-4656-4C13-A82C-11FDDAE45176}" /v LRversion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{C9FD7662-4656-4C13-A82C-11FDDAE45176}" /v LRversion') DO set PMPLR1=%%B
	echo [%DATE% %TIME%] !PMPLR1!	                                             >> %LOGFILE%
) else (
goto :PMPx64
:PMPx64
	REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{C9FD7662-4656-4C13-A82C-11FDDAE45176}" /v LRversion >NUL 2>&1 
	if %ERRORLEVEL%==0 (
		FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{C9FD7662-4656-4C13-A82C-11FDDAE45176}" /v LRversion') DO set PMPLR1=%%B
		echo [%DATE% %TIME%] !PMPLR1!	                                         >> %LOGFILE%
	) else (
		echo [%DATE% %TIME%] PMP patch not installed                             >> %LOGFILE%
	)
)

:: Checking VMM LR1 Applied or not
echo [%DATE% %TIME%] Checking VMM Patch installed on the system or not.          >> %LOGFILE%
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\HP\Insight Control virtual machine management" /v LRversion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\HP\Insight Control virtual machine management" /v LRversion') DO set VMMLR1=%%B
	echo [%DATE% %TIME%] !VMMLR1!	                                             >> %LOGFILE%
) else (
goto :VMMx64
:VMMx64
	REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\HP\Insight Control virtual machine management" /v LRversion >NUL 2>&1 
	if %ERRORLEVEL%==0 (
		FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\HP\Insight Control virtual machine management" /v LRversion') DO set VMMLR1=%%B
		echo [%DATE% %TIME%] !VMMLR1!	                                         >> %LOGFILE%
	) else (
		echo [%DATE% %TIME%] VMM patch not installed                             >> %LOGFILE%
	)
)

:: Checking SMP LR1 Applied or not
echo [%DATE% %TIME%] Checking SMP Patch installed on the system or not.          >> %LOGFILE%
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\HP\HP Server Migration Pack" /v LRversion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\HP\HP Server Migration Pack" /v LRversion') DO set SMPLR1=%%B
	echo [%DATE% %TIME%] !SMPLR1!	                                             >> %LOGFILE%
) else (
goto :SMPx64
:SMPx64
	REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\HP\HP Server Migration Pack" /v LRversion >NUL 2>&1 
	if %ERRORLEVEL%==0 (
		FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\HP\HP Server Migration Pack" /v LRversion') DO set SMPLR1=%%B
		echo [%DATE% %TIME%] !SMPLR1!	                                         >> %LOGFILE%
	) else (
		echo [%DATE% %TIME%] SMP patch not installed                             >> %LOGFILE%
	)
)

:: Checking Mxsync LR1 Applied or not
echo [%DATE% %TIME%] Checking Mxsync Patch installed on the system or not.       >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\Systems Insight Manager\mxsync" /v LRversion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\Systems Insight Manager\mxsync" /v LRversion') DO set MxsyncLR1=%%B
	echo [%DATE% %TIME%] !MxsyncLR1!	                                         >> %LOGFILE%
) else (
goto :Mxsyncx64
:Mxsyncx64
	REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\Systems Insight Manager\mxsync" /v LRversion >NUL 2>&1 
	if %ERRORLEVEL%==0 (
		FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\Systems Insight Manager\mxsync" /v LRversion') DO set MxsyncLR1=%%B
		echo [%DATE% %TIME%] !MxsyncLR1!	                                     >> %LOGFILE%
	) else (
		echo [%DATE% %TIME%] Mxsync patch not installed                          >> %LOGFILE%
	)
)

:: Checking VCEM LR1 Applied or not
echo [%DATE% %TIME%] Checking VCEM Patch installed on the system or not.         >> %LOGFILE%
REG QUERY "HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\HP Virtual Connect Enterprise Manager_is1" /v "LRVersion" >NUL 2>&1 
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\HP Virtual Connect Enterprise Manager_is1" /v "LRVersion"') DO set VCEMLR1=%%B
	echo [%DATE% %TIME%] !VCEMLR1!	                                             >> %LOGFILE%
) else (
goto :VCEMx64
:VCEMx64
	REG QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\HP Virtual Connect Enterprise Manager_is1" /v LRVersion >NUL 2>&1 
	if %ERRORLEVEL%==0 (
		FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\HP Virtual Connect Enterprise Manager_is1" /v LRVersion') DO set VCEMLR1=%%B
		echo [%DATE% %TIME%] !VCEMLR1!	                                         >> %LOGFILE%
	) else (
		echo [%DATE% %TIME%] VCEM patch not installed                            >> %LOGFILE%
	)
)

:: Checking MatrixOE LR1 Applied or not
echo [%DATE% %TIME%] Checking MatrixOE Patch installed on the system or not.     >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\Virtual Server Environment" /v LRversion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\Virtual Server Environment" /v LRversion') DO set MOELR1=%%B
	echo [%DATE% %TIME%] !MOELR1:~0,5!	                                         >> %LOGFILE%
) else (
goto :MatrixOEx64
:MatrixOEx64
	REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\Virtual Server Environment" /v LRversion >NUL 2>&1 
	if %ERRORLEVEL%==0 (
		FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\Virtual Server Environment" /v LRversion') DO set MOELR1=%%B
		echo [%DATE% %TIME%] !MOELR1:~0,5!	                                     >> %LOGFILE%
	) else (
		echo [%DATE% %TIME%] MatrixOE patch not installed                        >> %LOGFILE% 
	)
)

:: Checking MatrixIO LR1 Applied or not
echo [%DATE% %TIME%] Checking MatrixIO Patch installed on the system or not.     >> %LOGFILE%
REG QUERY "HKLM\Software\Wow6432Node\Hewlett-Packard\Insight Orchestration" /v LRversion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\Insight Orchestration" /v LRversion') DO set IOLR1=%%B
	echo [%DATE% %TIME%] !IOLR1!	                                             >> %LOGFILE%
) else (
goto :MatrixIOx64
:MatrixIOx64
	REG QUERY "HKLM\Software\Hewlett-Packard\Insight Orchestration" /v LRversion >NUL 2>&1 
	if %ERRORLEVEL%==0 (
		FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Hewlett-Packard\Insight Orchestration" /v LRversion') DO set IOLR1=%%B
		echo [%DATE% %TIME%] !IOLR1!	                                         >> %LOGFILE%
	) else (
		echo [%DATE% %TIME%] MatrixIO patch not installed                        >> %LOGFILE%
	)
)

:: Checking MatrixRM LR1 Applied or not
echo [%DATE% %TIME%] Checking MatrixRM Patch installed on the system or not.     >> %LOGFILE%
REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\Insight Recovery" /v LRversion >NUL 2>&1 
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\Hewlett-Packard\Insight Recovery" /v LRversion') DO set MRMLR1=%%B
	echo [%DATE% %TIME%] !MRMLR1!	                                             >> %LOGFILE%
) else (
goto :MatrixRMx64
:MatrixRMx64
	REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\Insight Recovery" /v LRversion >NUL 2>&1 
	if %ERRORLEVEL%==0 (
		FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Hewlett-Packard\Insight Recovery" /v LRversion') DO set MRMLR1=%%B
		echo [%DATE% %TIME%] !MRMLR1!	                                         >> %LOGFILE%
	) else (
		echo [%DATE% %TIME%] MatrixRM patch not installed                        >> %LOGFILE%
	)
)


:: Checking SPM LR1 Applied or not
:: echo [%DATE% %TIME%] Checking SPM Patch installed on the system or not.       >> %LOGFILE%
:: REG QUERY "HKLM\Software\Wow6432Node\Hewlett-Packard\LimitedRelease\Settings" /v SPMInstalled >NUL 2>&1 
:: if %ERRORLEVEL%==0 (
::	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Wow6432Node\Hewlett-Packard\LimitedRelease\Settings" /v SPMInstalled') DO set SPMLR1=%%B
::	echo [%DATE% %TIME%] !SPMLR1!	                                             >> %LOGFILE%
:: ) else (
::	echo [%DATE% %TIME%] SPM patch not installed                                 >> %LOGFILE%
:: )


:: Checking for Installed components

:: Checking cp017273.exe
set /a Component=0
set OpenSSHoutputfile="false"
if "!OpenSSHVersion!" LSS "!OpenSSHVERCOMP!" (
    echo [%DATE% %TIME%] OpenSSH Installed                                          >> %LOGFILE%
if "%OpenSSHBundled%" EQU "true" (
    echo [%DATE% %TIME%] OpenSSH Bundled                                            >> %LOGFILE%
set OpenSSHoutputfile="true"	
set /a Component=%Component%+1
echo OpenSSH Service 
echo [%DATE% %TIME%] OpenSSH Service                                                >> %LOGFILE%
    )
 )

:: Calling SMH install.bat
set smhoutputfile="false"
if "!SMH_VERSION!" EQU "7.6.0" (
    echo [%DATE% %TIME%] SMH Installed                                              >> %LOGFILE%   
if "%smhBundled%" EQU "true" (
    echo [%DATE% %TIME%] SMH Bundled                                                >> %LOGFILE%
if "!SMHLR1!" LSS "7.6.1" (
    echo [%DATE% %TIME%] SMH LR1 not Installed                                      >> %LOGFILE%
set smhoutputfile="true" 	
set /a Component=%Component%+1
echo HPE System Management Homepage
echo [%DATE% %TIME%] HPE System Management Homepage                                 >> %LOGFILE%
    )
  )
)

:: Calling WMIMAPPER install.bat
set WMIMAPPERoutputfile="false"
if "!WMIMAP_VERSION!" EQU "7.6.0" (
    echo [%DATE% %TIME%]WMIMAPPER Installed                                         >> %LOGFILE%
if "%wmimapperBundled%" EQU "true" (
    echo [%DATE% %TIME%] WMIMAPPER Bundled                                          >> %LOGFILE%
if "!WMIMAPPERLR1!" LSS "7.6.1" (
    echo [%DATE% %TIME%]WMIMAPPER LR1 not Installed                                 >> %LOGFILE%
set WMIMAPPERoutputfile="true"
echo WMI Mapper
echo [%DATE% %TIME%] WMI Mapper                                                     >> %LOGFILE%	
set /a Component=%Component%+1
    )
  )
)

:: Calling SIM install.bat
set simoutputfile="false"
if "!SIM_VERSION!" EQU "!SIMVERCOMP!" (
    echo [%DATE% %TIME%] SIM Installed                                              >> %LOGFILE%
if "%SimBundled%" EQU "true" (
    echo [%DATE% %TIME%] SIM Bundled                                                >> %LOGFILE%
if "!SIMLR1!" LSS "7.6.4" (
    echo [%DATE% %TIME%] SIM LR4 not Installed                                      >> %LOGFILE%
set simoutputfile="true"	
set /a Component=%Component%+1
echo HPE Systems Insight Manager
echo [%DATE% %TIME%] HPE Systems Insight Manager                                    >> %LOGFILE%
    )
  )
)

:: Calling VCRM install.bat
set VCRMoutputfile="false"
if "!VCRM_VERSION:~0,-3!" LSS "7.6.6" (
    echo [%DATE% %TIME%] VCRM Installed                                             >> %LOGFILE%
if "%vcrepBundled%" EQU "true" (
    echo [%DATE% %TIME%] VCRM Bundled                                               >> %LOGFILE%
set VCRMoutputfile="true"	
set /a Component=%Component%+1
echo HPE Version Control Repository Manager
echo [%DATE% %TIME%] HPE Version Control Repository Manager                         >> %LOGFILE%
    )
 )
 
:: Calling MSSW install.bat
set MSSWoutputfile="false"
if "!MSSW_VERSION:~0,-6!" EQU "7.6.0" (
    echo [%DATE% %TIME%] MSSW Installed                                             >> %LOGFILE%
if "%msswBundled%" EQU "true" (
    echo [%DATE% %TIME%] MSSW Bundled                                               >> %LOGFILE%
if "!MSSWLR1!" LSS "7.6.2" (
    echo [%DATE% %TIME%] MSSW LR2 not Installed                                     >> %LOGFILE%
set MSSWoutputfile="true"	
set /a Component=%Component%+1
echo HPE Insight managed system setup wizard
echo [%DATE% %TIME%] HPE Insight managed system setup wizard                        >> %LOGFILE%
    )
  )
)

:: Calling IPM install.bat
rem if "!IPM_VERSION!" EQU "7.6.0" (
set IPMoutputfile="false"
if "!IPM_VERSION:~0,-1!" EQU "7.6.0" (
    echo [%DATE% %TIME%] IPM Installed                                              >> %LOGFILE%
if "%ICpwrBundled%" EQU "true" (
    echo [%DATE% %TIME%] IPM Bundled                                                >> %LOGFILE%
if "!IPMLR1!" LSS "7.6.1" (
    echo [%DATE% %TIME%] IPM LR1 not Installed                                      >> %LOGFILE%
set IPMoutputfile="true"	
set /a Component=%Component%+1
echo HPE Insight Control power management
echo [%DATE% %TIME%] HPE Insight Control power management                           >> %LOGFILE%
    )
  )
)

:: Calling PMP install.bat
set PMPoutputfile="false"
if "!PMP_VERSION!" EQU "7.6.0" (
    echo [%DATE% %TIME%] PMP Installed                                              >> %LOGFILE%
if "%ICperfBundled%" EQU "true" (
    echo [%DATE% %TIME%] PMP Bundled                                                >> %LOGFILE%
if "!PMPLR1!" LSS "7.6.1" (
    echo [%DATE% %TIME%] PMP LR1 not Installed                                      >> %LOGFILE%
set PMPoutputfile="true"	
set /a Component=%Component%+1
echo HPE Insight Control performance management
echo [%DATE% %TIME%] HPE Insight Control performance management                     >> %LOGFILE%
    )
  )
)

:: Calling VMM install.bat
set VMMoutputfile="false"
if "!VMM_VERSION!" EQU "7.6.0" (
    echo [%DATE% %TIME%] VMM Installed                                              >> %LOGFILE%
if "%ICvirtBundled%" EQU "true" (
    echo [%DATE% %TIME%] VMM Bundled                                                >> %LOGFILE%
if "!VMMLR1!" LSS "7.6.3" (
    echo [%DATE% %TIME%] VMM LR3 not Installed                                      >> %LOGFILE%
set VMMoutputfile="true"	
set /a Component=%Component%+1
echo HPE Insight Control virtual machine management
echo [%DATE% %TIME%] HPE Insight Control virtual machine management                 >> %LOGFILE%
    )
  )
)

:: Calling SMP install.bat
set SMPoutputfile="false"
if "!SMP_VERSION!" EQU "7.6.0" (
    echo [%DATE% %TIME%] SMP Installed                                              >> %LOGFILE%
if "%ICmigrBundled%" EQU "true" (
    echo [%DATE% %TIME%] SMP Bundled                                                >> %LOGFILE%
if "!SMPLR1!" LSS "7.6.1" (
    echo [%DATE% %TIME%] SMP LR1 not Installed                                      >> %LOGFILE%
set SMPoutputfile="true"	
set /a Component=%Component%+1
echo HPE Insight Control server migration
echo [%DATE% %TIME%] HPE Insight Control server migration                           >> %LOGFILE%
    )
  )
)

:: Calling Mxsync install.bat
set Mxsyncoutputfile="false"
if "!Mxsync_VERSION:~0,-3!" EQU "7.6.0" (
    echo [%DATE% %TIME%] Mxsync Installed                                           >> %LOGFILE%
if "%mxsyncBundled%" EQU "true" (
    echo [%DATE% %TIME%] Mxsync Bundled                                             >> %LOGFILE%
if "!MxsyncLR1!" LSS "7.6.1" (
    echo [%DATE% %TIME%] Mxsync LR1 not Installed                                   >> %LOGFILE%
set Mxsyncoutputfile="true"	
set /a Component=%Component%+1
echo HPE Insight mxsync Utility
echo [%DATE% %TIME%] HPE Insight mxsync Utility                                     >> %LOGFILE%
    )
  )
)

:: Calling VCEM install.bat
set VCEMoutputfile="false"
if "!VCEM_VERSION:~0,-3!" EQU "7.6.0" (
    echo [%DATE% %TIME%] VCEM Installed                                             >> %LOGFILE%
if "%vcemBundled%" EQU "true" (
    echo [%DATE% %TIME%] VCEM Bundled                                               >> %LOGFILE%
if "!VCEMLR1!" LSS "7.6.4" (
    echo [%DATE% %TIME%] VCEM LR4 not Installed                                     >> %LOGFILE%
set VCEMoutputfile="true"	
set /a Component=%Component%+1
echo HPE Virtual Connect Enterprise Manager
echo [%DATE% %TIME%] HPE Virtual Connect Enterprise Manager                         >> %LOGFILE%
    )
  )
)

:: Calling MatrixOE install.bat
set MatrixOEoutputfile="false"
if "!MOE_VERSION:~0,-6!" EQU "7.6.0" (
    echo [%DATE% %TIME%] MatrixOE Installed                                         >> %LOGFILE%
if "%MatrixOEBundled%" EQU "true" (
    echo [%DATE% %TIME%] MatrixOE Bundled                                           >> %LOGFILE%
if "!MOELR1:~0,5!" LSS "7.6.4" (
    echo [%DATE% %TIME%] MatrixOE LR4 not Installed                                 >> %LOGFILE%
set MatrixOEoutputfile="true"	
set /a Component=%Component%+1
echo HPE Matrix Operating Environment
echo [%DATE% %TIME%] HPE Matrix Operating Environment                               >> %LOGFILE%
    )
  )
)

:: Calling MatrixIO install.bat
rem if "!IO_VERSION!" EQU "7.6.0" (
set MatrixIOoutputfile="false"
if "!IO_VERSION:~0,-1!" EQU "7.6.0" (
	echo [%DATE% %TIME%] MatrixIO Installed                                          >> %LOGFILE%
if "%MatrixIOBundled%" EQU "true" (
	echo [%DATE% %TIME%] MatrixIO bundled                                            >> %LOGFILE%
if "!IOLR1!" LSS "7.6.4" (
	echo [%DATE% %TIME%] MatrixIO LR4 not Installed                                  >> %LOGFILE%
set MatrixIOoutputfile="true"	
set /a Component=%Component%+1
echo HPE Matrix infrastructure orchestration
echo [%DATE% %TIME%] HPE Matrix infrastructure orchestration                        >> %LOGFILE%
    )
  )  
)

:: Calling MatrixRM install.bat
set MatrixRMoutputfile="false"
if "!RM_VERSION:~0,-6!" EQU "7.6.0" (
   echo [%DATE% %TIME%] MatrixRM Installed                                          >> %LOGFILE%
if "%MatrixRMBundled%" EQU "true" (
   echo [%DATE% %TIME%] MatrixRM Bundled                                            >> %LOGFILE%
if "!MRMLR1!" LSS "7.6.4" (
   echo [%DATE% %TIME%] MatrixRM LR4 not Installed                                  >> %LOGFILE%  
set MatrixRMoutputfile="true"   
set /a Component=%Component%+1
echo HPE Matrix recovery management
echo [%DATE% %TIME%] HPE Matrix recovery management                                 >> %LOGFILE%
    )
  )  
)

:: Calling SPM install.bat
set SPMoutputfile="false"
if "!SPM_VERSION:~0,-4!" EQU "2.6.0" (
    echo [%DATE% %TIME%] SPM Installed                                              >> %LOGFILE%
if "%spmBundled%" EQU "true" (
	echo [%DATE% %TIME%] SPM Bundled                                                >> %LOGFILE%
set SPMoutputfile="true"
set /a Component=%Component%+1
echo HPE Storage Provisioning Manager
echo [%DATE% %TIME%] HPE Storage Provisioning Manager                               >> %LOGFILE%
   )
)	 

if "%Component%"=="0" (
	echo [%DATE% %TIME%] All HPE Insight Management components are already updated. There are no updates to install. >> %LOGFILE%
	echo All HPE Insight Management components are already updated. There are no updates to install.
	REG DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\ALM\LR1" /f    >NUL 2>&1
	pause
	goto :EOF
) else (
    echo.
	echo The above listed components will be installed.
	echo.
	echo [%DATE% %TIME%] The above listed components will be installed.             >> %LOGFILE%
)

:LICENSEAGREEMENT
:: License Agreement Page
IF EXIST "%~dp0\License.htm" (
FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v LocaleName') DO set OSlang=%%B
echo [%DATE% %TIME%] Operating System language detected: !OSlang!                                 >> %LOGFILE%
echo [%DATE% %TIME%] click yes "If you accept the terms of the license agreement"                 >> %LOGFILE%
echo click yes "If you accept the terms of the license agreement"
echo [%DATE% %TIME%] click no "If you do not accept the terms of the license agreement"           >> %LOGFILE%
echo click no "If you do not accept the terms of the license agreement"
echo.
echo [%DATE% %TIME%] Opening License Agreement Page read the terms and conditions of the license. >> %LOGFILE%
echo Opening License Agreement Page read the terms and conditions of the license.
if "!OSlang!" EQU "ja-JP" (
	 	start " "  "%~dp0\License_ja.htm"
	) else if "!OSlang!" EQU "zh-CN" (
		start " "  "%~dp0\License_zh.htm"
	) else (
	 	start " "  "%~dp0\License.htm"
	)
) ELSE (
echo [%DATE% %TIME%] License file is not found, installation terminated.                          >> %LOGFILE%
echo License file is not found, installation terminated.
REG DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\ALM\LR1" /f    >NUL 2>&1
goto :EOF
)
:Choice
set /p input= [yes or no]: 
if /I "%input%"=="yes" goto :YES
if /I "%input%"=="no" goto :NO
goto :Choice
:YES
echo [%DATE% %TIME%] You accepted the terms of the license agreement.            >> %LOGFILE%
echo You accepted the terms of the license agreement.
echo [%DATE% %TIME%] Installation continue...                                    >> %LOGFILE%
echo Installation continue...
echo.

:STOP
echo.
echo Stopping HPE Insight Management services
echo [%DATE% %TIME%] Stopping HPE Insight Management services                     >> %LOGFILE%
echo This might take some time please wait...
echo [%DATE% %TIME%] This might take some time please wait...                     >> %LOGFILE%
call :CHECKOONAME
call :STOPSVC "HP Matrix infrastructure orchestration"
call :STOPSVC "HP Matrix infrastructure chargeback"
call :STOPSVC "HP Matrix infrastructure bursting connecter"
call :STOPSVC "HP Logical Server Automation"
call :STOPSVC "HP Storage Provisioning Manager Service"
call :STOPSVC "HP Extensible Storage & Server Adapter"
call :STOPSVC "%OOC%"
call :STOPSVC "%OOR%"
call :STOPSVC "HP Application Discovery"
call :STOPSVC "HP Capacity Advisor Data Service"
call :STOPSVC "HP Agentless Data Collector Service"
call :STOPSVC "HP Insight Control virtual machine management"
call :STOPSVC "HP Global Workload Manager Central Management Server"
call :STOPSVC "HP Insight Control server migration Application Service"
call :STOPSVC "HP Insight Control server migration Web Service"
call :STOPSVC "HP Systems Insight Manager"
rem call :STOPSVC "HP Version Control Agent"
call :STOPSVC "HP Version Control Repository Manager"
call :STOPSVC "HP System Management Homepage"
call :STOPSVC "Pegasus WMI Mapper"
call :STOPSVC "Pegasus WMIMapper Indications Service"
call :STOPSVC "OpenSSH Server"
echo Services stopped successfully
echo [%DATE% %TIME%] Services stopped successfully                                >> %LOGFILE%


:: Using multiple IF condition
:: Executing Conditions:
:: For successful execution both conditions needs to be true
:: Means: Component Should be installed in the system and should be participating in Limited Release
echo.
echo Installation of Components:
echo.
echo [%DATE% %TIME%] Installation of Components:                                 >> %LOGFILE%
echo [%DATE% %TIME%] Executing Conditions:                                       >> %LOGFILE%

:: Creating IMLR Output file
set OUTPUTFILE="%LogDirectory%\IMLR_Output_%mydate%_%mytime%.txt"
echo Install Status of the components:                                           > %OUTPUTFILE%
echo.                                                                            >> %OUTPUTFILE%

:: Calling OpenSSH cp017273.exe
IF %OpenSSHoutputfile%=="true" (
	echo Launching OpenSSH Service...
	echo [%DATE% %TIME%] Launching OpenSSH Service...                                                                   >> %LOGFILE%
	call "%~dp0\hpsim\file\cp017273.exe"	
)

:: Checking whether OpenSSH updated or not.
REG QUERY "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\OpenSSH Services_is1" /v DisplayVersion >NUL 2>&1
if %ERRORLEVEL%==0 (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\OpenSSH Services_is1" /v DisplayVersion') DO set OpenSSHVersion=%%B
	echo [%DATE% %TIME%] OpenSSH Service Version !OpenSSHVersion!	                                                    >> %LOGFILE%
	) else (
    echo [%DATE% %TIME%] OpenSSH Service is not installed                                                               >> %LOGFILE%
)
IF %OpenSSHoutputfile%=="true" (
IF !OpenSSHVersion! EQU !OpenSSHVERCOMP! (
echo OpenSSH Service installed successfully...
echo [%DATE% %TIME%] OpenSSH Service installed successfully                                                             >> %LOGFILE%
echo OpenSSH Service installed successfully                                                                             >> %OUTPUTFILE%
echo.
) ELSE (
echo OpenSSH Service is not updated.
echo [%DATE% %TIME%] OpenSSH Service is not updated                                                                     >> %LOGFILE%
echo OpenSSH Service is not updated                                                                                     >> %OUTPUTFILE%
echo.
  )
)

:: Calling SMH install.bat
IF %smhoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing HPE System Management Homepage                                                      >> %LOGFILE%
	call "%~dp0\smh\install.bat"
)
:: Reading SMH Output file
IF EXIST "%LogDirectory1%\SMH_output.txt" (
IF %smhoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory1%\SMH_output.txt) do (
	if "%%x"=="Success" (
		echo HPE System Management Homepage installed successfully                                                      >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE System Management Homepage installed successfully                                      >> %LOGFILE%
		) ELSE (
		echo HPE System Management Homepage failed to install                                                           >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE System Management Homepage failed to install                                           >> %LOGFILE%
	  )
    )
  )
)

:: Calling WMIMAPPER install.bat
IF %WMIMAPPERoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing WMI Mapper                                                                          >> %LOGFILE%
	call "%~dp0\wmimapper\install.bat"
)
:: Reading WMIMAPPER Output file
IF EXIST "%LogDirectory1%\WMIMAPPER_output.txt" (
IF %WMIMAPPERoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory1%\WMIMAPPER_output.txt) do (
	if "%%x"=="Success" (
		echo WMI Mapper installed successfully                                                                          >> %OUTPUTFILE% 
		echo [%DATE% %TIME%] WMI Mapper installed successfully                                                          >> %LOGFILE%
		) ELSE (
		echo WMI Mapper failed to install                                                                               >> %OUTPUTFILE%  
		echo [%DATE% %TIME%] WMI Mapper failed to install                                                               >> %LOGFILE%
	  )
    )
  )
)

:: Calling SIM install.bat
IF %simoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing HPE Systems Insight Manager                                                         >> %LOGFILE%
	call "%~dp0\hpsim\install.bat"
)
:: Reading SIM Output file
IF EXIST "%LogDirectory%\hpsim_output.txt" (
IF %simoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory%\hpsim_output.txt) do (
	if "%%x"=="Success" (
		echo HPE Systems Insight Manager installed successfully                                                         >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE Systems Insight Manager installed successfully                                         >> %LOGFILE%
		) ELSE (
		echo HPE Systems Insight Manager failed to install, skipping all other components installation.                 >> %OUTPUTFILE% 
		echo [%DATE% %TIME%] HPE Systems Insight Manager failed to install, skipping all other components installation. >> %LOGFILE%
        echo HPE Systems Insight Manager failed to install, skipping all other components installation.
		goto :START
	  )
    )
  )
)

:: Calling VCRM install.bat
IF %VCRMoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing HPE Version Control Repository Manager                                              >> %LOGFILE%
	call "%~dp0\vcrep\install.bat"
)
:: Reading VCRM Output file
IF EXIST "%LogDirectory%\VCRM_output.txt" (
IF %VCRMoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory%\VCRM_output.txt) do (
	if "%%x"=="Success" (
		echo HPE Version Control Repository Manager installed successfully                                              >> %OUTPUTFILE% 
		echo [%DATE% %TIME%] HPE Version Control Repository Manager installed successfully                              >> %LOGFILE%
		) ELSE (
		echo HPE Version Control Repository Manager failed to install                                                   >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE Version Control Repository Manager failed to install                                   >> %LOGFILE%
	  )
    )
  )
)

:: Calling MSSW install.bat
IF %MSSWoutputfile%=="true" (	
	echo [%DATE% %TIME%] Installing HPE Insight managed system setup wizard                                             >> %LOGFILE%
	call "%~dp0\mssw\install.bat"
)
:: Reading MSSW Output file
IF EXIST "%LogDirectory2%\MSSW_output.txt" (
IF %MSSWoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory2%\MSSW_output.txt) do (
	if "%%x"=="Success" (
		echo HPE Insight managed system setup wizard installed successfully                                             >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE Insight managed system setup wizard installed successfully                             >> %LOGFILE%
		) ELSE (
		echo HPE Insight managed system setup wizard failed to install                                                  >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE Insight managed system setup wizard failed to install                                  >> %LOGFILE%
	  )
    )
  )
)


:: Calling IPM install.bat
IF %IPMoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing HPE Insight Control power management                                                >> %LOGFILE%
	call "%~dp0\ICpwr\install.bat"
)
:: Reading IPM Output file
IF EXIST "%LogDirectory1%\ICpwr_output.txt" (
IF %IPMoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory1%\ICpwr_output.txt) do (
	if "%%x"=="Success" (
		echo HPE Insight Control power management installed successfully                                                >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE Insight Control power management installed successfully                                >> %LOGFILE%
		) ELSE (
		echo HPE Insight Control power management failed to install                                                     >> %OUTPUTFILE%  
		echo [%DATE% %TIME%] HPE Insight Control power management failed to install                                     >> %LOGFILE%
	  )
    )
  )
)

:: Calling PMP install.bat
IF %PMPoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing HPE Insight Control performance management                                          >> %LOGFILE%
	call "%~dp0\ICperf\install.bat"
)
:: Reading PMP Output file
IF EXIST "%LogDirectory1%\ICperf_output.txt" (
IF %PMPoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory1%\ICperf_output.txt) do (
	if "%%x"=="Success" (
		echo HPE Insight Control performance management installed successfully                                          >> %OUTPUTFILE% 
		echo [%DATE% %TIME%] HPE Insight Control performance management installed successfully                          >> %LOGFILE%
		) ELSE (
		echo HPE Insight Control performance management failed to install                                               >> %OUTPUTFILE% 
		echo [%DATE% %TIME%] HPE Insight Control performance management failed to install                               >> %LOGFILE%
	  )
    )
  )
)

:: Calling VMM install.bat
IF %VMMoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing HPE Insight Control virtual machine management                                      >> %LOGFILE%
	call "%~dp0\ICvirt\install.bat"
)
:: Reading VMM Output file
IF EXIST "%LogDirectory%\ICvirt_output.txt" (
IF %VMMoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory%\ICvirt_output.txt) do (
	if "%%x"=="Success" (
		echo HPE Insight Control virtual machine management installed successfully                                      >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE Insight Control virtual machine management installed successfully                      >> %LOGFILE%
		) ELSE (
		echo HPE Insight Control virtual machine management failed to install                                           >> %OUTPUTFILE% 
		echo [%DATE% %TIME%] HPE Insight Control virtual machine management failed to install                           >> %LOGFILE%
	  )
    )
  )
)


:: Calling SMP install.bat
IF %SMPoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing HPE Insight Control server migration                                                >> %LOGFILE%
	call "%~dp0\ICmigr\install.bat"
)
:: Reading SMP Output file
IF EXIST "%LogDirectory1%\ICmigr_output.txt" (
IF %SMPoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory1%\ICmigr_output.txt) do (
	if "%%x"=="Success" (
		echo HPE Insight Control server migration installed successfully                                                >> %OUTPUTFILE% 
		echo [%DATE% %TIME%] HPE Insight Control server migration installed successfully                                >> %LOGFILE%
		) ELSE (
		echo HPE Insight Control server migration failed to install                                                     >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE Insight Control server migration failed to install                                     >> %LOGFILE%
	  )
    )
  )
)

:: Calling Mxsync install.bat
IF %Mxsyncoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing HPE Insight mxsync Utility                                                          >> %LOGFILE%
	call "%~dp0\mxsync\install.bat"
)
:: Reading Mxsync Output file
IF EXIST "%LogDirectory1%\Mxsync_output.txt" (
IF %Mxsyncoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory1%\Mxsync_output.txt) do (
	if "%%x"=="Success" (
		echo HPE Insight mxsync Utility installed sucessfully                                                           >> %OUTPUTFILE% 
		echo [%DATE% %TIME%] HPE Insight mxsync Utility installed sucessfully                                           >> %LOGFILE%
		) ELSE (
		echo HPE Insight mxsync Utility failed to install                                                               >> %OUTPUTFILE% 
		echo [%DATE% %TIME%] HPE Insight mxsync Utility failed to instal                                                >> %LOGFILE%
	  )
    )
  )
)


:: Calling VCEM install.bat
IF %VCEMoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing HPE Virtual Connect Enterprise Manager                                               >> %LOGFILE%	
	call "%~dp0\vcem\install.bat"
)
:: Reading VCEM Output file
IF EXIST "%LogDirectory%\VCEM_output.txt" (
IF %VCEMoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory%\VCEM_output.txt) do (
	if "%%x"=="SUCCESS" (
		echo HPE Virtual Connect Enterprise Manager installed successfully                                               >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE Virtual Connect Enterprise Manager installed successfully                               >> %LOGFILE%
		) ELSE (
		echo HPE Virtual Connect Enterprise Manager failed to install                                                    >> %OUTPUTFILE%  
		echo [%DATE% %TIME%] HPE Virtual Connect Enterprise Manager failed to install                                    >> %LOGFILE%
	  )
    )
  )
)

:: Calling MatrixOE install.bat
IF %MatrixOEoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing HPE Matrix Operating Environment                                                     >> %LOGFILE%
	call "%~dp0\MatrixOE\install.bat"
)
:: Reading MatrixOE Output file
IF EXIST "%LogDirectory%\MatrixOE_output.txt" (
IF %MatrixOEoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory%\MatrixOE_output.txt) do (
	if "%%x"=="Success" (
		echo HPE Matrix Operating Environment installed successfully                                                                                        >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE Matrix Operating Environment installed successfully                                                                        >> %LOGFILE%
		) ELSE (
		echo HPE Matrix Operating Environment failed to install, skipping installation of dependent components [MatrixIO, MatrixRM and SPM]                 >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE Matrix Operating Environment failed to install, skipping installation of dependent components [MatrixIO, MatrixRM and SPM] >> %LOGFILE%
		echo HPE Matrix Operating Environment failed to install, skipping installation of dependent components [MatrixIO, MatrixRM and SPM]
		goto :START
	  )
    )
  )
)


:: Calling MatrixIO install.bat
IF %MatrixIOoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing HPE Matrix infrastructure orchestration                                              >> %LOGFILE%
	call "%~dp0\MatrixIO\install.bat"
)
:: Reading MatrixIO Output file
IF EXIST "%LogDirectory%\MatrixIO_output.txt" (
IF %MatrixIOoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory%\MatrixIO_output.txt) do (
	if "%%x"=="Success" (
		echo HPE Matrix infrastructure orchestration installed successfully                                              >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE Matrix infrastructure orchestration installed successfully                              >> %LOGFILE%
		) ELSE (
		echo HPE Matrix infrastructure orchestration failed to install                                                   >> %OUTPUTFILE% 
		echo [%DATE% %TIME%] HPE Matrix infrastructure orchestration failed to install                                   >> %LOGFILE%
	  )
    )
  )
)

:: Calling MatrixRM install.bat
IF %MatrixRMoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing HPE Matrix recovery management                                                       >> %LOGFILE%
	call "%~dp0\MatrixRM\install.bat"
)
:: Reading MatrixRM Output file
IF EXIST "%LogDirectory%\MatrixRM_output.txt" (
IF %MatrixRMoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory%\MatrixRM_output.txt) do (
	if "%%x"=="Success" (
		echo HPE Matrix recovery management installed successfully                                                       >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE Matrix recovery management installed successfully                                       >> %LOGFILE%
		) ELSE (
		echo HPE Matrix recovery management failed to install                                                            >> %OUTPUTFILE% 
		echo [%DATE% %TIME%] HPE Matrix recovery management failed to install                                            >> %LOGFILE%
	  )
    )
  )
)
 
:: Calling SPM install.bat
IF %SPMoutputfile%=="true" (
	echo [%DATE% %TIME%] Installing HPE Storage Provisioning Manager                                                     >> %LOGFILE%
	call "%~dp0\spm\install.bat"
)	 
:: Reading SPM Output file
IF EXIST "%LogDirectory1%\spm_output.txt" (
IF %SPMoutputfile%=="true" (
for /F "tokens=1 delims= " %%x in (%LogDirectory1%\spm_output.txt) do (
	if "%%x"=="Success" (
		echo HPE Storage Provisioning Manager installed successfully                                                     >> %OUTPUTFILE%
		echo [%DATE% %TIME%] HPE Storage Provisioning Manager installed successfully                                     >> %LOGFILE%
		) ELSE (
		echo HPE Storage Provisioning Manager failed to install                                                          >> %OUTPUTFILE% 
		echo [%DATE% %TIME%] HPE Storage Provisioning Manager failed to install                                          >> %LOGFILE%
	  )
    )
  )
)

rem goto :EOF

:START
echo.
echo Starting HPE Insight Management services
echo [%DATE% %TIME%] Starting HPE Insight Management services                                                            >> %LOGFILE%
echo This might take some time please wait...
echo [%DATE% %TIME%] This might take some time please wait...                                                            >> %LOGFILE%
call :CHECKOONAME
call :STARTSVC "Pegasus WMI Mapper"
call :STARTSVC "Pegasus WMIMapper Indications Service"
call :STARTSVC "OpenSSH Server"
call :STARTSVC "HP System Management Homepage"
call :STARTSVC "HP Version Control Repository Manager"
rem call :STARTSVC "HP Version Control Agent"
call :STARTSVC "HP Systems Insight Manager"
call mxstatus -v -w                                                                                                      >> %LOGFILE%
call :STARTSVC "HP Insight Control server migration Web Service"
call :STARTSVC "HP Insight Control server migration Application Service"
call :STARTSVC "HP Global Workload Manager Central Management Server"
call :STARTSVC "HP Insight Control virtual machine management"
call :STARTSVC "HP Agentless Data Collector Service"
call :STARTSVC "HP Capacity Advisor Data Service"
call :STARTSVC "HP Application Discovery"
call :STARTSVC "%OOR%"
call :STARTSVC "%OOC%"
call :STARTSVC "HP Extensible Storage & Server Adapter"
call :STARTSVC "HP Storage Provisioning Manager Service"
call :STARTSVC "HP Logical Server Automation"
:: QXCR1001604646: In the 7.6 LR1 bursting connecter service is not been stopped when it is running
:: Implementing this for above QUIX

REG QUERY "HKLM\SYSTEM\CurrentControlSet\Services\hpio-connecter" /v Start >NUL 2>&1 
if %ERRORLEVEL%==0 (                                
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\SYSTEM\CurrentControlSet\Services\hpio-connecter" /v Start') DO set hpioconnecter=%%B
	echo [%DATE% %TIME%] !hpioconnecter!	                                                                             >> %LOGFILE%
) else (
	echo [%DATE% %TIME%] HP Matrix infrastructure bursting connecter service is not installed                            >> %LOGFILE%
)
 if "!hpioconnecter:~2,1!" == "4" (
    echo [%DATE% %TIME%] !hpioconnecter:~2,1!                                                                            >> %LOGFILE%
	goto :hpioconnecter
  )
call :STARTSVC "HP Matrix infrastructure bursting connecter"
goto continue
:hpioconnecter
echo [%DATE% %TIME%] The HP Matrix infrastructure bursting connecter service can not be started, because it is disabled. >> %LOGFILE%
:continue
call :STARTSVC "HP Matrix infrastructure chargeback"
call :STARTSVC "HP Matrix infrastructure orchestration"
echo Services started successfully
echo [%DATE% %TIME%] Services started successfully                                                                       >> %LOGFILE%
goto :DELETINLRCHREGISTRY

:STOPSVC
REM Function to stop a service if it is installed and running.
REM Argument is service display name
rem echo %1
call :GETSVCNAME %1
if "%SVCNAME%"=="" goto :EOF
call :CHECKSVCRUNNING %1
if %ISRUNNING%==0 (
    echo Service %1 is not running.                                                                                      >> %LOGFILE%
    echo.                                                                                                                >> %LOGFILE%
    goto :EOF
)
rem echo [%DATE% %TIME%] Stopping service %1                                                                             >> %LOGFILE%
net stop %1                                                                                                              >> %LOGFILE%
REM confirm service has stopped in case of timeout
call :CHECKSVCRUNNING %1
if %ISRUNNING%==1 (
    echo Waiting up to 15 seconds for service %1 to stop.                                                                >> %LOGFILE%
    for /l %%A in (1,1,5) do (
        TIMEOUT /T 3 > nul
        setlocal enabledelayedexpansion
        call :CHECKSVCRUNNING %1
        if !ISRUNNING!==0 ( 
            endlocal
            goto :STOPPED
        )
        endlocal
    )
	echo Service %1 could not be stopped.  Please stop it manually or try again.                                         >> %LOGFILE%
    goto :EOF
    
)
:STOPPED
rem echo Service %1 has been stopped.                                                                                    >> %LOGFILE%
goto :EOF

:GETSVCNAME
REM Function to get service name from service display name argument.
REM Service name is returned in variable SVCNAME.
REM Note: "Name = ..." may appear on second line of output
for /f "tokens=*" %%A in ('sc GetKeyName %1') do ( 
    for /f "tokens=2 delims==" %%B in ("%%A") do ( 
        for /f "tokens=*" %%C in ("%%B") do (
            if not "%%C"=="" (
                set SVCNAME=%%C
                goto :EOF     
            )
        )
    )
    for /f "tokens=*" %%B in ("%%A") do (
        for %%C in (%%B) do (
            if "%%C"=="1060:" (
                echo Service %1 is not installed                                                                         >> %LOGFILE%
                echo.                                                                                                    >> %LOGFILE%
                set SVCNAME=
                goto :EOF
            )
        )                 
    )
)
echo Error: Failed to look up service %1                                                                                 >> %LOGFILE%
set SVCNAME=
goto :EOF

:CHECKSVCRUNNING
set ISRUNNING=0
for /f "tokens=*" %%A in ('net start') do (
    if /i "%%A"==%1 (
        set ISRUNNING=1
        goto :EOF
    )
)
goto :EOF

:STARTSVC
REM Function to start a service if it is installed and not running.
REM Argument is service display name
call :GETSVCNAME %1
if "%SVCNAME%"=="" goto :EOF
call :CHECKSVCRUNNING %1
if %ISRUNNING%==1 (
    echo Service %1 is already running                                                                                   >> %LOGFILE%
    echo.                                                                                                                >> %LOGFILE%
    goto :EOF
)
rem echo [%DATE% %TIME%] Starting service %1.                                                                            >> %LOGFILE%
net start %1                                                                                                             >> %LOGFILE%
REM confirm service has started in case of timeout
call :CHECKSVCRUNNING %1
if %ISRUNNING%==0 (
    echo Waiting up to 15 seconds for service %1 to start.                                                               >> %LOGFILE%
    for /l %%A in (1,1,5) do (
        TIMEOUT /T 3 > nul
        setlocal enabledelayedexpansion
        call :CHECKSVCRUNNING %1
        if !ISRUNNING!==1 ( 
            endlocal
            goto :STARTED
        )
        endlocal
    )
	echo Service %1 could not be started.  Please start it manually or try again.                                        >> %LOGFILE%
    echo.                                                                                                                >> %LOGFILE%
    goto :EOF
)

:STARTED
rem echo Service %1 has been started.                                                                                    >> %LOGFILE%
rem echo.
goto :EOF

:CHECKOONAME
FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\ALM\Settings" /v InstallPath') DO set IM_PATH=%%B

rem echo %IM_PATH%
set IM_PATH1=%IM_PATH:\=/% 
Rem echo %IM_PATH1%

REM set IM_PATH2=%IM_PATH1:~-4%
set IM_PATH2=%IM_PATH1:~0,-1%
rem echo %IM_PATH2%

set OOC1=HPE Operations Orchestration Central (@ 
set OOC2=/Operations Orchestration 10/central)
set OOC=%OOC1%%IM_PATH2%%OOC2%

set OOR1=HPE Operations Orchestration RAS (@ 
set OOR2=/Operations Orchestration 10/ras)
set OOR=%OOR1%%IM_PATH2%%OOR2%

goto :EOF

:DELETINLRCHREGISTRY
rem echo [%DATE% %TIME%] Deleting Limited Release registry                               							     >> %LOGFILE%  
REG DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\ALM\LR1" /f    >NUL 2>&1
rem echo [%DATE% %TIME%] Deleted Limited Release registry                           									 >> %LOGFILE%  
rem EXIT /B %ERRORLEVEL%

:: Displaying Status of the Installed Components
:: Opening IMLR_Output.txt
start " " "%OUTPUTFILE%"

goto :EOF

:DVDVERSIONNOTFOUND 
echo HPE Insight Management version 7.6.0 does not exist on the CMS.
echo [%DATE% %TIME%] HPE Insight Management version 7.6.0 does not exist on the CMS.                                     >> %LOGFILE%
REG DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\ALM\LR1" /f    >NUL 2>&1
pause
EXIT /B %ERRORLEVEL%

:LRINSTALLED
rem echo Process aborted: Another instance already running on the machine.
echo Another instance of this setup is already running. Please wait for the other instance to finish and then try again.
pause
EXIT /B %ERRORLEVEL%

:NO
echo [%DATE% %TIME%] You did not accept the terms of the license agreement. Installation can not proceed.                >> %LOGFILE%
echo You did not accept the terms of the license agreement. Installation can not proceed. 
REG DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Hewlett-Packard\ALM\LR1" /f    >NUL 2>&1
pause
EXIT /B %ERRORLEVEL%
endlocal