@echo off

SETLOCAL

net session >nul 2>nul

if %ERRORLEVEL% GTR 0 (
	echo " Could you please run the scripts as administrator (Run as Adminisitrator).  "
	goto end
)

echo.
echo.


if "%EGURKHA_INSTALL_DIR%" == "" (
set /P EGURKHA_INSTALL_DIR= Enter eGurkha Install Dir path : 
)





set JAVA_HOME=%EGURKHA_INSTALL_DIR%\jre\bin;


set path=%JAVA_HOME%\bin;%path%;%EGURKHA_INSTALL_DIR%\lib;%EGURKHA_INSTALL_DIR%\bin;

set BACKUP_DIR=714bakup
set curDir="%cd%"
set MyCurDir="%cd%"
set size=0
set agentlogsize=0
set me=%cd%

set classpath=.;%me%\eg_upgrade.jar;%EGURKHA_INSTALL_DIR%\lib\eg_agent.jar;%EGURKHA_INSTALL_DIR%\upgrade\lib\eg_upgrade.jar;%EGURKHA_INSTALL_DIR%\lib\classes12.zip;.\Extract.jar;.\Extract.zip;%EGURKHA_INSTALL_DIR%\lib\eg_util.jar;
set Myclasspath=.;%me%\eg_upgrade.jar;%EGURKHA_INSTALL_DIR%\lib\eg_agent.jar;%EGURKHA_INSTALL_DIR%\upgrade\lib\eg_upgrade.jar;%EGURKHA_INSTALL_DIR%\lib\classes12.zip;.\Extract.jar;.\Extract.zip;%EGURKHA_INSTALL_DIR%\lib\eg_util.jar;


echo  %classpath% >> %EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event

echo.
echo.
echo.
echo WELCOME TO eG AGENT UPGRADE
echo =================================
title WELCOME TO eG AGENT UPGRADE



echo.
echo.

echo Enter Your Option :
echo.
echo.
set /P CH=[U - Upgrade  R - Revert  C - Commit E - Exit] ?  
if /I "%CH%"=="U" goto Upgrade
if /I "%CH%"=="R" goto Revert
if /I "%CH%"=="C" goto Commit
if /I "%CH%"=="E" goto End
goto end 


:Upgrade
      
        cls
      
	echo.
	echo.
	echo.
	
	echo WELCOME TO eG AGENT UPGRADE
	echo ==================================
	echo.
	title eG Upgrade is in progress...


	if exist %EGURKHA_INSTALL_DIR%\tmp\logging del %EGURKHA_INSTALL_DIR%\tmp\logging /q 
	echo   > %EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
	
	if exist %EGURKHA_INSTALL_DIR%\lib\eg_upgrade.jar del %EGURKHA_INSTALL_DIR%\lib\eg_upgrade.jar /Y >nul 2>nul

REM :*******************************************************************************************************************	


echo *********************************************************
echo  Upgrade process might take several minutes to complete.
echo  PLEASE DO NOT INTERRUPT THIS PROCESS.
echo *********************************************************

	
	
REM :*******************************************************************************************************************	
REM : If Upgrade folder is present we need to delete it & take a fresh backup
REM :*******************************************************************************************************************


	if exist %EGURKHA_INSTALL_DIR%\upgrade (
		
		rmdir %EGURKHA_INSTALL_DIR%\upgrade /S /Q >nul 2>nul
		
		)

	if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% (
		
		rmdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% /S /Q >nul 2>nul
		
		)
		
	
	java ZipExtractor eG_Agent_718_1I.zip %EGURKHA_INSTALL_DIR% >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>>%EGURKHA_INSTALL_DIR%\tmp\logging
	if %ERRORLEVEL% GTR 0 (
			echo ERROR Unable to Extract upgrade Zip File %curDir%\eG_Agent_718_1I.zip
			echo Upgrade Failed!!!...
			echo No changes made to the existing system.
			if exist %EGURKHA_INSTALL_DIR%\upgrade rmdir %EGURKHA_INSTALL_DIR%\upgrade /q  /s >nul >nul

			echo ERROR Unable to Extract upgrade Zip File %curDir%\eG_Agent_718_1I.zip >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
			echo Upgrade Failed!!!... >%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event

			goto end
		)


	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0 (
			echo ERROR Unable to Extract upgrade Zip File %curDir%\eG_Agent_718_1I.zip
			echo Error in extracting upgrade zip file!!!
			echo No changes made to the existing system.
			type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event

			if exist %EGURKHA_INSTALL_DIR%\upgrade rmdir %EGURKHA_INSTALL_DIR%\upgrade /q  /s >nul >nul

			echo ERROR Unable to Extract upgrade Zip File %curDir%\eG_win2016_x32_eG6.3.1.zip >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
			echo Error in upgrade zip file!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event

			goto end 
		) 

	if not exist %EGURKHA_INSTALL_DIR%\upgrade (
		echo ERROR Unable to Extract upgrade Zip File %curDir%\eG_Agent_718_1I.zip
		echo Upgrade Failed!!!...
		echo No changes made to the existing system.
		goto end
	)


	echo Agent Upgrade started at %date% %time% >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
	echo.
	echo.

	echo.
	echo Starting upgrade of the eG Agent ...

	echo Stopping the eG Agent ...
	if exist %EGURKHA_INSTALL_DIR%\agent net stop eGAgentmon >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>nul
	%EGURKHA_INSTALL_DIR%\bin\egwait 30 
	
	echo *********************************
	echo The eGAgentMon service stopped..
	echo *********************************
	
	
	if exist %EGURKHA_INSTALL_DIR%\agent net stop egurkhaAgent >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>nul
	%EGURKHA_INSTALL_DIR%\bin\egwait 30 
	echo ************************************
	echo The eGurkhaAgent service stopped...
	echo ************************************
	echo ***********************************************
	echo The eG Agent has been successfully stopped.
	echo ***********************************************



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



REM :*******************************************************************************************************************	
REM : eG Agent Backup Process Start
REM :*******************************************************************************************************************

REM	if not exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% (
					
					echo Backup of the eG Agent started ...
					echo Backup Begins ..>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
REM :*******************************************************************************************************************	
REM : creating %BACKUP_DIR%\bin begins....
REM :*******************************************************************************************************************

mkdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\bin >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>>%EGURKHA_INSTALL_DIR%\tmp\logging

	if not exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\bin (
		echo ERROR Unable to Create Directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\bin
		echo Upgrade Failed!!!...
		echo No changes made to existing System!!! 
		echo ERROR Unable to Create Directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\bin  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo No changes made to existing System!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		goto end
		)

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

	if %size% GTR 0	(
		echo ERROR Unable to Create Directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\bin
		echo Upgrade Failed!!!...
		echo No changes made to existing System!!! 
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo ERROR Unable to Create Directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\bin  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo No changes made to existing System!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		goto end
		) 
						
REM :*******************************************************************************************************************	
REM : creating %BACKUP_DIR%\lib begins....
REM :*******************************************************************************************************************

mkdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\lib >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>>%EGURKHA_INSTALL_DIR%\tmp\logging

	if not exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\lib (
		echo ERROR Unable to Create Directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\lib
		echo Upgrade Failed!!!...
		echo No changes made to existing System!!! 
		echo ERROR Unable to Create Directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\lib  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo No changes made to existing System!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% 	rmdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% /s /q 
		goto end
		)
				
	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0	(
		echo ERROR Unable to Create Directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\lib
		echo Upgrade Failed!!!...
		echo No changes made to existing System!!! 
		echo ERROR Unable to Create Directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\lib  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo No changes made to existing System!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% 	rmdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% /s /q 
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		goto end
		) 

mkdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\agent >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>>%EGURKHA_INSTALL_DIR%\tmp\logging

	if not exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\agent (
		echo ERROR Unable to Create Directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\agent
		echo Upgrade Failed!!!...
		echo No changes made to existing System!!! 
		echo ERROR Unable to Create Directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\agent  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo No changes made to existing System!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% 	rmdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% /s /q 
		goto end
		)
				
	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0	(
		echo ERROR Unable to Create Directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\agent
		echo Upgrade Failed!!!...
		echo No changes made to existing System!!! 
		echo ERROR Unable to Create Directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\agent  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo No changes made to existing System!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% 	rmdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% /s /q 
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		goto end
		) 


xcopy %EGURKHA_INSTALL_DIR%\lib %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\lib /E /K /Y  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>>%EGURKHA_INSTALL_DIR%\tmp\logging

	if %ERRORLEVEL% GTR 0 (
		echo ERROR Unable To Back Up %EGURKHA_INSTALL_DIR%\lib 
		echo Upgrade Failed!!!...
		echo No changes made to existing System!!! 

		echo ERROR Unable To Back Up %EGURKHA_INSTALL_DIR%\lib >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!...>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo No changes made to existing System!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event

		if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% 	rmdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% /s /q 
			goto end
		)

	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0	(
		echo ERROR Unable To Back Up %EGURKHA_INSTALL_DIR%\lib 
		echo Upgrade Failed!!!...
		echo No changes made to existing System!!! 

		echo ERROR Unable To Back Up %EGURKHA_INSTALL_DIR%\lib >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!...>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo No changes made to existing System!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% 	rmdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% /s /q 
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
			goto end
		) 

xcopy %EGURKHA_INSTALL_DIR%\bin %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\bin /E /K /Y  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>>%EGURKHA_INSTALL_DIR%\tmp\logging

	if %ERRORLEVEL% GTR 0 (
		echo ERROR Unable To Back Up %EGURKHA_INSTALL_DIR%\bin 
		echo Upgrade Failed!!!...
		echo No changes made to existing System!!! 

		echo ERROR Unable To Back Up %EGURKHA_INSTALL_DIR%\bin >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!...>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo No changes made to existing System!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event

		if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% 	rmdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% /s /q 
			goto end
		)

	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0	(
		echo ERROR Unable To Back Up %EGURKHA_INSTALL_DIR%\bin
		echo Upgrade Failed!!!...
		echo No changes made to existing System!!! 

		echo ERROR Unable To Back Up %EGURKHA_INSTALL_DIR%\bin >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!...>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo No changes made to existing System!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% 	rmdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% /s /q 
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
			goto end
		) 


xcopy %EGURKHA_INSTALL_DIR%\agent %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\agent /E /K /Y  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>>%EGURKHA_INSTALL_DIR%\tmp\logging

	if %ERRORLEVEL% GTR 0 (
		echo ERROR Unable To Back Up %EGURKHA_INSTALL_DIR%\agent 
		echo Upgrade Failed!!!...
		echo No changes made to existing System!!! 

		echo ERROR Unable To Back Up %EGURKHA_INSTALL_DIR%\agent >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!...>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo No changes made to existing System!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event

		if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% 	rmdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% /s /q 
			goto end
		)

	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0	(
		echo ERROR Unable To Back Up %EGURKHA_INSTALL_DIR%\agent
		echo Upgrade Failed!!!...
		echo No changes made to existing System!!! 

		echo ERROR Unable To Back Up %EGURKHA_INSTALL_DIR%\agent >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!...>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo No changes made to existing System!!! >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% 	rmdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% /s /q 
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
			goto end
		) 


	echo Backup of the eG Agent completed 
	echo Backup of the eG Agent completed   >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event



REM :*******************************************************************************************************************	
REM :UPGRADE operation Starts.....................................
REM :*******************************************************************************************************************

	echo Upgrading the eG Agent configuration ...
	echo Upgrading the eG Agent configuration ...>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
	
	
REM :*******************************************************************************************************************	
REM :copying new files to %EGURKHA_INSTALL_DIR%\egcli begins....
REM :*******************************************************************************************************************


xcopy %EGURKHA_INSTALL_DIR%\upgrade\lib %EGURKHA_INSTALL_DIR%\lib /E /K /Y >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>%EGURKHA_INSTALL_DIR%\tmp\logging

	if %ERRORLEVEL% GTR 0 (
		echo ERROR : Unable to Copy %EGURKHA_INSTALL_DIR%\upgrade\lib\ folder files
		echo Upgrade Failed!!!...
		echo Please refer to file %EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event for further details
		echo Execute eGupgrade.bat With Revert option
		echo ERROR : Unable to Copy %EGURKHA_INSTALL_DIR%\upgrade\lib\ folder files >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 
		echo Execute eGupgrade.bat With Revert option >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		goto end
		)
	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0 (
		echo ERROR : Unable to Copy %EGURKHA_INSTALL_DIR%\upgrade\lib\ folder files
		echo Upgrade Failed!!!...
		echo Please refer to file %EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event for further details
		echo Execute eGupgrade.bat With Revert option
		echo ERROR : Unable to Copy %EGURKHA_INSTALL_DIR%\upgrade\lib\ folder files >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Execute eGupgrade.bat With Revert option  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
			goto end
		) 
xcopy %EGURKHA_INSTALL_DIR%\upgrade\bin %EGURKHA_INSTALL_DIR%\bin /E /K  /Y >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>%EGURKHA_INSTALL_DIR%\tmp\logging

	if %ERRORLEVEL% GTR 0 (
		echo ERROR : Unable to Copy %EGURKHA_INSTALL_DIR%\upgrade\bin\ folder files
		echo Upgrade Failed!!!...
		echo Please refer to file %EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event for further details
		echo Execute eGupgrade.bat With Revert option
		echo ERROR : Unable to Copy %EGURKHA_INSTALL_DIR%\upgrade\bin\ folder files >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 
		echo Execute eGupgrade.bat With Revert option >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		goto end
		)
	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0 (
		echo ERROR : Unable to Copy %EGURKHA_INSTALL_DIR%\upgrade\bin\ folder files
		echo Upgrade Failed!!!...
		echo Please refer to file %EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event for further details
		echo Execute eGupgrade.bat With Revert option
		echo ERROR : Unable to Copy %EGURKHA_INSTALL_DIR%\upgrade\bin\ folder files >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Execute eGupgrade.bat With Revert option  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
			goto end
		) 

xcopy %EGURKHA_INSTALL_DIR%\upgrade\agent %EGURKHA_INSTALL_DIR%\agent /E /K /Y >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>%EGURKHA_INSTALL_DIR%\tmp\logging

	if %ERRORLEVEL% GTR 0 (
		echo ERROR : Unable to Copy %EGURKHA_INSTALL_DIR%\upgrade\agent\ folder files
		echo Upgrade Failed!!!...
		echo Please refer to file %EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event for further details
		echo Execute eGupgrade.bat With Revert option
		echo ERROR : Unable to Copy %EGURKHA_INSTALL_DIR%\upgrade\agent\ folder files >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 
		echo Execute eGupgrade.bat With Revert option >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		goto end
		)
	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0 (
		echo ERROR : Unable to Copy %EGURKHA_INSTALL_DIR%\upgrade\agent\ folder files
		echo Upgrade Failed!!!...
		echo Please refer to file %EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event for further details
		echo Execute eGupgrade.bat With Revert option
		echo ERROR : Unable to Copy %EGURKHA_INSTALL_DIR%\upgrade\agent\ folder files >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Upgrade Failed!!!... >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Execute eGupgrade.bat With Revert option  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
			goto end
		) 
%EGURKHA_INSTALL_DIR%\jre\bin\java eG_Agent_718_1I %EGURKHA_INSTALL_DIR%\ >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>%EGURKHA_INSTALL_DIR%\tmp\logging

	title eG Agent has been upgraded
	echo.
	echo.
	echo ******************************************************************
	echo The eG Agent upgrade has been completed successfully!!!
	echo Please start the agent.
	echo ******************************************************************

	echo.
	echo.
	echo.
	echo.
	goto End



REM ****************************************************
REM This will revert the eG Agent to Previous Version
REM ****************************************************

:Revert
if exist %EGURKHA_INSTALL_DIR%\tmp\logging del %EGURKHA_INSTALL_DIR%\tmp\logging /q 
	
	if not exist %EGURKHA_INSTALL_DIR%\upgrade ( 
			echo No Upgrades to Revert
			goto end
		) else (
			title eG Revert is in progress...

			echo WELCOME TO REVERT OPERATION
			echo ===========================
			echo WELCOME TO REVERT OPERATION >> %EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
			echo =========================== >> %EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
			echo.
			echo Reverting the eG agent to its original configuration.
			echo.	
			
			echo ******************************************************
			echo Revert process might take several minutes to complete.
			echo PLEASE DO NOT INTERRUPT THIS PROCESS.
			echo ******************************************************

				
			if exist %EGURKHA_INSTALL_DIR%\agent net stop egurkhaAgent >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2>nul
			%EGURKHA_INSTALL_DIR%\bin\egwait 30 
			echo ************************************
			echo The eGurkhaAgent service stopped...
			echo ************************************
			echo ***********************************************
			echo The eG Agent has been successfully stopped.
			echo ***********************************************



REM :*******************************************************************************************************************
REM :  Restoring the files from the backup...
REM :*******************************************************************************************************************	

REM :*******************************************************************************************************************
REM :  Restoring BIN from the backup...
REM :*******************************************************************************************************************	

if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\bin rmdir %EGURKHA_INSTALL_DIR%\bin  /s /q >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2> %EGURKHA_INSTALL_DIR%\tmp\logging
			
	if %ERRORLEVEL% NEQ 0 (
		echo ERROR : Failed to remove bin created during upgrade!!!!
		echo ERROR : Failed to remove bin created during upgrade!!!!>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end
		)
	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0 (
		echo Revert failed!!!..
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end 
		) 

if not exist %EGURKHA_INSTALL_DIR%\bin mkdir %EGURKHA_INSTALL_DIR%\bin  

	if not exist %EGURKHA_INSTALL_DIR%\bin (
		echo ERROR : Failed to create bin !!!!
		echo ERROR : Failed to create bin!!!!>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end
		)
	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0 (
		echo Revert failed!!!..
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end 
		) 

xcopy %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\bin %EGURKHA_INSTALL_DIR%\bin /E /K /Y /R >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2> %EGURKHA_INSTALL_DIR%\tmp\logging

	if %ERRORLEVEL% NEQ 0 (
		echo ERROR : Failed to Revert bin!!!!
		echo ERROR : Failed to Revert bin!!!!>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end
		)
	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0 (
		echo Revert failed!!!..
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end 
		) 

REM :*******************************************************************************************************************
REM :  Restoring LIB from the backup...
REM :*******************************************************************************************************************	

if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\lib rmdir %EGURKHA_INSTALL_DIR%\lib  /s /q  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2> %EGURKHA_INSTALL_DIR%\tmp\logging

	if %ERRORLEVEL% NEQ 0 (
		echo ERROR : Failed to remove lib created during upgrade!!!!
		echo ERROR : Failed to remove lib created during upgrade!!!!>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end
		)
	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0 (
		echo Revert failed!!!..
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end 
		) 

if not exist %EGURKHA_INSTALL_DIR%\lib mkdir %EGURKHA_INSTALL_DIR%\lib 

	if not exist %EGURKHA_INSTALL_DIR%\lib (
		echo ERROR : Failed to create lib  during revert!!!!
		echo ERROR : Failed to create lib  during revert!!!!>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end
		)
	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0 (
		echo Revert failed!!!..
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end 
		) 

xcopy %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\lib %EGURKHA_INSTALL_DIR%\lib /E /K /Y /R >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2> %EGURKHA_INSTALL_DIR%\tmp\logging
				
	if %ERRORLEVEL% NEQ 0 (
		echo ERROR : Failed to Revert lib!!!!
		echo ERROR : Failed to Revert lib!!!!>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end
		)
	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0 (
		echo Revert failed!!!..
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end 
		) 

REM :*******************************************************************************************************************
REM :  Restoring Agent from the backup...
REM :*******************************************************************************************************************	

if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\agent (

xcopy %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%\agent\config\*.*  %EGURKHA_INSTALL_DIR%\agent\config\ /E /K /Y /R >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event 2> %EGURKHA_INSTALL_DIR%\tmp\logging
				
	if %ERRORLEVEL% NEQ 0 (
		echo ERROR : Failed to Revert agent!!!!
		echo ERROR : Failed to Revert agent!!!!>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end
		)
	for %%F in (%EGURKHA_INSTALL_DIR%\tmp\logging) do set size=%%~zF
	if %size% GTR 0 (
		echo Revert failed!!!..
		type %EGURKHA_INSTALL_DIR%\tmp\logging >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo Please contact support@eginnovations.com for further assistance
		goto end 
		) 
)


	title eG Agent has been reverted.
	if exist %EGURKHA_INSTALL_DIR%\upgrade rmdir %EGURKHA_INSTALL_DIR%\upgrade /S /Q >nul 2>nul

	if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% (
		rmdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% /S /Q >nul
	)

	echo ***********************************************************************************
	echo Successfully reverted the eG Agent to the previous version!
	echo Please execute the command %EGURKHA_INSTALL_DIR%\lib\start_agent to start  eG Agent.
	echo ***********************************************************************************
)
	goto end


REM : ***********************************************************
REM : This will Commit the eG agent and initiate Agent upgrade 
REM : ***********************************************************


:Commit

	echo *********************************************************** >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
	echo In Commit Operation >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event

	if not exist %EGURKHA_INSTALL_DIR%\upgrade (
		echo.
		echo No Upgrades to Commit
		echo No Upgrades to Commit  >%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		goto end
	 	) 
	else (
		if exist %EGURKHA_INSTALL_DIR%\tmp\logging del %EGURKHA_INSTALL_DIR%\tmp\logging /q >nul

		cls
      		echo.
		echo.
		echo.
		title eG Commit is in progress...

		echo WELCOME TO COMMIT OPERATION
		echo ===========================

		echo WELCOME TO COMMIT OPERATION >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo =========================== >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
		echo.
		echo.	
		echo Committing the eG Agent Upgrade Changes ....
		echo.


	if exist %EGURKHA_INSTALL_DIR%\upgrade rmdir %EGURKHA_INSTALL_DIR%\upgrade /S /Q >nul 2>nul

	if exist %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% (
					
		rmdir %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% /S /Q >nul
	
		if %ERRORLEVEL% GTR 0 (
			echo ERROR : Unable to Delete Back up directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%
			echo please close all applications and delete %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% manually
			echo ERROR : Unable to Delete Back up directory %EGURKHA_INSTALL_DIR%\%BACKUP_DIR%  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
			echo please close all applications and delete %EGURKHA_INSTALL_DIR%\%BACKUP_DIR% manually  >>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
			goto end
			)
		)
			
	
	title eG Agent has been committed.
	echo.

	echo *********************************************
	echo  Successfully Committed the eG Agent Upgrade.
	echo *********************************************

	echo ******************************************************************>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
	echo The eG Agent commit has been completed successfully!!!>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
	echo please start the eG Agent>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
	echo using the command %EGURKHA_INSTALL_DIR%\lib\start_agent.>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event
	echo ******************************************************************>>%EGURKHA_INSTALL_DIR%\agent\logs\upgrade_event

:End  
	echo.
	@pause
	
ENDLOCAL

