@echo off

 if "%~d0" == "\\" (
	echo.
	echo The update 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 update 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 "%TCFITDIR%" NEQ "" goto tc
if "%FITDIR%" NEQ "" goto fit

echo "FITDIR nor TCFITDIR found!"

"%DIR%\..\JRE_WIN\bin\java" -cp "%CP%" %* -DSERVICENAME="SFCMTCService" -DINST_SYS=fit4tc -DINST_OS=msw -DINST_DIR="%DIR%" -Dant.home="%DIR%/install" org.apache.tools.ant.launch.Launcher -buildfile "%DIR%/install/ant_update.xml" doUpdateStandAlone

goto end


:fit 
 title Update FIT MI Server
 echo "FITDIR found"
 echo.
 echo Start update of FIT MI Server...
 echo.

 echo  - Installed service name is FITMITomcat5
 echo  - Stopping Service...
 net stop FITMITomcat5
 timeout /t 10

 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 --- for 64bit: Additional modifications in update script are required. Disabled until fixed. 
 rem if exist "%FITDIR%\run\bin\java\jdk64" set JAVA_HOME=%FITDIR%\run\bin\java\jdk64

 "%JAVA_HOME%/bin/java" -cp "%CP%" -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_update.xml" update

 echo.
 echo ===============================================
 echo.
 echo Update finished!
 echo.
 echo Please start the Fit-MI Tomcat Service

 goto end
	
:tc	
 title Update MachineToolClient Service
 echo "TCFITDIR found"
 echo.
 echo Start update of MachineToolClient Service...
 echo.

 echo Check installed Machine Tool Client Service Configuration
 sc query FITMITomcat5 | find "SERVICE_NAME" >nul
 if %ERRORLEVEL% EQU 0 set SERVICENAME=FITMITomcat5

 sc query SFCMTCService | find "SERVICE_NAME" >nul
 if %ERRORLEVEL% EQU 0 set SERVICENAME=SFCMTCService

 if "%SERVICENAME%" NEQ "" (
 	echo  - Installed service name is %SERVICENAME%
	echo  - Stopping Service...
	net stop %SERVICENAME%
	echo.
	timeout /t 10
 ) else (
	echo =========================================================================
	echo Update following MTC Server installation:
	echo %TCFITDIR%
	echo.
	echo SFC MTC services not found! Update is not able to stop SFC MTC services!
	echo.
	echo +---------------------------------------------------------------------+
	echo ^| Please stop all running MTC services before continuing this update! ^|
	echo ^|                                                                     ^|
	echo ^| - Machine Tool Client Service                                       ^|
	echo +---------------------------------------------------------------------+
	echo.
	echo =========================================================================
	pause
 )
	
 "%TCFITDIR%/java/j2sdk/jre/bin/java" -cp "%CP%" -DSERVICENAME="%SERVICENAME%" -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_update.xml" update
	
 echo.
 echo ===============================================
 echo.
 echo Update finished!
 echo.

 if "%SERVICENAME%" EQU "FITMITomcat5" goto m1 
 if "%SERVICENAME%" EQU "SFCMTCService" goto m2

 goto end
	
:m1
 echo Please start the FIT MI Apache Tomcat Service
 goto end
	
:m2
 echo Please start the SFC_MTCService

:end
 echo.
 echo ===============================================

 pause
