@echo off

SETLOCAL

set EGURKHA_INSTALL_DIR=%1

set path=%EGURKHA_INSTALL_DIR%\lib;%EGURKHA_INSTALL_DIR%\bin;%path%
set classpath=.;%EGURKHA_INSTALL_DIR%\lib\eg_agent.jar;%EGURKHA_INSTALL_DIR%\lib\eg_util.jar;%EGURKHA_INSTALL_DIR%\lib;%EGURKHA_INSTALL_DIR%\bin;

if exist %EGURKHA_INSTALL_DIR%\tmp\logging del %EGURKHA_INSTALL_DIR%\tmp\logging /q 

echo WELCOME TO JRE UPGRADE >%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent

%EGURKHA_INSTALL_DIR%\bin\egwait 120 

echo Agent JRE Upgrade started at %date% %time% >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent

	echo Stopping the eG Agent ... >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	
	net stop eGAgentMon >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent 2>nul
		
	
	echo *********************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo The eGAgentMon service stopped..>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo *********************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
		
	net stop eGurkhaAgent >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent 2>nul

	echo ************************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo The eGurkhaAgent service stopped...>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo ************************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent

	call %EGURKHA_INSTALL_DIR%\bin\KillAgentExecProcesses >nul 2>nul
	
	echo ***********************************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo The eG Agent has been successfully stopped.>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo ***********************************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent

	net stop eGTrapReceiver >nul 2>nul
	net stop eGNetFlowAgent >nul 2>nul
	net stop eGSyslogServer >nul 2>nul

%EGURKHA_INSTALL_DIR%\bin\egwait 30

	%EGURKHA_INSTALL_DIR%\bin\apppid | findStr /i "egurkha\jre" >%EGURKHA_INSTALL_DIR%\tmp\process.txt

	for /F "tokens=1,2 delims=~" %%i in (%EGURKHA_INSTALL_DIR%\tmp\process.txt) do (
		%EGURKHA_INSTALL_DIR%\bin\processkill.exe -p %%j
	)
	cscript /nologo %EGURKHA_INSTALL_DIR%\bin\Proccess.vbs | findStr /i "egurkha\jre" >%EGURKHA_INSTALL_DIR%\tmp\process.txt
	for /F "tokens=1,2 delims=~" %%i in (%EGURKHA_INSTALL_DIR%\tmp\process.txt) do (
		%EGURKHA_INSTALL_DIR%\bin\processkill.exe -p %%j
	)
	del /F %EGURKHA_INSTALL_DIR%\tmp\process.txt

if not exist %EGURKHA_INSTALL_DIR%\JRE_new goto end

	move /Y %EGURKHA_INSTALL_DIR%\JRE  %EGURKHA_INSTALL_DIR%\JRE_Older724 >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent 2>%EGURKHA_INSTALL_DIR%\tmp\logging
	
		if %ERRORLEVEL% NEQ 0 (
			echo ERROR Unable to rename the %EGURKHA_INSTALL_DIR%\JRE >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			echo No changes made to existing System!!!  >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			goto end
			)

		for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF

			if %size% GTR 0	(
				echo ERROR Unable to rename the %EGURKHA_INSTALL_DIR%\JRE >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
				echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
				echo No changes made to existing System!!!  >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
				type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			) 


	move /Y %EGURKHA_INSTALL_DIR%\JRE_new %EGURKHA_INSTALL_DIR%\JRE >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent 2>%EGURKHA_INSTALL_DIR%\tmp\logging

		if %ERRORLEVEL% NEQ 0 (
			echo ERROR Unable to upgrade the JRE >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			if exist %EGURKHA_INSTALL_DIR%\JRE_Older724 	move /Y %EGURKHA_INSTALL_DIR%\JRE_Older724 %EGURKHA_INSTALL_DIR%\JRE
			goto end
			)

		for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
		if %size% GTR 0	(
			echo ERROR Unable to upgrade the JRE >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			if exist %EGURKHA_INSTALL_DIR%\JRE_Older724 	move /Y %EGURKHA_INSTALL_DIR%\JRE_Older724 %EGURKHA_INSTALL_DIR%\JRE
			goto end
			) 
	

	if exist %EGURKHA_INSTALL_DIR%\JRE_Older724 (

		copy %EGURKHA_INSTALL_DIR%\JRE_Older724\lib\security\cacerts %EGURKHA_INSTALL_DIR%\JRE\lib\security\cacerts /Y >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent 2>%EGURKHA_INSTALL_DIR%\tmp\logging

		if %ERRORLEVEL% NEQ 0 (
			echo ERROR Unable to copy the %EGURKHA_INSTALL_DIR%\JRE_Older724\lib\security\cacerts file>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			echo If the agent allows only trusted certificates, then copy the "cacerts" certificate file from %EGURKHA_INSTALL_DIR%\JRE_Older724\lib\security\cacerts to %EGURKHA_INSTALL_DIR%\JRE\lib\security\cacerts >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			goto end
			) 

		for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
		if %size% GTR 0	(
			echo ERROR Unable to copy the %EGURKHA_INSTALL_DIR%\JRE_Older724\lib\security\cacerts file >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			echo If the agent allows only trusted certificates, then copy the "cacerts" certificate file from %EGURKHA_INSTALL_DIR%\JRE_Older724\lib\security\cacerts to %EGURKHA_INSTALL_DIR%\JRE\lib\security\cacerts >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			goto end
			)
		)

%EGURKHA_INSTALL_DIR%\JRE\bin\java UpdateSecurityFiles %EGURKHA_INSTALL_DIR%\JRE_Older724 %EGURKHA_INSTALL_DIR%\JRE >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent 2>%EGURKHA_INSTALL_DIR%\tmp\logging

for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
		if %size% GTR 0	(
			echo ERROR Unable to copy JVM security related files >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
			goto end
			)

if exist %EGURKHA_INSTALL_DIR%\JRE_Older724\ (
		rmdir %EGURKHA_INSTALL_DIR%\JRE_Older724\ /S /Q >nul 2>nul
	)
	echo ******************************************************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo The eG Agent upgrade has been completed successfully!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo ******************************************************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent

:end
	echo Starting the eG Agent ... >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent

	net start eGAgentmon >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent 2>nul
		
	echo *********************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo The eGAgentMon service started..>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo *********************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
		
	net start egurkhaAgent >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent 2>nul
	
	echo ************************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo The eGurkhaAgent service started...>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo ************************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	
	echo ***********************************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo The eG Agent has been successfully started...>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	echo ***********************************************>>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent

	net start eGTrapReceiver >nul 2>nul
	net start eGNetFlowAgent >nul 2>nul
	net start eGSyslogServer >nul 2>nul
	del %EGURKHA_INSTALL_DIR%\bin\Proccess.vbs /q  >nul 2>nul

	echo JRE UPGRADED.... >>%EGURKHA_INSTALL_DIR%\agent\logs\JREupgradeEvent
	
ENDLOCAL

EXIT
