@ECHO OFF
Setlocal EnableDelayedExpansion

ECHO Installing HPE Insight Control performance management...
echo This might take some time please wait...

:: Creating LogFile with time stamp
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%

:: Creating LogFile Folder 
if not exist "%SystemDrive%\HPIC\logs\LimitedRelease1" mkdir "%SystemDrive%\HPIC\logs\LimitedRelease1"
set LOGFILE=%SystemDrive%\HPIC\logs\LimitedRelease1\ICperf_%mydate%_%mytime%.log
ECHO [%DATE% %TIME%] Installing HPE Insight Control performance management...                                                                                                        >> %LOGFILE%
echo [%DATE% %TIME%] This might take some time please wait...                                                                                                                        >> %LOGFILE%
echo [%DATE% %TIME%] Log file path: %LOGFILE%                                                                                                                                        >> %LOGFILE% 

:: Getting SIM Installation Path from Registry
FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "HKLM\Software\Hewlett-Packard\Systems Insight Manager\Settings" /v InstallPath') DO set SIMInstallPath=%%B
echo [%DATE% %TIME%] Reading HPESIM install path from registry                                                                                                                       >> %LOGFILE% 
echo [%DATE% %TIME%] HPESIM InstallPath: %SIMInstallPath%                                                                                                                            >> %LOGFILE% 
echo [%DATE% %TIME%] Applying Patch                                                                                                                                                  >> %LOGFILE% 

:: Creating Backup Folder
echo [%DATE% %TIME%] Creating Backup Folder:                                                                                                                                         >> %LOGFILE% 
set PMP=Insight Control performance management
for /D %%D in ("%SIMInstallPath%") do (
	set BackupInstallPath=%%~dpD
)

if not exist "%BackupInstallPath%LimitedRelease1\%PMP%" mkdir "%BackupInstallPath%LimitedRelease1\%PMP%"
set BackupFolder=%BackupInstallPath%LimitedRelease1\%PMP%
echo [%DATE% %TIME%] Backup Folder Path: %BackupFolder%                                                                                                                              >> %LOGFILE% 

:: Copying files 
:pmp-web.war
echo [%DATE% %TIME%] Copying pmp-web.war                                                         to "%SIMInstallPath%\jboss\server\hpsim\deploy\pmpnext.ear"                         >> %LOGFILE%
ROBOCOPY "%SIMInstallPath%\jboss\server\hpsim\deploy\pmpnext.ear\pmp-web.war"                       "%BackupFolder%\pmp-web.war" /S /E                                               >> %LOGFILE%
ROBOCOPY "%~dp0\File\pmp-web.war"                                                                   "%SIMInstallPath%\jboss\server\hpsim\deploy\pmpnext.ear\pmp-web.war" /S /E       >> %LOGFILE%
echo [%DATE% %TIME%] errorlevel:%errorlevel%                                                                                                                                         >> %LOGFILE%
if %ERRORLEVEL%  GEQ 8 goto rollback

:ICperftools.xml
echo [%DATE% %TIME%] Copying ICperftools.xml                                                     to "%SIMInstallPath%\config\preload-plugins\76\icperf\addfiles"                     >> %LOGFILE%
copy /Y "%SIMInstallPath%\config\preload-plugins\76\icperf\addfiles\ICperftools.xml"                "%BackupFolder%\ICperftools.xml"                                                 >NUL 2>&1 
copy /Y "%~dp0\File\ICperftools.xml"                                                                "%SIMInstallPath%\config\preload-plugins\76\icperf\addfiles"                     >NUL 2>&1 
echo [%DATE% %TIME%] errorlevel:%errorlevel%                                                                                                                                         >> %LOGFILE% 
if errorlevel 1 goto rollback   

:index.html\en
echo [%DATE% %TIME%] Copying index.html                                                          to "%SIMInstallPath%\hpwebadmin\webapps\mxhelp\icperf\en\content"                   >> %LOGFILE%
if not exist "%BackupFolder%\en" mkdir "%BackupFolder%\en"                                                                                                                           >> %LOGFILE%
copy /Y "%SIMInstallPath%\hpwebadmin\webapps\mxhelp\icperf\en\content\index.html"                   "%BackupFolder%\en\index.html"                                                   >NUL 2>&1 
copy /Y %~dp0\File\en\index.html                                                                    "%SIMInstallPath%\hpwebadmin\webapps\mxhelp\icperf\en\content"                   >NUL 2>&1 
echo [%DATE% %TIME%] errorlevel:%errorlevel%                                                                                                                                         >> %LOGFILE% 
if errorlevel 1 goto rollback  

:index.html\ja
echo [%DATE% %TIME%] Copying index.html                                                          to "%SIMInstallPath%\hpwebadmin\webapps\mxhelp\icperf\ja\content"                   >> %LOGFILE%
if not exist "%BackupFolder%\ja" mkdir "%BackupFolder%\ja"                                                                                                                           >> %LOGFILE%
copy /Y "%SIMInstallPath%\hpwebadmin\webapps\mxhelp\icperf\ja\content\index.html"                   "%BackupFolder%\ja\index.html"                                                   >NUL 2>&1 
copy /Y %~dp0\File\ja\index.html                                                                    "%SIMInstallPath%\hpwebadmin\webapps\mxhelp\icperf\ja\content"                   >NUL 2>&1 
echo [%DATE% %TIME%] errorlevel:%errorlevel%                                                                                                                                         >> %LOGFILE% 
if errorlevel 1 goto rollback  

:index.html\zh_CN
echo [%DATE% %TIME%] Copying index.html                                                          to "%SIMInstallPath%\hpwebadmin\webapps\mxhelp\icperf\zh_CN\content"                >> %LOGFILE%
if not exist "%BackupFolder%\zh_CN" mkdir "%BackupFolder%\zh_CN"                                                                                                                     >> %LOGFILE%
copy /Y "%SIMInstallPath%\hpwebadmin\webapps\mxhelp\icperf\zh_CN\content\index.html"                "%BackupFolder%\zh_CN\index.html"                                                >NUL 2>&1 
copy /Y %~dp0\File\zh_CN\index.html                                                                 "%SIMInstallPath%\hpwebadmin\webapps\mxhelp\icperf\zh_CN\content"                >NUL 2>&1 
echo [%DATE% %TIME%] errorlevel:%errorlevel%                                                                                                                                         >> %LOGFILE% 
if errorlevel 1 goto rollback 
 
:home.html
echo [%DATE% %TIME%] Copying home.html                                                          to "%SIMInstallPath%\hpwebadmin\webapps\mxhelp\icperf\en\content"                    >> %LOGFILE%
copy /Y "%SIMInstallPath%\hpwebadmin\webapps\mxhelp\icperf\en\content\home.html"                   "%BackupFolder%\home.html"                                                        >NUL 2>&1 
copy /Y %~dp0\File\home.html                                                                       "%SIMInstallPath%\hpwebadmin\webapps\mxhelp\icperf\en\content"                    >NUL 2>&1 
echo [%DATE% %TIME%] errorlevel:%errorlevel%                                                                                                                                         >> %LOGFILE% 
if errorlevel 1 goto rollback 
 
echo [%DATE% %TIME%] All files copied successfully.                                                                                                                                  >> %LOGFILE%
:: For successful PMP Patch installation 7.6.1 registry created in below registry path
REG ADD "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{C9FD7662-4656-4C13-A82C-11FDDAE45176}" /v "LRversion" /t REG_SZ /d 7.6.1 /f  >NUL 2>&1 
echo Installation completed successfully.
echo [%DATE% %TIME%] Installation completed successfully.                                                                                                                            >> %LOGFILE%
echo.
echo Success>"%SystemDrive%\HPIC\logs\LimitedRelease1\ICperf_output.txt"
exit /B %ERROELEVEL%   

:rollback
echo Fail>"%SystemDrive%\HPIC\logs\LimitedRelease1\ICperf_output.txt"
echo [%DATE% %TIME%] Failed to install, see %LOGFILE% for details.                                                                                                                   >> %LOGFILE%
echo Failed to install, see %LOGFILE% for details.
echo.
call "%~dp0\rollback.bat"
EXIT /B %ERRORLEVEL%
endlocal
