<?xml version="1.0"?>

<project default="update" basedir="." xmlns:if="ant:if" xmlns:unless="ant:unless">
	
	<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
	<taskdef resource="com/sardak/antform/taskdefs.properties"/>
	<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
	<taskdef name="RequireConfirmOverwrite" classname="com.aplusb.utils.AntSetupTaks.RequireConfirmOverwrite"/>
	
    <macrodef name="serviceStop">
        <sequential>
        	<echo message="Stop running service using shutdown script..." />
        	<exec executable="shutdown.bat" dir="${DST_DIR}\Tomcat 5.0\bin" resolveexecutable="true" osfamily="windows" discardError="true"/>
        	<exec executable="shutdown.sh" dir="${DST_DIR}/Tomcat 5.0/bin" resolveexecutable="true" osfamily="unix" discardError="true"/>

        	<echo message="Wait 10 seconds for service shutdown complete..." />
			<sleep seconds="10" />
        </sequential>
    </macrodef>

	<tstamp/>


	<loadfile property="VERSION" srcFile="${INST_DIR}/version.txt">
		<filterchain>
			<striplinebreaks/>
		</filterchain>
	</loadfile>
	
	<if>
		<equals arg1="${INST_OS}" arg2="msw" />
		<then>
			<property name="isMsWin" value="true" />
			<property name="isUnix" value="false" />
		</then>
		<else>
			<property name="isUnix" value="true" />
			<property name="isMsWin" value="false" />
		</else>
	</if>

	<target name="onCancel">
		<fail message="Update canceled!"/>
	</target>

	<target name="doUpdateStandAlone">

		<if>
			<isset property="sfcMtcInstallationPath"></isset>
			<then>
				<echo message="Received installation path via command line: ${sfcMtcInstallationPath}"/>
			</then>
			<else>
				<fail message="Missing '-DsfcMtcInstallationPath' and silent installation mode enabled! Can't start update!" if:true="${silent}" />
				
				<antform title="Select installation directory" image="install/siemens_ab_logo_trans_klein.png">
					<label>Please select the base directory to update SFC MTC Service.</label>
					<label>A subdirectory named "ShopFloorConnect" is inside this directory.</label>
					<separator/>
					<fileSelectionProperty label="Base directory: " 
						property="sfcMtcInstallationPath" 
						directoryChooser="true" Required="true"/>
					<controlbar>
						<button label="OK" type="ok"></button>
						<button label="Reset values" type="reset"></button>
						<button label="Cancel" type="cancel" target="onCancel"></button>
					</controlbar>
				</antform>
				
			</else>
		</if>
			
		<if>
			<matches string="${sfcMtcInstallationPath}" pattern="^.+ShopFloorConnect$"/>
			<then>
				<property name="BASE_DIR" value="${sfcMtcInstallationPath}" />
			</then>
			<else>
				<property name="BASE_DIR" value="${sfcMtcInstallationPath}${file.separator}ShopFloorConnect" />
			</else>
		</if>
		
		<fail message="Installation path '${BASE_DIR}' doesn't exists!">
			<condition>
				<not><available file="${BASE_DIR}" type="dir" /></not>
			</condition>
		</fail>

		<if>
			<available file="${BASE_DIR}${file.separator}webapps" type="dir" />
			<then>
				<property name="isStandAlone" value="false" />
			</then>
			<else>
				<property name="isStandAlone" value="true" />
			</else>
		</if>

		<!-- Set logfile. BASE_DIR points to (TC)FITDIR -->
		<if>
			<equals arg1="${INST_SYS}" arg2="fit4tc" />
			<then>
				<property name="update.log.filename" value="${BASE_DIR}/log/update_MTC_${DSTAMP}${TSTAMP}.log"/>
			</then>
			<else>
				<property name="update.log.filename" value="${BASE_DIR}/run/log/update_MIServer_${DSTAMP}${TSTAMP}.log"/>
			</else>
		</if>
		
		<record name="${update.log.filename}"
		    loglevel="info" append="true"/>

		<echo message="===================================================================="/>
		<echo message="Update Shop Floor Connect Machine Tool Client Service to ${VERSION}"/>
		<echo message="===================================================================="/>

		<echo message="Installaton path:    ${BASE_DIR}"/>
		<echo message="===================================================================="/>

		<!-- Find server dir which contains SFC_base.jar.xz -->
		<last id="first">
			<sort id="src.files">
				<dirset id="dist.contents" dir="${INST_DIR}${file.separator}.." includes="Server*"/>
				<date />
			</sort>
		</last>
		<property name="prop.dist.contents" refid="dist.contents"/>
		<property name="BASE_JAR_DIR" value="${toString:first}"></property>
		
		<echo message="Base package dir:    ${BASE_JAR_DIR}"/>
		<echo message="===================================================================="/>

		<if>
			<contains string="${BASE_JAR_DIR}" substring="server" casesensitive="false"/>
			<then>
			</then>
			<else>
				<fail message="No Server directory found in '${INST_DIR}${file.separator}..' to extract base package SFC_Base" />
			</else>
		</if>

		<input message="Press enter to continue" unless:true="${silent}"/>

		<property name="DST_DIR" value="${BASE_DIR}${file.separator}FIT-MI_Server"></property>
		<antcall target="update" />
	</target>

	<target name="update" >
		
		<property name="TEMP_DIR" value="${BASE_DIR}/update_temp" />
		<!-- Set logfile. BASE_DIR points to (TC)FITDIR -->
		<if>
			<equals arg1="${INST_SYS}" arg2="fit4tc" />
			<then>
				<property name="update.log.filename" value="${BASE_DIR}/log/update_MTC_${DSTAMP}${TSTAMP}.log"/>
			</then>
			<else>
				<property name="update.log.filename" value="${BASE_DIR}/run/log/update_MIServer_${DSTAMP}${TSTAMP}.log"/>
			</else>
		</if>
		
		<record name="${update.log.filename}"
		    loglevel="info" append="true"/>
		
		<if>
			<equals arg1="${INST_SYS}" arg2="fit4tc" />
			<then>
				<echo message="Update Machine Tool Client Service"/>
				<echo message=" for Shop Floor Connect for Teamcenter"/>
			</then>
			<else>
				<echo message="Update Fit-MI_Server"/>
			</else>
		</if>

		<if>
			<equals arg1="${INST_OS}" arg2="unix" />
			<then>
				<available file="${DST_DIR}/Version.txt" property="ov" />
				<if>
					<equals arg1="${ov}" arg2="true" />
					<then>
						<move file="${DST_DIR}/Version.txt" tofile="${DST_DIR}/version.txt" />
					</then>
				</if>
			</then>
		</if>

		<loadfile property="OLDVERSION" srcFile="${DST_DIR}/version.txt">
			<filterchain>
				<striplinebreaks/>
			</filterchain>
		</loadfile>

		<echo message="================================"/>
		<echo message="Update of directory: ${DST_DIR}"/>
		<echo message="Update to Version: ${VERSION}" />
		<echo message="Installed Version is: ${OLDVERSION}" />
	
		<if unless:true="${silent}">
			<matches string="${VERSION}" pattern="^.*\(testBuild\)$"/>
			<then>
				<echo message="Install a test build, skip version checks..." />
				<property name="confirmOverwrite" value="false"/>
			</then>
			<else>
				<if>
					<matches string="${OLDVERSION}" pattern="^.*\(testBuild\)$"/>
					<then>
						<echo message="Update a test build, skip version checks..." />
						<property name="confirmOverwrite" value="false"/>
					</then>
					<else>
						<echo message="Check for downgrade..." />
						<!--Check old and new build number... -->
						<RequireConfirmOverwrite oldVersion="${OLDVERSION}" newVersion="${VERSION}" property="confirmOverwrite"/>
					</else>
				</if>
			</else>
		</if>
		
		<if>
			<equals arg1="${confirmOverwrite}" arg2="true" />
			<then>
				<input message="Overwrite installed version '${OLDVERSION}' with version '${VERSION}'?" validargs="y,n" addproperty="ask.overwrite"/>
				<if>
					<equals arg1="${ask.overwrite}" arg2="n" />
					<then>
						<echo message="Update aborted by user, existing version '${OLDVERSION}' should not be overwritten with '${VERSION}'!" />
						<fail>Update aborted by user, existing version should not be overwritten!</fail>
					</then>
				</if>
			</then>
		</if>
		
		<serviceStop />
		
		<if>
			<isset property="isStandAlone" />
			<then>
				<echo message="Skip Java version check for standalone update, Java will be updated!" />
				<antcall target="doupdate"/>
			</then>
			<else>
				<antcall target="check_java" />
			</else>
		</if>
	</target>

	<target name="check_java" >
		<echo message="Check installed java version..." />
		<echo message="Needed version is: 11" />
		<echo message="Installed version is: ${java.specification.version}" />
		<if>
			<or>
				<equals arg1="${java.specification.version}" arg2="11"/>
				<equals arg1="${java.specification.version}" arg2="12"/>
				<equals arg1="${java.specification.version}" arg2="17"/>
			</or>
			<then>
				<echo message="Java version is ok!" />
				<antcall target="doupdate"/>
			</then>
			<else>
				<echo message="Update of Java needed. Please first update java!" />
			</else>
		</if>
	</target>
	
	<target name="doupdate">
		<antcall target="backup"/>
		<antcall target="extract"/>
		<antcall target="update_java" />
		<antcall target="update_tomcat"/>
		<antcall target="update_wut"/>
		<antcall target="check"/>
		<antcall target="cleanup"/>
	</target>
	
	<target name="backup">
		<tstamp/>

		<echo message="Remove old update fragments..." />
		<delete failonerror="false" dir="${TEMP_DIR}" />

		<echo message="Create Backup archive" />
		<if>
			<isset property="isStandAlone" />
			<then>
				<mkdir dir="${BASE_DIR}\backup" />
				<mkdir dir="${BASE_DIR}\dummy" />
				<zip destfile="${BASE_DIR}\backup\SFC_${OLDVERSION}_backup_${DSTAMP}${TSTAMP}.jar" level="9">
					<zipfileset dir="${BASE_DIR}" excludes="logsave* dummy/** log/** backup/** "/>
					<zipfileset dir="${BASE_DIR}/dummy" fullpath="log"/>
				</zip>
				<delete dir="${BASE_DIR}/dummy" />
			</then>
			<else>
				<mkdir dir="${DST_DIR}/../backup" />
				<if>
					<equals arg1="${INST_SYS}" arg2="fit4tc" />
					<then>
						<jar destfile="${DST_DIR}/../backup/SFC_MTCService_${OLDVERSION}_backup_${DSTAMP}${TSTAMP}.jar">
							<fileset dir="${DST_DIR}/../FIT-MI_Server"/>
						</jar>
					</then>
					<else>
						<jar destfile="${DST_DIR}/../backup/FIT_MI-Server_${OLDVERSION}_backup_${DSTAMP}${TSTAMP}.jar">
							<fileset dir="${DST_DIR}/../FIT-MI_Server"/>
						</jar>
					</else>
				</if>
			</else>
		</if>
	</target>
	
	<target name="extract">
		<mkdir dir="${TEMP_DIR}" />
		<echo message="Extract mi_server.jar into ${TEMP_DIR}"/>
		<unjar src="${INST_DIR}/mi_server.jar" dest="${TEMP_DIR}">
		</unjar>
	</target>

	<target name="cleanup">
		<echo message="Cleanup directory ${TEMP_DIR}"/>
		<delete dir="${TEMP_DIR}" />
		
		<!-- Delete obsolete files and dirs -->
		<delete dir="${DST_DIR}/webapps/wut/WEB-INF/src" quiet="true"/>
		<delete file="${DST_DIR}/02_03_Update_FIT-MIServer_eng.pdf" quiet="true"/>
		<delete file="${DST_DIR}/02_03_Update_FIT-MIServer_ger.pdf" quiet="true"/>
		<delete file="${DST_DIR}/ACHTUNG_UPDATE.txt" quiet="true"/>
		<delete file="${DST_DIR}/install.bat" quiet="true"/>
		<delete file="${DST_DIR}/update_files.bat" quiet="true"/>
		<delete file="${DST_DIR}/readme.txt" quiet="true"/>
		<delete file="${DST_DIR}/ChangeLog.txt" quiet="true"/>
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/lib/WuT_Filter.jar" quiet="true"/>
		<delete file="${DST_DIR}/Tomcat 5.0/lib/log4j.properties.fit" quiet="true"/>
		<delete file="${DST_DIR}/Tomcat 5.0/lib/log4j.properties.fit4tc" quiet="true"/>
		<delete file="${DST_DIR}/Tomcat 5.0/lib/log4j.properties" quiet="true"/>
		<delete file="${DST_DIR}/Tomcat 5.0/bin/FIT4TC_MI_Tomcat_service.bat" quiet="true"/>
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/log4j.properties" quiet="true"/>
		
		<!-- Remove not required UNIX or WIN files -->
		<if>
			<equals arg1="${INST_OS}" arg2="unix" />
			<then>
				<!-- chmod ausführen -->
				<chmod dir="${DST_DIR}/Tomcat 5.0/bin" perm="u+x" includes="**/*.sh"/>
				<!-- Windows batch löschen -->
				<delete>
				    <fileset dir="${DST_DIR}/Tomcat 5.0/bin" includes="**/*.bat"/>
					<fileset dir="${DST_DIR}/Tomcat 5.0/bin" includes="**/*.dll"/>
					<fileset dir="${DST_DIR}/Tomcat 5.0/bin" includes="**/*.exe"/>
				</delete>
			</then>
			<else>
				<!-- Unix scripte löschen -->
				<delete>
				    <fileset dir="${DST_DIR}/Tomcat 5.0/bin" includes="**/*.sh"/>
				</delete>
				<delete dir="${DST_DIR}/bin/init.d_scripts" />
			</else>
		</if>

		<!-- Remove not required scripts -->
		<if>
			<equals arg1="${INST_SYS}" arg2="fit4tc" />
			<then>
				<delete>
					<filelist dir="${DST_DIR}/Tomcat 5.0/bin">
						<file name="FIT_MI_Tomcat_service.bat"/>
						<file name="FIT_MI-Configure.bat"/>
						<file name="FIT_MI-Monitor.bat"/>
						<file name="install_fit.bat"/>
						<file name="remove_fit.bat"/>
						<file name="setenv_fit.bat"/>
						<file name="setenv_fit.sh"/>
					</filelist>
				</delete>
				<delete failonerror="false" file="${DST_DIR}/webapps/wut/WEB-INF/conf/machine1.properties" />
				<delete failonerror="false" file="${DST_DIR}/webapps/wut/WEB-INF/conf/opcua1.properties" />
				<delete failonerror="false" file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/sinrpc_db_config.ini.default" />
				<delete failonerror="false" file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/genius-upload.templ.default" />
				<delete failonerror="false" file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/logback.xml.fit" />
				<move failonerror="false" file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/logback.xml.fit4tc" tofile="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/logback.xml.default" />
			</then>
			<else>
				<delete>
					<filelist dir="${DST_DIR}/Tomcat 5.0/bin">
						<file name="FIT4TC_MI_Tomcat_service.bat"/>
						<file name="install_fit4tc.bat"/>
						<file name="remove_fit4tc.bat"/>
						<file name="setenv_fit4tc.bat"/>
						<file name="setenv_fit4tc.sh"/>
					</filelist>
				</delete>
				<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/logback.xml.fit4tc" />
				<move failonerror="false" file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/logback.xml.fit" tofile="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/logback.xml.default" />
			</else>
		</if>
		
	</target>
	
	<!-- copy überschreibt nur die Dateien, die vom Setup-Medium mit einem neueren Datum kommen! -->
	
	<target name="update_tomcat">
		<echo message="Update Tomcat..."/>
		
		<echo message="Remove obsolete files..."/>
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/Uninstall.exe" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/commons-launcher.jar" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/commons-logging-api.jar" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/jmx.jar" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/launcher.properties" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/LauncherBootstrap.class" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/shutdown-using-launcher.sh" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/shutdown-using-launcher.bat" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/startup-using-launcher.sh" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/startup-using-launcher.bat" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/tool-wrapper-using-launcher.sh" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/tool-wrapper-using-launcher.bat" />

		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/conf/Catalina/localhost/admin.xml" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/conf/Catalina/localhost/manager.xml" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/conf/Catalina/localhost/wut.xml.fit" />

		<delete failonerror="false" dir="${DST_DIR}/Tomcat 5.0/server" />
		<delete failonerror="false" dir="${DST_DIR}/Tomcat 5.0/shared" />

		<delete failonerror="false" dir="${DST_DIR}/Tomcat 5.0/webapps/ROOT/WEB-INF/classes" />
		<delete failonerror="false" dir="${DST_DIR}/Tomcat 5.0/webapps/ROOT/WEB-INF/lib" />
		<delete failonerror="false" dir="${DST_DIR}/Tomcat 5.0/webapps/tomcat-docs" />

		<delete failonerror="false" dir="${DST_DIR}/Tomcat 5.0/work/Catalina/localhost/tomcat-docs" />
		<delete failonerror="false" dir="${DST_DIR}/Tomcat 5.0/work/Catalina/localhost/manager" />
		<delete failonerror="false" dir="${DST_DIR}/Tomcat 5.0/work/Catalina/localhost/balancer" />
		<delete failonerror="false" dir="${DST_DIR}/Tomcat 5.0/work/Catalina/localhost/admin" />

		<delete failonerror="false" dir="${DST_DIR}/Tomcat 5.0/lib" />

		<echo message="Copy files for Apache Tomcat"/>

		<!-- Bug 2909: bootstrap.jar wird nicht immer überschrieben -->
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/bootstrap.jar" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/tomcat-juli.jar" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/tomcat-native.tar.gz" />

		<copy todir="${DST_DIR}/Tomcat 5.0/bin" verbose="true" >
			<fileset dir="${TEMP_DIR}/Tomcat 5.0/bin">
			</fileset>
		</copy>

		<if>
			<equals arg1="${INST_OS}" arg2="msw" />
			<then>
				<copy overwrite="false" file="${TEMP_DIR}/Tomcat 5.0/bin/tomcat8_32.exe" tofile="${DST_DIR}/Tomcat 5.0/bin/${SERVICENAME}.exe" />
				<copy overwrite="false" file="${TEMP_DIR}/Tomcat 5.0/bin/tomcat8_64.exe" tofile="${DST_DIR}/Tomcat 5.0/bin/${SERVICENAME}_64.exe" />
				<copy overwrite="false" file="${TEMP_DIR}/Tomcat 5.0/bin/tomcat8w_32.exe" tofile="${DST_DIR}/Tomcat 5.0/bin/${SERVICENAME}w.exe" />
				<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/tomcat8_32.exe" />
				<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/tomcat8_64.exe" />
				<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/tomcat8w_32.exe" />
			</then>
		</if>
			
		<!-- Copy configuration files -->
		<copy failonerror="false" todir="${DST_DIR}/Tomcat 5.0/lib" verbose="true" >
			<fileset dir="${DST_DIR}/Tomcat 5.0/common/classes">
			</fileset>
		</copy>
		
		<delete dir="${DST_DIR}/Tomcat 5.0/common" />

		<copy todir="${DST_DIR}/Tomcat 5.0/lib" verbose="true" overwrite="true">
			<fileset dir="${TEMP_DIR}/Tomcat 5.0/lib">
			</fileset>
		</copy>
		
		<copy overwrite="true" file="${TEMP_DIR}/Tomcat 5.0/conf/catalina.policy" tofile="${DST_DIR}/Tomcat 5.0/conf/catalina.policy" />
		<copy overwrite="true" file="${TEMP_DIR}/Tomcat 5.0/conf/catalina.properties" tofile="${DST_DIR}/Tomcat 5.0/conf/catalina.properties" />
		<copy overwrite="true" file="${TEMP_DIR}/Tomcat 5.0/conf/context.xml" tofile="${DST_DIR}/Tomcat 5.0/conf/context.xml" />
		<copy overwrite="true" file="${TEMP_DIR}/Tomcat 5.0/conf/logging.properties" tofile="${DST_DIR}/Tomcat 5.0/conf/logging.properties" />
		
		<copy overwrite="true" file="${TEMP_DIR}/Tomcat 5.0/LICENSE" tofile="${DST_DIR}/Tomcat 5.0/LICENSE" />
		<copy overwrite="true" file="${TEMP_DIR}/Tomcat 5.0/NOTICE" tofile="${DST_DIR}/Tomcat 5.0/NOTICE" />
		<copy overwrite="true" file="${TEMP_DIR}/Tomcat 5.0/RELEASE-NOTES" tofile="${DST_DIR}/Tomcat 5.0/RELEASE-NOTES" />
		<copy overwrite="true" file="${TEMP_DIR}/Tomcat 5.0/RUNNING.txt" tofile="${DST_DIR}/Tomcat 5.0/RUNNING.txt" />
		<copy overwrite="true" file="${TEMP_DIR}/tomcat-version.txt" tofile="${DST_DIR}/tomcat-version.txt" />
		
		<mkdir dir="${DST_DIR}/Tomcat 5.0/temp" />
		
		<!-- Bug 2402: docs und ROOT-Webapp entfernen -->
		<delete failonerror="false" dir="${DST_DIR}/Tomcat 5.0/webapps/tomcat-docs" />
		<delete failonerror="false" dir="${DST_DIR}/Tomcat 5.0/webapps/ROOT" />
		<copy todir="${DST_DIR}/Tomcat 5.0/webapps" verbose="false" >
			<fileset dir="${TEMP_DIR}/Tomcat 5.0/webapps">
			</fileset>
		</copy>

		<!-- Unnoetige Dateien entfernen... -->
		<delete failonerror="false" dir="${DST_DIR}/Tomcat 5.0/bin/contrib" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/catalina.xml" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/launcher.properties" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/LauncherBootstrap.class" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/shutdown-using-launcher.bat" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/shutdown-using-launcher.sh" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/startup-using-launcher.bat" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/startup-using-launcher.sh" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/tool-wrapper-using-launcher.bat" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/tool-wrapper-using-launcher.sh" />
		<delete failonerror="false" file="${DST_DIR}/Tomcat 5.0/bin/tcnative-1.dll" />

		<!-- Server.xml anpassen wenn nötig -->
		<echo message="Check server.xml ..."/>
		
		<delete file="${DST_DIR}/Tomcat 5.0/conf/server_org.xml" failonerror="false" />
		<move file="${DST_DIR}/Tomcat 5.0/conf/server.xml" tofile="${DST_DIR}/Tomcat 5.0/conf/server_org.xml" />
		
		<xmltask outputter="simple" source="${DST_DIR}/Tomcat 5.0/conf/server_org.xml" dest="${DST_DIR}/Tomcat 5.0/conf/server.xml">
			<remove path="Server/Listener[@className='org.apache.catalina.core.AprLifecycleListener']" />
			<remove path="Server/Listener[@className='org.apache.catalina.mbeans.ServerLifecycleListener']" />

			<!-- Check for old user database definition -->
			<call path="Server/GlobalNamingResources/ResourceParams[@name='UserDatabase']">
				<param name="update.reqd" value="true"/>
				<actions>
					<echo>Found old user database, converting</echo>
					<property name="update.reqd" value="true"/>
				</actions>
					

			</call>
			
			<remove if="update.reqd" path="Server/GlobalNamingResources/ResourceParams[@name='UserDatabase']" />
			<replace if="update.reqd" path="Server/GlobalNamingResources/Resource[@name='UserDatabase']">
				<![CDATA[
				    <Resource name="UserDatabase" auth="Container"
				              type="org.apache.catalina.UserDatabase"
				              description="User database that can be updated and saved"
				              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
				              pathname="conf/tomcat-users.xml" />
				]]>
			</replace>
				
			<insert if="update.reqd" path="Server/GlobalNamingResources" position="before">
				<![CDATA[
				  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
				  <Listener className="org.apache.catalina.core.JasperListener" />

				]]>
			</insert>

			<!-- Check for GlobalResourcesLifecycleListener -->
			<call path="Server/Listener[@className='org.apache.catalina.mbeans.GlobalResourcesLifecycleListener']">
				<actions>
					<echo>GlobalResourcesLifecycleListener already registered...</echo>
					<property name="GlobalResourcesLifecycleListener.set" value="true"/>
				</actions>
			</call>
			<insert unless="GlobalResourcesLifecycleListener.set" path="Server/GlobalNamingResources" position="before">
				<![CDATA[
				  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
				  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

				]]>
			</insert>

			<!-- Bug 2956: Since 7.0.63 maxPostSize 0 disables POST requests. Change to -1 -->
			<attr path="Server/Service/Connector[@maxPostSize='0']" attr="maxPostSize" value="-1"/>

			<!-- Bug 3163: Update to 8.5.15 and remove obsolete attributes and elements -->
			<attr path="Server" attr="debug" remove="true"/>
			<attr path="Server/Listener" attr="debug" remove="true"/>
			<attr path="Server/Service/Connector" attr="debug" remove="true"/>
			<attr path="Server/Service/Connector" attr="maxSpareThreads" remove="true"/>
			<attr path="Server/Service/Engine" attr="debug" remove="true"/>
			<attr path="Server/Service/Engine/Host" attr="debug" remove="true"/>
			<attr path="Server/Service/Engine/Host" attr="xmlNamespaceAware" remove="true"/>
			<attr path="Server/Service/Engine/Host" attr="xmlValidation" remove="true"/>
			<remove path="Server/Listener[@className='org.apache.catalina.core.JasperListener']"/>
			
			<!-- Bug 3416: Check for HTTPS-Connector -->
			<call path="Server/Service/Connector[@SSLEnabled='true']">
				<param name="https.present" value="true"/>
				<actions>
					<property name="https.present" value="true"/>
				</actions>
			</call>
			<insert unless="https.present" path="Server/Service/Connector" position="after">
				<![CDATA[
			    <Connector SSLEnabled="true" acceptCount="100" disableUploadTimeout="true" enableLookups="true" port="8493" scheme="https" secure="true">
			    	<SSLHostConfig sslProtocol="TLS">
			    		<Certificate certificateKeyAlias="@@CERTKEYALIAS@@" certificateKeystoreFile="@@CERTKEYSTOREFILE@@" certificateKeystorePassword="@@CERTKEYSTOREPWD@@" certificateKeyPassword="@@CERTKEYPWD@@">
			    		</Certificate>
			    	</SSLHostConfig>
			    </Connector>

				]]>
			</insert>
			<attr unless="https.present" path="Server/Service/Connector[@port='8090']" attr="redirectPort" value="8493"/>

			<!-- Bug 5864: Hide tomcat version information from error pages -->
			<call path="Server/Service/Engine/Host/Valve[@className='org.apache.catalina.valves.ErrorReportValve']">
				<actions>
					<echo>ErrorReportValve already configured...</echo>
					<property name="ErrorReportValve.set" value="true"/>
				</actions>
			</call>
			<insert unless="ErrorReportValve.set" path="Server/Service/Engine/Host" position="under">
				<![CDATA[
				<Valve className="org.apache.catalina.valves.ErrorReportValve" showServerInfo="false" />

				]]>
			</insert>
			
		</xmltask>

		<!-- Bug 3416: Check for HTTPS-Connector -->
		<if>
			<isset property="https.present"/>
			<then>
				<echo message="HTTPS Connector already configured! Configuration not changed!"/>
			</then>
			<else>
				<echo message="Insert new HTTPS connector with default configuration!"/>
				<echo message=" -> New HTTPS port is: 8493"/>
				<echo message=" => Please check this configuration to match your CA!"/>				
				<if>
					<equals arg1="${INST_SYS}" arg2="fit4tc" />
					<then>
						<replace file="${DST_DIR}/Tomcat 5.0/conf/server.xml">
							<replacefilter token="@@CERTKEYALIAS@@" value="SFC_Server"/>
							<replacefilter token="@@CERTKEYSTOREFILE@@" value="../../conf/mykeystore.ks"/>
							<replacefilter token="@@CERTKEYSTOREPWD@@" value="aplusbpwd"/>
							<replacefilter token="@@CERTKEYPWD@@" value="aplusbpwd"/>
						</replace>
					</then>
					<else>
						<replace file="${DST_DIR}/Tomcat 5.0/conf/server.xml">
							<replacefilter token="@@CERTKEYALIAS@@" value="tomcat"/>
							<replacefilter token="@@CERTKEYSTOREFILE@@" value="${FITDIR}/run/bin/Tomcat 5.0/conf/ABkeystore.ks"/>
							<replacefilter token="@@CERTKEYSTOREPWD@@" value="AplusBpwd2015"/>
							<replacefilter token="@@CERTKEYPWD@@" value="AplusBpwd2015"/>
						</replace>
					</else>
				</if>
			</else>
		</if>
			
		<!-- Bug 3711: Update service configuration -->
		<if>
			<equals arg1="${INST_OS}" arg2="unix" />
			<then>
				<shellscript shell="bash">
					if grep -q CLASSPATH.*wut/WEB-INF/conf ${DST_DIR}/Tomcat\ 5.0/bin/setenv.sh; then
						echo CLASSPATH is ok
					else
						echo CLASSPATH needs to be modified
						echo "# Add conf directory to classpath - required to find log4j.properties and other configuration files..." >> ${DST_DIR}/Tomcat\ 5.0/bin/setenv.sh
						echo "CLASSPATH=\${CATALINA_HOME}/../webapps/wut/WEB-INF/conf" >> ${DST_DIR}/Tomcat\ 5.0/bin/setenv.sh
					fi
				</shellscript>
			</then>
			<else>
				<echo message="Check windows service configuration..."/>	
				<exec executable="${INST_DIR}\\install\\MTCServiceConfigUpdater.exe"/>
				
				<!-- check setenv.bat (SFC) -->
				<if>
					<and>
						<equals arg1="${INST_SYS}" arg2="fit4tc" />
						<resourcecontains resource="${DST_DIR}/Tomcat 5.0/bin/setenv.bat" substring="set JAVA_HOME=%TCFITDIR%\Java\j2sdk\jre" />
					</and>
					<then>
						<echo message="setenv.bat contains correct JAVA_HOME setting." />
					</then>
					<else>
						<echo message="setenv.bat may not contain correct JAVA_HOME value, check this..." />
						<if>
							<resourcecontains resource="${DST_DIR}/Tomcat 5.0/bin/setenv.bat" substring="set JAVA_HOME=%TCFITDIR%\Java\j2sdk" />
							<then>
								<echo message=" Change JAVA_HOME from %TCFITDIR%\Java\j2sdk to %TCFITDIR%\Java\j2sdk\jre" />
								<replace file="${DST_DIR}/Tomcat 5.0/bin/setenv.bat" token="set JAVA_HOME=%TCFITDIR%\Java\j2sdk" value="set JAVA_HOME=%TCFITDIR%\Java\j2sdk\jre"/>
							</then>
						</if>
					</else>
				</if>
			</else>
		</if>

	</target>
	
	<target name="update_wut">
		
		<!-- Remove old properties files -->
		<delete>
			<fileset dir="${DST_DIR}/webapps/wut/WEB-INF/classes" includes="**/*.properties"/>
		</delete>
		
		<delete dir="${DST_DIR}/webapps/wut/WEB-INF/lib" failonerror="false">
			<exclude name="**/amd64/AGLink*"/>
			<exclude name="**/x86/AGLink*"/>
			<exclude name="**/MI_AGLink-*.jar"/>
			<exclude name="**/aglink-java-wrapper.jar"/>
		</delete>
		
		<!-- delete dir="${DST_DIR}/webapps/wut/WEB-INF/lib" failonerror="false" / -->
		
		<echo message="Copy files for WuT Webapp"/>
		<copy todir="${DST_DIR}/webapps\wut" verbose="true" overwrite="true">
			<fileset dir="${TEMP_DIR}/webapps\wut">
			</fileset>
		</copy>
		
		<echo message="Copy bin files"/>
		<copy todir="${DST_DIR}/bin" verbose="true" overwrite="true">
			<fileset dir="${TEMP_DIR}/bin">
			</fileset>
		</copy>
		
		<delete file="${DST_DIR}/bin/MCISServiceCall.exe" failonerror="false" />

		<!-- Alten internen FTP-Client löschen, wenn noch vorhanden -->
		<delete dir="${DST_DIR}/webapps/wut/WEB-INF/classes/com/aplusb/ftp" failonerror="false" />
		
		<!-- Alte unvollständige COM-Server Implementierungen löschen -->
		<delete dir="${DST_DIR}/webapps/wut/WEB-INF/classes/com/aplusb/mi/comservers/bbelec" failonerror="false" />
		<delete dir="${DST_DIR}/webapps/wut/WEB-INF/classes/com/aplusb/mi/comservers/moxa" failonerror="false" />
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/classes/com/aplusb/webadapter/com/Moxa.class" failonerror="false" />
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/classes/com/aplusb/webadapter/com/VlinxBBElec.class" failonerror="false" />

		<!-- Alte Dateien löschen -->
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/lib/fsw_pkg.jar" failonerror="false" />
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/lib/win32com.dll" failonerror="false" />
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/lib/ReadmeJavaCommAPI.html" failonerror="false" />
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/lib/javax.comm.properties" failonerror="false" />
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/lib/comm.jar" failonerror="false" />
		<delete dir="${DST_DIR}/webapps/wut/WEB-INF/classes/com/aplusb/mi/JUnitTest" failonerror="false" />
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/users.lst" failonerror="false" />
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/classes/com/aplusb/webadapter/servlet/WutInitServlet.class" failonerror="false" />

		<copy overwrite="true" file="${TEMP_DIR}/version.txt" tofile="${DST_DIR}/version.txt" />
		<copy overwrite="true" file="${TEMP_DIR}/ChangeLog_de.txt" tofile="${DST_DIR}/ChangeLog_de.txt" />
		<copy overwrite="true" file="${TEMP_DIR}/ChangeLog_en.txt" tofile="${DST_DIR}/ChangeLog_en.txt" />
		
		<!-- logback.xml vorhanden? -->
		<available file="${DST_DIR}/webapps/wut/WEB-INF/conf/logback.xml" property="av_log4j" />
		<if>
			<not><equals arg1="${av_log4j}" arg2="true" /></not>
			<then>
				<echo message="logback.xml doesn't exist. Create based on default file..." />
				<copy file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/logback.xml.${INST_SYS}" tofile="${DST_DIR}/webapps/wut/WEB-INF/conf/logback.xml" />
			</then>
			<else>
				<echo message="logback.xml already exist!" />
			</else>
		</if>

		<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/mi_server.ini.SFCfTC" failonerror="false"/>
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/mi_server.ini.SFC" failonerror="false"/>
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/mi_server.ini.FIT" failonerror="false"/>
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/mi_server.ini.SFCfTC" failonerror="false"/>
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/mi_server.ini.FIT" failonerror="false"/>
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/mi_server.ini.default" failonerror="false"/>
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/log4j.properties.fit" failonerror="false"/>
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/log4j.properties.fit4tc" failonerror="false"/>
		
	</target>
	
	<target name="update_java">
		
		<if>
			<and>
				<isset property="isStandAlone" />
				<equals arg1="${isMsWin}" arg2="true" />
			</and>
			<then>
				<echo message="Save cacerts..."/>
				<copy failonerror="false" file="${BASE_DIR}/Java/j2sdk/jre/lib/security/cacerts" tofile="${BASE_DIR}/cacerts.user"/>
				
				<echo message="Update internal java version..."/>
				<delete dir="${BASE_DIR}/Java/j2sdk" />
				
				<echo message="Unkompress ${BASE_JAR_DIR}${file.separator}SFC_Base.jar.xz"/>
				<unxz src="${BASE_JAR_DIR}${file.separator}SFC_Base.jar.xz" dest="${TEMP_DIR}${file.separator}SFC_Base.jar"/>
				<unjar src="${TEMP_DIR}${file.separator}SFC_Base.jar" dest="${BASE_DIR}">
					<patternset>
						<include name="Java/"/>
					</patternset>
				</unjar>
				
				<delete failonerror="false" file="${TEMP_DIR}${file.separator}SFC_Base.jar" />
				<delete failonerror="false" file="${BASE_DIR}${file.separator}Java${file.separator}JavaService.exe" />

				<echo message="Replace cacerts with old file..."/>
				<copy failonerror="false" file="${BASE_DIR}/Java/j2sdk/jre/lib/security/cacerts" tofile="${BASE_DIR}/Java/j2sdk/jre/lib/security/cacerts.org" />
				<move failonerror="false" file="${BASE_DIR}/cacerts.user" tofile="${BASE_DIR}/Java/j2sdk/jre/lib/security/cacerts" />
			</then>
		</if>
	</target>

	<target name="check">
		<echo message="Check old configuration style"/>
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/webadapter.ini.default" quiet="true" />
		
		<available file="${DST_DIR}/webapps/wut/WEB-INF/conf/webadapter.ini" property="wini" />
		<if>
			<equals arg1="${wini}" arg2="true" />
			<then>
				<echo message="Rename webadapter.ini to mi_server.ini" />
				<move file="${DST_DIR}/webapps/wut/WEB-INF/conf/webadapter.ini" tofile="${DST_DIR}/webapps/wut/WEB-INF/conf/mi_server.ini" />
			</then>
			<else>
				<echo message="webadapter.ini already renamed to mi_server.ini" />
			</else>
		</if>
		<if>
			<equals arg1="${INST_OS}" arg2="unix" />
			<then>
				<!-- chmod ausführen -->
				<chmod dir="${DST_DIR}/Tomcat 5.0/bin" perm="u+x" includes="**/*.sh"/>
				<chmod dir="${DST_DIR}/bin/init.d_scripts/Linux" perm="u+x" includes="**/*.sh"/>
			</then>
		</if>
		
	</target>
	


</project>
