@ECHO OFF
set mname=
set ecpath=
chcp 65001 >NUL
chcp 1252 >NUL

SET /P mname=Do you want to install eclipse Plugin for Avaya Breeze(R) platform[y/n]:

IF "%mname%"=="Y" GOTO install_eclipse_plugin
IF "%mname%"=="y" GOTO install_eclipse_plugin
IF not "%mname%"=="y" GOTO Error
IF not "%mname%"=="Y" GOTO Error

:install_eclipse_plugin

	set /p ecpath=Enter eclipse Install Directory [c:\eclipse\]:
	
	IF NOT  "%ecpath%"=="" (
	set ecpath=%ecpath:"=%
	)
	
	IF "%ecpath%"=="" GOTO SET_DEF
	
	GOTO IN_PLUGIN	
	
	:SET_DEF
		set ecpath=c:\eclipse
		
	:IN_PLUGIN
	
		IF EXIST "%ecpath%\plugins"  GOTO PROCEED
			echo plugins folder not found in the target directory
			IF EXIST "%ecpath%\dropins" GOTO PROCEED
			echo dropins folder not found in the target directory
			GOTO Error
		
	:PROCEED
		set Install_Path=%~d0%~p0
		echo Install_Path=%Install_Path%
		
		IF EXIST "%Install_Path%\tmp" (
			cd \tmp
		
			IF EXIST "%Install_Path%\tmp\com.avaya.zephyr.eclipse.plugin.jar" (	
				del "%Install_Path%\tmp\com.avaya.zephyr.eclipse.plugin.jar"
			)
		
			IF EXIST "%Install_Path%\tmp\META-INF\MANIFEST.MF" (	
				del "%Install_Path%\tmp\META-INF\MANIFEST.MF"
			)
		)
		
		
		REM timeout 5
		cd %Install_Path%
		IF NOT EXIST "%Install_Path%\tmp" (
		md tmp
		)
		copy lib\com.avaya.zephyr.eclipse.plugin.jar "%Install_Path%\tmp"
		cd tmp
			
		jar -xvf com.avaya.zephyr.eclipse.plugin.jar META-INF/MANIFEST.MF
		
		REM Read Bundle-Version for new plug-in
		
		setlocal EnableDelayedExpansion
		
		FOR /F "usebackq tokens=1* delims==:" %%i IN ("%Install_Path%\tmp\META-INF\MANIFEST.MF") DO set "%%i=%%j"
						
		set RELEASE_BUNDLE_VERSION= %Bundle-Version%
		echo "Release Bundle Version is ::%RELEASE_BUNDLE_VERSION%"
			
		FOR /F "usebackq tokens=1,2,3,4 delims=. " %%a in ('%RELEASE_BUNDLE_VERSION%') do set  major=%%a&set  minor=%%b&set  micro=%%c&set  qualifier=%%d
		echo "MAJOR_VERSION: %major%"
		echo "MINOR_VERSION: %minor%"
		echo "MICRO_VERSION: %micro%"
		echo "QUALIFIER: %qualifier%"
			
		FOR /F "usebackq tokens=1,2 delims=- " %%a in ('%qualifier%') do set qualifier_version=%%a&set release=%%b
		echo "QUALIFIER_VERSION: %qualifier_version%"
		echo "RELEASE: %release%"
						
		REM Read Bundle Version in deployed Plug-in 
		set IS_AVAIL=
		IF EXIST "%ecpath%\plugins\com.avaya.zephyr.eclipse.plugin.jar" set IS_AVAIL=Y
		IF EXIST "%ecpath%\dropins\com.avaya.zephyr.eclipse.plugin.jar" set IS_AVAIL=Y
		
		IF "%IS_AVAIL%"=="Y" (
		echo "Plug-in already deployed checking for Bundle Version!"
		del "%Install_Path%\tmp\com.avaya.zephyr.eclipse.plugin.jar"
		del "%Install_Path%\tmp\META-INF\MANIFEST.MF"
		REM timeout 5
		IF EXIST "%ecpath%\plugins\com.avaya.zephyr.eclipse.plugin.jar" (
			copy "%ecpath%\plugins\com.avaya.zephyr.eclipse.plugin.jar" "%Install_Path%\tmp"
			)
		IF EXIST "%ecpath%\dropins\com.avaya.zephyr.eclipse.plugin.jar" (
			copy "%ecpath%\dropins\com.avaya.zephyr.eclipse.plugin.jar" "%Install_Path%\tmp"
			)			
		jar -xvf com.avaya.zephyr.eclipse.plugin.jar META-INF/MANIFEST.MF
		REM timeout 5
		set Bundle-Version=
						
		FOR /F "usebackq tokens=1* delims==:" %%i IN ("%Install_Path%\tmp\META-INF\MANIFEST.MF") DO set "%%i=%%j"	
						
		set DEPLOYED_BUNDLE_VERSION=!Bundle-Version!
		echo DEPLOYED Bundle Version is :: !DEPLOYED_BUNDLE_VERSION!
		cd %Install_Path%
		rmdir /s /q tmp
		) ELSE (
				echo "Plug-in not installed previously on specified eclipse!"
				cd %Install_Path%
				rmdir /s /q tmp
				GOTO CopyEclipsePlugin
			)
			
			
		FOR /F "usebackq tokens=1,2,3,4 delims=. " %%a in ('%DEPLOYED_BUNDLE_VERSION%') do set deployed_major=%%a&set deployed_minor=%%b&set deployed_micro=%%c&set deployed_qualifier=%%d
		
		echo.DEPLOYED_MAJOR_VERSION: %deployed_major%
		echo.DEPLOYED_MINOR_VERSION: %deployed_minor%
		echo.DEPLOYED_MICRO_VERSION: %deployed_micro%
		echo.DEPLOYED_QUALIFIER: %deployed_qualifier%
		 
	
		FOR /F "usebackq tokens=1,2 delims=- " %%a in ('%deployed_qualifier%') do set deployed_qualifier_version=%%a&set deployed_release=%%b
		echo.DEPLOYED_QUALIFIER_VERSION: %deployed_qualifier_version%
		echo.DEPLOYED_RELEASE: %deployed_release%
		
		REM verify that release plug-in is not older plug-in 
			
		
		IF EXIST "%ecpath%\plugins\com.avaya.zephyr.eclipse.plugin.jar" (
			if %major% GTR %deployed_major% (
				echo "Deployed plug-in major version is lower than major version of new plug-in!"
				if %release%==SNAPSHOT GOTO CopyingSnapshotVersionEclipsePlugin
				echo "Replacing old plug-in with new plug-in".
				GOTO CopyEclipsePlugin
				) ELSE (if %deployed_major% GTR %major% (
				echo "Deployed plug-in major version is higher than major version of new plug-in!" 
				GOTO WarningReleasePluginOld
				)
				)
			if %minor% GTR %deployed_minor% (
				echo "Deployed plug-in minor version is lower than minor version of new plug-in!"
				if %release%==SNAPSHOT GOTO CopyingSnapshotVersionEclipsePlugin
				echo "Replacing old plug-in with new plug-in".
				GOTO CopyEclipsePlugin
				) ELSE (if %deployed_minor% GTR %minor% (
				echo "Deployed plug-in minor version is higher than minor version of new plug-in!" 
				GOTO WarningReleasePluginOld
				))
			if %micro% GTR %deployed_micro% (
				echo "Deployed plug-in micro version is lower than micro version of new plug-in!"
				if %release%==SNAPSHOT GOTO CopyingSnapshotVersionEclipsePlugin
				echo "Replacing old plug-in with new plug-in".
				GOTO CopyEclipsePlugin
				) ELSE (if %deployed_micro% GTR %micro% (
				echo "Deployed plug-in micro version is higher than micro version of new plug-in!" 
				GOTO WarningReleasePluginOld
				))
			if %qualifier_version% GTR %deployed_qualifier_version% (
				echo "Deployed plug-in micro version is lower than micro version of new plug-in!"
				if %release%==SNAPSHOT GOTO CopyingSnapshotVersionEclipsePlugin
				echo "Replacing old plug-in with new plug-in".
				GOTO CopyEclipsePlugin
				) ELSE (if %deployed_qualifier_version% GTR %qualifier_version% (
				echo "Deployed plug-in qualifier version is higher than qualifier version of new plug-in!" 
				GOTO WarningReleasePluginOld
				))
			if %deployed_release%==SNAPSHOT GOTO CopyEclipsePlugin
			if %release%==SNAPSHOT GOTO CopyingSnapshotVersionEclipsePlugin
			if %release% GTR %deployed_release% (
				echo "Deployed plug-in release version is lower than release version of new plug-in!"
				echo "Replacing old plug-in with new plug-in".
				GOTO CopyEclipsePlugin
				) ELSE (if %deployed_release% GTR %release% (
				echo "Deployed plug-in release version is higher than release version of new plug-in!" 
				GOTO WarningReleasePluginOld
				))
			
			if %deployed_major%==%major% (
				if %deployed_minor%==%minor% (
					if %deployed_micro%==%micro% (
						if %deployed_qualifier_version%==%qualifier_version% (
							if %deployed_release%==%release% GOTO SameReleaseAndDeployedVersionWarning
								)
							)
						)
					)
					echo Eclipse-release plug-in version is older.
					 GOTO WarningReleasePluginOld
				)
		
		

	:SameReleaseAndDeployedVersionWarning
	echo Deployed plug-in and release plug-in both has same Bundle-Version.Replacing deployed plug-in with release plug-in.
	GOTO CopyEclipsePlugin

	:WarningReleasePluginOld
	SET /P input=Do you want to install old eclipse Plug-in for Avaya Breeze(R) platform[y/n]:

	IF "%input%"=="Y" GOTO CopyEclipsePlugin
	IF "%input%"=="y" GOTO CopyEclipsePlugin
	IF "%input%"=="n" GOTO Error
	IF "%input%"=="N" GOTO Error
	IF not "%input%"=="y" GOTO Error
	IF not  "%input%"=="Y" GOTO Error



	:CopyingSnapshotVersionEclipsePlugin

	SET /P input=Do you want to install eclipse Plug-in snapshot version for Avaya Breeze(R) platform[y/n]:

	IF "%input%"=="Y" GOTO CopyEclipsePlugin
	IF "%input%"=="y" GOTO CopyEclipsePlugin
	IF "%input%"=="n" GOTO Error
	IF "%input%"=="N" GOTO Error
	IF not "%input%"=="y" GOTO Error
	IF not  "%input%"=="Y" GOTO Error


	:CopyEclipsePlugin
	echo Copying eclipse plugin/dropins to the specified eclipse plug-in directory!
	cd %Install_Path%
	IF EXIST "%ecpath%\plugins" (
		copy lib\com.avaya.zephyr.eclipse.plugin.jar "%ecpath%\plugins\"
		copy eclipse-bundles\*.jar "%ecpath%\plugins\"
		)
	
	IF EXIST "%ecpath%\dropins" (
		copy lib\com.avaya.zephyr.eclipse.plugin.jar "%ecpath%\dropins\"
		copy eclipse-bundles\*.jar "%ecpath%\dropins\"
		)
	
	SET /P input=Eclipse plugin installation needs eclipse to start in clean mode. Do you want to add the '-clean' option to eclipse.ini file ? [y/n]:
	SET userInput=N
	IF "%input%" == "Y" SET userInput=Y
	IF "%input%" == "y" SET userInput=Y
	IF "%userInput%" == "Y" (
		FIND /C /I "-clean" "%ecpath%\eclipse.ini"
		IF NOT !ERRORLEVEL! EQU 0 ( 
			(echo -clean) > "%ecpath%\tmpeclipse.ini"
			type "%ecpath%\eclipse.ini" >> "%ecpath%\tmpeclipse.ini"
			move /y "%ecpath%\tmpeclipse.ini" "%ecpath%\eclipse.ini"
		)
	) ELSE (
		SET /P input="Please start Eclipse with -clean option for all your existing eclipse Workspaces for this plugin to take effect. Hit enter to continue."
	)
		
	IF NOT EXIST "%ecpath%\plugins\com.avaya.zephyr.eclipse.plugin.jar" (
		IF NOT EXIST "%ecpath%\dropins\com.avaya.zephyr.eclipse.plugin.jar" (
			GOTO FAIL_COPY
			)
		)	
				
	echo Eclipse plugin installation Succeeded.Restart your eclipse to load the new plugin.

	GOTO End
		

	:FAIL_COPY
	echo Failed to copy com.avaya.zephyr.eclipse.plugin.jar on to target directory :%ecpath%\plugins
	GOTO End

	:Error
	echo Exiting without eclipse plugin installation
	:End
