@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\egvmagent.jar;%EGURKHA_INSTALL_DIR%\lib\eg_util.jar;%EGURKHA_INSTALL_DIR%\lib;%EGURKHA_INSTALL_DIR%\bin;

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

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

%EGURKHA_INSTALL_DIR%\lib\egwait 120

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

	echo Stopping the eG VM Agent ... >>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	
	net stop eGVmAgentMon >>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent 2>nul
		
	
	echo *********************************>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	echo The eGVMAgentMon service stopped..>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	echo *********************************>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
		
	net stop eGVmAgent >>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent 2>nul

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

	TASKKILL.EXE /F /T /IM AppLaunch.exe >nul 2>nul
	
	echo ***********************************************>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	echo The eG VM Agent has been successfully stopped.>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	echo ***********************************************>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent


%EGURKHA_INSTALL_DIR%\lib\egwait 30

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

	for /F "tokens=1,2 delims=~" %%i in (%EGURKHA_INSTALL_DIR%\process.txt) do (
		TASKKILL.EXE /pid %%j /f
	)
	cscript /nologo %EGURKHA_INSTALL_DIR%\bin\Proccess.vbs | findStr /i "egurkha\jre" >%EGURKHA_INSTALL_DIR%\process.txt
	for /F "tokens=1,2 delims=~" %%i in (%EGURKHA_INSTALL_DIR%\process.txt) do (
		TASKKILL.EXE /pid %%j /f
	)
	del /F %EGURKHA_INSTALL_DIR%\process.txt

if not exist %EGURKHA_INSTALL_DIR%\JRE_new goto end

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

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

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


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

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

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

	if exist %EGURKHA_INSTALL_DIR%\JRE_Older (

		copy %EGURKHA_INSTALL_DIR%\JRE_Older\lib\security\cacerts %EGURKHA_INSTALL_DIR%\JRE\lib\security\cacerts /Y >>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent 2>%EGURKHA_INSTALL_DIR%\logs\logging

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

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

	echo ******************************************************************>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	echo The eG VM Agent upgrade has been completed successfully!!! >>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	echo ******************************************************************>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent

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

	net start eGVmAgentMon >>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent 2>nul
		
	echo *********************************>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	echo The eGVmAgentMon service started..>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	echo *********************************>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
		
	net start eGVmAgent >>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent 2>nul
	
	echo ************************************>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	echo The eGVmAgent service started...>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	echo ************************************>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	
	echo ***********************************************>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	echo The eG VM Agent has been successfully started...>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent
	echo ***********************************************>>%EGURKHA_INSTALL_DIR%\logs\JREupgradeEvent

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

EXIT
