<?xml version="1.0"?>

<project default="doinstall" basedir="." xmlns:if="ant:if" xmlns:unless="ant:unless">
	
	<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
	<taskdef resource="com/sardak/antform/taskdefs.properties"/>

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

	<property environment="env"/>
	<hostinfo/>
	
	<!-- Set Default values if not received via args... -->
	<property name="sfcMtcRegisterAsService" value="true"/>
	<property name="sfcMtcServiceName" value="SFCMTCService"/>
	<property name="sfcMtcServiceStartupType" value="delayed"/>
	<property name="sfcMtcPortHttp" value="8090"/>
	<property name="sfcMtcPortHttps" value="8493"/>
	<property name="sfcMtcPortShutdown" value="8006"/>
	<property name="sfcHost" value="localhost"/>
	<property name="sfcPort" value="8888"/>
	<property name="sfcUseSecureCommunication" value="false"/>
	
	
	<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="doinstallStandAlone">
		
		<property name="sfcMtcHostName" value="${NAME}" />
		
		<echo message="===================================================================="/>
		<echo message="Install Shop Floor Connect Machine Tool Client Service ${VERSION}"/>
		<echo message="===================================================================="/>
        <echo message="OS Name is:          ${os.name}" />
        <echo message="OS Version is:       ${os.version}" />
        <echo message="OS User Name is:     ${user.name}" />
		<echo message="===================================================================="/>

		<!-- Find server dir which contains SFC_base.jar.xz -->
		<local name="BASE_JAR_DIR"/>
		<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>

		<if>
			<isset property="sfcMtcInstallationPath"></isset>
			<then>
				<echo message="Received installation path via command line: ${sfcMtcInstallationPath}"/>
			</then>
			<else>
				<fail message="Silent installation mode enabled but 'sfcMtcInstallationPath' not specified!" if:true="${silent}" />
				
				<antform title="Install ShopFloorConnect MachineToolClient Service" image="install/siemens_ab_logo_trans_klein.png">
					<label>Please select the directory into which you want to install\n SFC MTC Service.</label>
					<label>A subdirectory named "ShopFloorConnect" will be created there.</label>
					<fileSelectionProperty label="Installation directory: " 
						property="sfcMtcInstallationPath" 
						directoryChooser="true" Required="true"/>
					<separator/>
					<label>Please set TCP port numbers for service to listen on:</label>
					<textProperty label="HTTP Port" property="sfcMtcPortHttp" />
					<textProperty label="HTTPs Port" property="sfcMtcPortHttps" />
					<textProperty label="Shutdown Port" property="sfcMtcPortShutdown" />
					<separator if:true="${isMsWin}" />
					<booleanProperty label="Register as windows service" property="sfcMtcRegisterAsService" if:true="${isMsWin}"/>
					<textProperty label="Service Name" property="sfcMtcServiceName" if:true="${isMsWin}"/>
					<selectionProperty label="Service startup type" property="sfcMtcServiceStartupType" values="delayed,auto,manual" if:true="${isMsWin}"/>
					<textProperty label="Service user name" property="sfcMtcServiceUserName" if:true="${isMsWin}" />
					<textProperty label="Service user password" property="sfcMtcServiceUserPassword" password="true" if:true="${isMsWin}" />
					<separator if:true="${isUnix}" />
					<booleanProperty label="Register as systemd service" property="sfcMtcRegisterAsService" if:true="${isUnix}"/>
					<textProperty label="Service Name" property="sfcMtcServiceName" if:true="${isUnix}"/>
					<selectionProperty label="Service startup type" property="sfcMtcServiceStartupType" values="auto,manual" if:true="${isUnix}"/>
					<textProperty label="sudo password" property="sfcMtcServiceUserPassword" password="true" if:true="${isUnix}" />
					<separator />
					<label>Please set ShopFloorConnect Server data:</label>
					<textProperty label="SFC Host" property="sfcHost" />
					<textProperty label="SFC Port" property="sfcPort" />
					<textProperty label="Local Hostname" property="sfcMtcHostName" />
					<booleanProperty label="Secure communication" property="sfcUseSecureCommunication" />
					<separator />
					<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>

		<echo message="Installaton path:    ${BASE_DIR}"/>
		<echo message="--------------------------------------------------------------------"/>
		<echo message="HTTP port:           ${sfcMtcPortHttp}"/>
		<echo message="HTTPs port:          ${sfcMtcPortHttps}"/>
		<echo message="Shutdown port:       ${sfcMtcPortShutdown}"/>
		<echo message="--------------------------------------------------------------------"/>
		<echo message="Register service:    ${sfcMtcRegisterAsService}" />
		<echo message="Service name:        ${sfcMtcServiceName}"/>
		<echo message="Service Startup:     ${sfcMtcServiceStartupType}"/>
		<echo message="Service Username:    ${sfcMtcServiceUserName}" if:true="${isMsWin}"/>
		<echo message="--------------------------------------------------------------------"/>
		<echo message="SFC Server host:     ${sfcHost}"/>
		<echo message="SFC Server port:     ${sfcPort}"/>
		<echo message="SFC secure comm.:    ${sfcUseSecureCommunication}"/>
		<echo message="Local Hostname:      ${sfcMtcHostName}"/>
		<echo message="===================================================================="/>
		
		<fail message="Installation path '${BASE_DIR}${file.separator}FIT-MI_Server' already exists!">
			<condition>
				<available file="${BASE_DIR}${file.separator}FIT-MI_Server" type="dir" />
			</condition>
		</fail>
			
		<if>
			<available file="${BASE_DIR}" type="dir" />
			<then>
				<echo message="Installation path '${BASE_DIR}' already exists! Install SFC_MTCService into this directory." />
				<property name="isStandAlone" value="false" />
			</then>
			<else>
				<echo message="Do standalone installation of SFC_MTCService..." />
				<property name="isStandAlone" value="true" />
			</else>
		</if>
		
		<input message="Press enter to continue" unless:true="${silent}"/>
		<echo message="Create directory ${BASE_DIR}" if:true="${isStandAlone}" />
		<mkdir dir="${BASE_DIR}" if:true="${isStandAlone}"/>

		<property name="TEMP_DIR" value="${BASE_DIR}${file.separator}install_temp" />
		<mkdir dir="${TEMP_DIR}" />

		<property name="DST_DIR" value="${BASE_DIR}${file.separator}FIT-MI_Server" />
		<echo message="Install to directory: ${DST_DIR}"/>

		
		<echo message="Extract SFC_Base.jar into ${TEMP_DIR}" if:true="${isStandAlone}"/>
		<unxz src="${BASE_JAR_DIR}${file.separator}SFC_Base.jar.xz" dest="${TEMP_DIR}${file.separator}SFC_Base.jar" if:true="${isStandAlone}"/>
		<unjar src="${TEMP_DIR}${file.separator}SFC_Base.jar" dest="${BASE_DIR}" if:true="${isStandAlone}">
			<patternset>
				<exclude name="Java/" if:true="${isUnix}"/>
			</patternset>
		</unjar>
		
		<delete failonerror="false" file="${TEMP_DIR}${file.separator}SFC_Base.jar" if:true="${isStandAlone}"/>
		<delete failonerror="false" file="${BASE_DIR}${file.separator}Java${file.separator}JavaService.exe" if:true="${isStandAlone}"/>
		
		<mkdir dir="${BASE_DIR}${file.separator}conf" if:true="${isStandAlone}"/>
		
		<property name="j.home" value="${BASE_DIR}${file.separator}java${file.separator}j2sdk"></property>
		<antcall target="check"/>
		
	</target>

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

	<target name="doinstall">
		<property name="TEMP_DIR" value="${BASE_DIR}${file.separator}install_temp" />
		<property name="isStandAlone" value="false" />
		<property name="sfcMtcHostName" value="localhost" />
		
		<if>
			<equals arg1="${INST_SYS}" arg2="fit4tc" />
			<then>
				<echo message="Install Machine Tool Client Service Version ${VERSION}"/>
				<echo message=" for Shop Floor Connect for Teamcenter"/>
			</then>
			<else>
				<echo message="Install Fit-MI_Server Version ${VERSION}"/>
			</else>
		</if>

		<echo message="===================================================="/>
		<echo message="Install to directory: ${DST_DIR}"/>

		<antcall target="check"/>
		
	</target>

	<target name="check">
		<available file="${DST_DIR}" type="dir" property="MI_EXIST" />
		<if>
			<not><equals arg1="${MI_EXIST}" arg2="true" /></not>
			<then>
				<echo message="Start installation..." />

				<antcall target="extract"/>
				<antcall target="copy"/>
				<antcall target="prep_configFiles"/>
				<antcall target="cleanup"/>

				<echo message="Saving configuration data..." />
				<mkdir dir="${BASE_DIR}${file.separator}conf${file.separator}install" />
				<propertyfile file="${BASE_DIR}${file.separator}conf${file.separator}install${file.separator}configuration.properties">
					<entry  key="sfcMtcPortHttp" value="${sfcMtcPortHttp}" if:set="sfcMtcPortHttp"/>
					<entry  key="sfcMtcPortHttps" value="${sfcMtcPortHttps}" if:set="sfcMtcPortHttps"/>
					<entry  key="sfcMtcPortShutdown" value="${sfcMtcPortShutdown}" if:set="sfcMtcPortShutdown"/>
					<entry  key="sfcMtcRegisterAsService" value="${sfcMtcRegisterAsService}" if:set="sfcMtcRegisterAsService"/>
					<entry  key="sfcMtcServiceName" value="${sfcMtcServiceName}" if:set="sfcMtcServiceName"/>
					<entry  key="sfcMtcServiceStartupType" value="${sfcMtcServiceStartupType}" if:set="sfcMtcServiceStartupType"/>
					<entry  key="sfcMtcServiceUserName" value="${sfcMtcServiceUserName}" if:set="sfcMtcServiceUserName"/>
				</propertyfile>

			</then>
			<else>
				<fail message="Service already exist! Please execute update!" />
			</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}" />
		
		<!-- 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 failonerror="false">
				    <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"/>
				    <fileset dir="${BASE_DIR}/bin" includes="**/*.bat"/>
				    <fileset dir="${BASE_DIR}" includes="**/*.bat"/>
				</delete>
			</then>
			<else>
				<!-- Unix scripte löschen -->
				<delete failonerror="false">
				    <fileset dir="${DST_DIR}/Tomcat 5.0/bin" includes="**/*.sh"/>
					<fileset dir="${BASE_DIR}/bin" includes="**/*.sh"/>
					<fileset dir="${BASE_DIR}" 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>
			</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>
			</else>
		</if>

	</target>

	<target name="copy">
		<echo message="Copy files for Apache Tomcat"/>
		<copy todir="${DST_DIR}/Tomcat 5.0" verbose="true" >
			<fileset dir="${TEMP_DIR}/Tomcat 5.0">
				<exclude name="**/*.java"/>
			</fileset>
		</copy>
		<echo message="Copy files for MI Server"/>
		<copy todir="${DST_DIR}/webapps" verbose="true" >
			<fileset dir="${TEMP_DIR}/webapps">
			</fileset>
		</copy>
		<echo message="Copy bin files"/>
		<copy todir="${DST_DIR}/bin" verbose="true">
			<fileset dir="${TEMP_DIR}/bin">
			</fileset>
		</copy>
		<copy overwrite="true" file="${TEMP_DIR}/version.txt" tofile="${DST_DIR}/version.txt" />
		<copy overwrite="true" file="${TEMP_DIR}/tomcat-version.txt" tofile="${DST_DIR}/tomcat-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" />
		<mkdir dir="${DST_DIR}/Tomcat 5.0/temp" />

	</target>
	
	<target name="prep_configFiles" >
		<echo message="Prepare configuration files..." />
		
		<!-- 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..." />
				<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! Please compare with the new one!" />
			</else>
		</if>
		
		<if>
			<equals arg1="${INST_OS}" arg2="unix" /> <!-- <equals arg1="${os.name}" arg2="Linux"/> -->
			<then>
				<!-- setenv.sh -->
				<echo message="Create setenv.sh" />
				<copy file="${DST_DIR}/Tomcat 5.0/bin/setenv_${INST_SYS}.sh" tofile="${DST_DIR}/Tomcat 5.0/bin/setenv.sh" />
				<replace file="${DST_DIR}/Tomcat 5.0/bin/setenv.sh" token="${JAVA_HOME}" value="${j.home}"/>
				<replace file="${DST_DIR}/Tomcat 5.0/bin/setenv.sh" token="@@TCFITDIR@@" value="${BASE_DIR}"/>
				
				<!-- 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"/>

				<copy verbose="true" file="${DST_DIR}/bin/init.d_scripts/Linux/SFC_MTCServer.sh" tofile="${BASE_DIR}/etc/init.d/SFC_MTCServer.sh"/>
				<copy verbose="true" file="${DST_DIR}/bin/init.d_scripts/Linux/systemd/SFC_MTCServer.service" tofile="${BASE_DIR}/etc/systemd/SFC_MTCServer.service"/>

				<replace file="${BASE_DIR}/etc/systemd/SFC_MTCServer.service" token="@@TCFITDIR@@" value="${BASE_DIR}"/>
				<replace file="${BASE_DIR}/etc/systemd/SFC_MTCServer.service" token="@@OSUSERNAME@@" value="${user.name}"/>
				<replace file="${BASE_DIR}/etc/init.d/SFC_MTCServer.sh" token="@@TCFITDIR@@" value="${BASE_DIR}"/>
				<replace file="${BASE_DIR}/etc/init.d/SFC_MTCServer.sh" token="@@OSUSERNAME@@" value="${user.name}"/>
				
				<chmod dir="${BASE_DIR}/etc/init.d" perm="u+x" includes="**/*.sh"/>
				<chmod dir="${BASE_DIR}/etc/systemd" perm="u+x" includes="**/*.sh **/*.service"/>
				
				<echo message="Register systemd service..." if:true="${sfcMtcRegisterAsService}"/>
				<if>
					<equals arg1="${INST_SYS}" arg2="fit4tc" />
					<then>
						<if>
							<equals arg1="${sfcMtcServiceName}" arg2="SFCMTCService" />
							<then>
								<property name="SystemdServiceFile" value="${BASE_DIR}/etc/systemd/SFC_MTCServer.service"/>
								<property name="SystemdServiceFileName" value="SFC_MTCServer.service"/>
							</then>
							<else>
								<copy verbose="true" file="${BASE_DIR}/etc/systemd/SFC_MTCServer.service" tofile="${BASE_DIR}/etc/systemd/SFC_MTCServer.${sfcMtcServiceName}.service"/>
								<property name="SystemdServiceFile" value="${BASE_DIR}/etc/systemd/SFC_MTCServer.${sfcMtcServiceName}.service"/>
								<property name="SystemdServiceFileName" value="SFC_MTCServer.${sfcMtcServiceName}.service"/>
							</else>
						</if>
						<echo message="  Using sudo to register systemd service description..." />
						<shellscript shell="sh" dir="${BASE_DIR}/etc/systemd" osfamily="unix">
							if [ "${sfcMtcServiceUserPassword}" = "" ]
								then sudo cp  ${SystemdServiceFile} /etc/systemd/system
								else echo "${sfcMtcServiceUserPassword}" | sudo -S cp  ${SystemdServiceFile} /etc/systemd/system
							fi
							sudo systemctl daemon-reload
							if [ "${sfcMtcServiceStartupType}" = "auto" ]
							   then sudo systemctl enable ${SystemdServiceFileName}
							fi
						</shellscript>
					</then>
					<else>
					</else>
				</if>
				
			</then>
			<else>
				<if>
					<equals arg1="${os.arch}" arg2="x86" />
					<then>
						<echo message="Install 32bit Tomcat Service Wrapper..." />
						<copy overwrite="false" file="${DST_DIR}/Tomcat 5.0/bin/tomcat8_32.exe" tofile="${DST_DIR}/Tomcat 5.0/bin/${SERVICENAME}.exe" />
						<copy overwrite="false" file="${DST_DIR}/Tomcat 5.0/bin/tomcat8_64.exe" tofile="${DST_DIR}/Tomcat 5.0/bin/${SERVICENAME}_64.exe" />
					</then>
					<else>
						<echo message="Install 64bit Tomcat Service Wrapper..." />
						<copy overwrite="false" file="${DST_DIR}/Tomcat 5.0/bin/tomcat8_32.exe" tofile="${DST_DIR}/Tomcat 5.0/bin/${SERVICENAME}_32.exe" />
						<copy overwrite="false" file="${DST_DIR}/Tomcat 5.0/bin/tomcat8_64.exe" tofile="${DST_DIR}/Tomcat 5.0/bin/${SERVICENAME}.exe" />
					</else>
				</if>
				<copy overwrite="false" file="${DST_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" />

				<!-- setenv.sh -->
				<echo message="Create setenv.bat" />
				<copy file="${DST_DIR}/Tomcat 5.0/bin/setenv_${INST_SYS}.bat" tofile="${DST_DIR}/Tomcat 5.0/bin/setenv.bat" />
				<replace file="${DST_DIR}/Tomcat 5.0/bin/setenv.bat" token="%TCFITDIR%" value="${BASE_DIR}"/>
				
				<!-- instal_...bat ausführen -->
				<echo message="Register Windows service..." if:true="${sfcMtcRegisterAsService}"/>
				<if>
					<equals arg1="${INST_SYS}" arg2="fit4tc" />
					<then>
						<exec executable="SFC_MTC_Tomcat_service.bat" dir="${DST_DIR}/Tomcat 5.0/bin" resolveexecutable="true" if:true="${sfcMtcRegisterAsService}">
							<env key="TCFITDIR" value="${BASE_DIR}"/>
							<arg value="install"/>
							<arg value="${sfcMtcServiceName}"/>
							<arg value="${sfcMtcServiceStartupType}" />
							<arg value="${sfcMtcServiceUserName}" if:set="sfcMtcServiceUserName"/>
							<arg value="${sfcMtcServiceUserPassword}" if:set="sfcMtcServiceUserPassword"/>
						</exec>
					</then>
					<else>
						<exec executable="install_${INST_SYS}.bat" dir="${DST_DIR}/Tomcat 5.0/bin" resolveexecutable="true" if:true="${sfcMtcRegisterAsService}">
							<env key="TCFITDIR" value="${BASE_DIR}"/>
							<env key="FITDIR" value="${BASE_DIR}"/>
						</exec>
					</else>
				</if>
			</else>
		</if>

		<if>
			<equals arg1="${isStandAlone}" arg2="true" />
			<then>
				<echo message="Create selfsigned TLS certificates..." />
				
				<exec dir="${BASE_DIR}${file.separator}bin" executable="cmd" osfamily="windows">
					<arg value="/c" />
					<arg value="gen_ssl_cert.bat" />
					<arg value="${NAME}" />
				</exec>

				<exec dir="${BASE_DIR}${file.separator}bin" executable="/bin/sh" osfamily="unix">
					<arg value="gen_ssl_cert.sh" />
					<arg value="${NAME}" />
				</exec>

				<if>
					<equals arg1="${INST_OS}" arg2="unix" />
					<then>
						<replace file="${BASE_DIR}/saveLogFile.sh" token="${TCFITDIR}" value="${BASE_DIR}"/>
						<replace file="${BASE_DIR}/saveLogFile.bat" token="Tomcat 5.0\lib\" value="FIT-MI_Server/webapps/wut/WEB-INF/lib/"/>						
					</then>
					<else>
						<replace file="${BASE_DIR}/saveLogFile.bat" token="%TCFITDIR%" value="${BASE_DIR}"/>
						<replace file="${BASE_DIR}/saveLogFile.bat" token="Tomcat 5.0\lib\" value="FIT-MI_Server\webapps\wut\WEB-INF\lib\"/>						
					</else>
				</if>
				
			</then>
		</if>
			
		<if>
			<equals arg1="${INST_SYS}" arg2="fit4tc" />
			<then>
				<copy overwrite="false" file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/mi_server.ini.SFC" tofile="${DST_DIR}/webapps/wut/WEB-INF/conf/mi_server.ini" />
				
				<if>
					<equals arg1="${sfcUseSecureCommunication}" arg2="true" />
					<then>
						<replace file="${DST_DIR}/webapps/wut/WEB-INF/conf/mi_server.ini">
							<replacefilter token="fsw_ip = localhost" value="fsw_ip = ${sfcHost}"/>
							<replacefilter token="fsw_port = 8888/tcdnc/services" value="fsw_port = ${sfcPort}/tcdnc/services"/>
							<replacefilter token="fsw_protocol = http" value="fsw_protocol = https"/>
							<replacefilter token="local_port = 8090" value="local_port = ${sfcMtcPortHttps}"/>
							<replacefilter token="local_ip = localhost" value="local_ip = ${sfcMtcHostName}"/>
						</replace>
					</then>
					<else>
						<replace file="${DST_DIR}/webapps/wut/WEB-INF/conf/mi_server.ini">
							<replacefilter token="fsw_ip = localhost" value="fsw_ip = ${sfcHost}"/>
							<replacefilter token="fsw_port = 8888/tcdnc/services" value="fsw_port = ${sfcPort}/tcdnc/services"/>
							<replacefilter token="local_port = 8090" value="local_port = ${sfcMtcPortHttp}"/>
							<replacefilter token="local_ip = localhost" value="local_ip = ${sfcMtcHostName}"/>
						</replace>
					</else>
				</if>
				<if>
					<equals arg1="${os.name}" arg2="Linux"/>
					<then>
						<copy verbose="true" file="${DST_DIR}/bin/init.d_scripts/Linux/SFC_MTCServer.sh" tofile="${BASE_DIR}/etc/init.d/SFC_MTCServer.sh"/>
						<copy verbose="true" file="${DST_DIR}/bin/init.d_scripts/Linux/systemd/SFC_MTCServer.service" tofile="${BASE_DIR}/etc/systemd/SFC_MTCServer.service"/>

						<replace file="${BASE_DIR}/etc/systemd/SFC_MTCServer.service" token="@@TCFITDIR@@" value="${BASE_DIR}"/>
						<replace file="${BASE_DIR}/etc/systemd/SFC_MTCServer.service" token="@@OSUSERNAME@@" value="${user.name}"/>
						<replace file="${BASE_DIR}/etc/init.d/SFC_MTCServer.sh" token="@@TCFITDIR@@" value="${BASE_DIR}"/>
						<replace file="${BASE_DIR}/etc/init.d/SFC_MTCServer.sh" token="@@OSUSERNAME@@" value="${user.name}"/>
						
						<chmod dir="${BASE_DIR}/etc/init.d" perm="u+x" includes="**/*.sh"/>
						<chmod dir="${BASE_DIR}/etc/systemd" perm="u+x" includes="**/*.sh **/*.service"/>
					</then>
				</if>
				
				<!-- Prepare HTTPS -->
				<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>

				<!-- Delete FIT related configuration files -->
				<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/machine1.properties" />
				<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/opcua1.properties" />
				<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/sinrpc_db_config.ini.default" />
				<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/genius-upload.templ.default" />
				<delete 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>
				<copy overwrite="false" file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/mi_server.ini.FIT" tofile="${DST_DIR}/webapps/wut/WEB-INF/conf/mi_server.ini" />
				
				<!-- Bug 4409: Use new Java directory -->
				<replace file="${DST_DIR}/Tomcat 5.0/bin/setenv.bat" token="@@JAVA_HOME@@" value="${j.home}"/>
				
				<!-- Prepare HTTPS -->
				<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>

				<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>

		<replace file="${DST_DIR}/Tomcat 5.0/conf/server.xml">
			<replacefilter token="@@SHUTDOWNPORT@@" value="${sfcMtcPortShutdown}"/>
			<replacefilter token="@@HTTPPORT@@" value="${sfcMtcPortHttp}"/>
			<replacefilter token="@@HTTPSPORT@@" value="${sfcMtcPortHttps}"/>
		</replace>
			
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/mi_server.ini.SFC" />
		<delete file="${DST_DIR}/webapps/wut/WEB-INF/conf/defaults/mi_server.ini.FIT" />
	</target>

</project>
