@echo off

 if "%~d0" == "\\" (
	echo.
	echo The install script can not be launched from a UNC path.
	echo Please start over a drive letter!
	echo.
	goto end
 )

	rem -- Check for admin rights
	mkdir "%windir%\BatchGotAdmin"
	if '%errorlevel%' == '0' (
	  rmdir "%windir%\BatchGotAdmin" & goto checked
	) else ( 
		echo.
		echo The install script requires admin rights.
		echo Please run as Administrator!
		echo.
		pause
		exit
	)


:checked

	rem -- Save currect directory (%CD% not used due to UAC)
	rem -- ~dp0 contains \ at end of path!
	set DIR=%~dp0
	rem -- removing trailing slash
	set DIR=%DIR:~0,-1%

	set CP=%DIR%/install/ant-launcher.jar

if "%FITDIR%" NEQ "" goto fit
if "%TCFITDIR%" NEQ "" goto tc

:installStandAlone
	echo "Starting Standalone installation..."
	echo " (Because Environment variables FITDIR or TCFITDIR not found)"
	
	set CMDLINE_ARGS=-DSERVICENAME="SFCMTCService" -Dj.home="%TCFITDIR%/java/j2sdk" -DINST_SYS=fit4tc -DINST_OS=msw -DINST_DIR="%DIR%" -Dant.home="%DIR%/install" org.apache.tools.ant.launch.Launcher -buildfile "%DIR%/install/ant_install.xml" doinstallStandAlone
	
	rem -- Use java runtime from distribution media
	"%DIR%\..\JRE_WIN\bin\java" -version > nul 2>&1
	if %ERRORLEVEL% == 0 goto bundled

	"%JRE_HOME%\bin\java" -version > nul 2>&1
	if %ERRORLEVEL% == 0 goto jrehome

	"java" -version > nul 2>&1
	if %ERRORLEVEL% == 0 goto installed

	echo.
	echo ****************************************************
	echo * ERROR: No Java Runtime found to execute install! *
	echo *        Please install Java Runtime Environment   *
	echo ****************************************************
	echo.
	goto end
	
:bundled	
	"%DIR%\..\JRE_WIN\bin\java" -cp "%CP%" %* %CMDLINE_ARGS%
	goto end

:jrehome
	"%JRE_HOME%\bin\java" -cp "%CP%" %* %CMDLINE_ARGS%
	goto end

:installed

	"java" -cp "%CP%" %* %CMDLINE_ARGS%
	goto end


:fit 
	echo "FITDIR found"
	
	if exist "%FITDIR%\run\bin\java\j2sdk1.4.2_02" set JAVA_HOME=%FITDIR%\run\bin\java\j2sdk1.4.2_02
	if exist "%FITDIR%\run\bin\java\jdk" set JAVA_HOME=%FITDIR%\run\bin\java\jdk
	rem --- 64bit disabled until update script is modified!
	rem if exist "%FITDIR%\run\bin\java\jdk64" set JAVA_HOME=%FITDIR%\run\bin\java\jdk64
	
	"%JAVA_HOME%\bin\java" -cp "%CP%" -Dj.home="${JAVA_HOME}" -DSERVICENAME="FITMITomcat5" -DINST_OS=msw -DINST_SYS=fit -DINST_DIR="%DIR%" -DDST_DIR="%FITDIR%/run/bin/FIT-MI_Server" -DBASE_DIR="%FITDIR%" -Dant.home="%DIR%/install" org.apache.tools.ant.launch.Launcher -buildfile "%DIR%/install/ant_install.xml" doinstall
	goto end
	
:tc	
	echo "TCFITDIR found"
	
	"%TCFITDIR%/java/j2sdk/jre/bin/java" -cp "%CP%" -Dj.home="%TCFITDIR%/java/j2sdk" -DSERVICENAME="SFCMTCService" -DINST_SYS=fit4tc -DINST_OS=msw -DINST_DIR="%DIR%" -DDST_DIR="%TCFITDIR%/FIT-MI_Server" -DBASE_DIR="%TCFITDIR%" -Dant.home="%DIR%/install" org.apache.tools.ant.launch.Launcher -buildfile "%DIR%/install/ant_install.xml" doinstall
	
:end
pause
