<?xml version="1.0"?>

<project name="portal-test-tomcat" basedir="." default="test" xmlns:antelope="antlib:ise.antelope.tasks">
	<import file="build-test.xml" />

 	<target name="copy-5.1.7-5.2.7-upgrade-dependencies">
		<copy
			file="${hudson.portal.dependencies.dir}${lp.version}/lpe2005.7-ee5207-portal-impl-jdk5.jar"
			todir="${app.server.tomcat.lib.portal.dir}"
		/>

		<copy
			file="${hudson.portal.dependencies.dir}${lp.version}/lpe2005-ee5207-hook-5.2.7.1.war"
			todir="${user.home}/liferay/deploy"
		/>
	</target>

	<target name="prepare-cluster-tomcat-node">
		<exec executable="${pscp.executable}">
			<arg line="-pw ${vm.password} dist/liferay-portal-tomcat-${lp.version}.zip ${vm.username}@${cluster-node.host}:/" />
		</exec>

		<exec executable="${plink.executable}">
			<arg line="-pw ${vm.password} ${vm.username}@${cluster-node.host} cmd.exe /c C:\Progra~1\7-Zip\7z.exe x C:\liferay-portal-tomcat-${lp.version}.zip -oC:\" />
		</exec>

		<antcall target="replace-remote-file">
			<param name="remote.host" value="${cluster-node.host}" />
			<param name="remote.file" value="/liferay-portal-${lp.version}/tomcat-${app.server.tomcat.version}/bin/startup.bat" />
			<param name="remote.replace.token" value="set &quot;CATALINA_HOME=%cd%&quot;" />
			<param name="remote.replace.value" value="set CATALINA_HOME=C:\liferay-portal-${lp.version}\tomcat-${app.server.tomcat.version}" />
		</antcall>

		<antcall target="replace-remote-file">
			<param name="remote.host" value="${cluster-node.host}" />
			<param name="remote.file" value="/liferay-portal-${lp.version}/tomcat-${app.server.tomcat.version}/bin/shutdown.bat" />
			<param name="remote.replace.token" value="set &quot;CATALINA_HOME=%cd%&quot;" />
			<param name="remote.replace.value" value="set CATALINA_HOME=C:\liferay-portal-${lp.version}\tomcat-${app.server.tomcat.version}" />
		</antcall>

		<antcall target="replace-remote-file">
			<param name="remote.host" value="${cluster-node.host}" />
			<param name="remote.file" value="/liferay-portal-${lp.version}/tomcat-${app.server.tomcat.version}/conf/server.xml" />
			<param name="remote.replace.token" value="&lt;Engine name=&quot;Catalina&quot; defaultHost=&quot;localhost&quot;&gt;" />
			<param name="remote.replace.value" value="&lt;Engine name=&quot;Catalina&quot; defaultHost=&quot;localhost&quot; jvmRoute=&quot;${cluster-node.value}&quot;&gt;" />
		</antcall>

		<antcall target="prepare-portal-ext-properties" inheritAll="false" />

		<echo file="portal-impl/src/portal-ext.properties" append="true">

net.sf.ehcache.configurationResourceName=/ehcache/hibernate-clustered.xml

ehcache.multi.vm.config.location=/ehcache/liferay-multi-vm-clustered.xml

redirect.url.ips.allowed=127.0.0.1,SERVER_IP,${vm.cluster-node-1.ip}

cluster.link.enabled=true

web.server.display.node=true</echo>

		<exec executable="${pscp.executable}">
			<arg line="-pw ${vm.password} portal-impl/src/portal-ext.properties ${vm.username}@${cluster-node.host}:/liferay-portal-${lp.version}/tomcat-${app.server.tomcat.version}/webapps/ROOT/WEB-INF/classes" />
		</exec>

		<delete file="portal-impl/src/portal-ext.properties" />
	</target>

	<target name="prepare-vm-tomcat">
		<antcall target="prepare-zip-tomcat" />

		<exec executable="${pscp.executable}">
			<arg line="-pw ${vm.password} dist/liferay-portal-tomcat-${lp.version}.zip ${vm.username}@${vm.host}:/" />
		</exec>

		<exec executable="${plink.executable}">
			<arg line="-pw ${vm.password} ${vm.username}@${vm.host} cmd.exe /c C:\Progra~1\7-Zip\7z.exe x C:\liferay-portal-tomcat-${lp.version}.zip -oC:\" />
		</exec>

		<antcall target="replace-remote-file">
			<param name="remote.host" value="${vm.host}" />
			<param name="remote.file" value="/liferay-portal-${lp.version}/tomcat-${app.server.tomcat.version}/bin/startup.bat" />
			<param name="remote.replace.token" value='set "CATALINA_HOME=%cd%"' />
			<param name="remote.replace.value" value="set CATALINA_HOME=C:\liferay-portal-${lp.version}\tomcat-${app.server.tomcat.version}" />
		</antcall>
	</target>

	<target name="prepare-zip-tomcat">
		<ant antfile="build-dist.xml" target="build-dist-tomcat" />

		<antcall target="revert-test-properties" />

		<mkdir dir="dist" />

		<ant antfile="build-dist.xml" target="zip-tomcat" />
	</target>

	<target name="run-selenium-tomcat">
		<if>
			<not>
				<isset property="build.app.server" />
			</not>
			<then>
				<ant antfile="build-dist.xml" target="build-dist-tomcat" />
			</then>
		</if>

		<antcall target="revert-test-properties" />

		<if>
			<isset property="vm.host" />
			<then>
				<replace
					file="portal-web/test/test-portal-web.properties"
					token="#vm.host="
					value="vm.host=${vm.host}"
				/>
			</then>
		</if>

		<antcall target="prepare-virtual-host-name-properties" />

		<antcall target="start-selenium" />

		<antcall target="run-tomcat" inheritAll="false">
			<param name="test.class" value="${test.name}" />
		</antcall>

		<antcall target="stop-selenium" />
	</target>

	<target name="run-selenium-versioned-tomcat">
		<if>
			<not>
				<uptodate
					srcfile="${hudson.portal.dependencies.dir}/${lp.version}/${file.name}"
					targetfile="${app.server.parent.dir}/${file.name}"
				/>
			</not>
			<then>
				<copy
					file="${hudson.portal.dependencies.dir}/${lp.version}/${file.name}"
					todir="${app.server.parent.dir}"
				/>
			</then>
		</if>

		<unzip src="${app.server.parent.dir}/${file.name}" dest="${app.server.parent.dir}" />

		<echo file="app.server.${user.name}.properties">app.server.tomcat.dir=${app.server.parent.dir}/${tomcat.folder.dir}</echo>

		<antcall target="revert-test-properties" />

		<if>
			<isset property="jdbc.drivers.optional.dir" />
			<then>
				<antcall target="copy-optional-jars" inheritAll="false" />
			</then>
		</if>

		<if>
			<equals arg1="${legacy.config}" arg2="true" />
			<then>
				<if>
					<equals arg1="${db.type}" arg2="mysql" />
					<then>
						<antcall target="prepare-root-xml" inheritAll="false">
							<param name="db.url" value="jdbc:mysql://${db.mysql.host}/${db.mysql.schema}?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;useFastDateParsing=false" />
						</antcall>
					</then>
					<else>
						<antcall target="prepare-root-xml" inheritAll="false" />
					</else>
				</if>
			</then>
		</if>

		<antcall target="deploy-properties" inheritAll="false" />

		<delete dir="${user.home}/liferay" />

		<if>
			<or>
				<equals arg1="${lp.version}" arg2="5.2.5" />
				<equals arg1="${lp.version}" arg2="5.2.7" />
				<equals arg1="${lp.version}" arg2="5.2.8" />
			</or>
			<then>
				<copy
					file="${hudson.license.dependencies.dir}/${lp.version}/license"
					todir="${user.home}/liferay/ee"
				/>
			</then>
			<elseif>
				<or>
					<equals arg1="${lp.version}" arg2="5.2.9" />
					<equals arg1="${lp.version}" arg2="6.0.10" />
					<equals arg1="${lp.version}" arg2="6.0.11" />
					<equals arg1="${lp.version}" arg2="6.0.12" />
				</or>
				<then>
					<copy
						todir="${user.home}/liferay/deploy">
							<fileset
								dir="${hudson.license.dependencies.dir}/${lp.version}"
								includes="license-portal-*.xml"
						/>
					</copy>
				</then>
			</elseif>
		</if>

		<antcall target="start-selenium" />

		<antcall target="run-tomcat" inheritAll="false">
			<param name="test.class" value="${test.name}" />
		</antcall>

		<antcall target="stop-selenium" />
	</target>

	<target name="run-tomcat">
		<antcall target="clean-up-logs" />

		<record action="start" name="log" />

		<antcall target="run-simple-server">
			<param name="simple.server.dir" value="${app.server.tomcat.dir}" />
			<param name="simple.server.bin.dir" value="${app.server.tomcat.bin.dir}" />
			<param name="simple.server.deploy.dir" value="${app.server.tomcat.deploy.dir}" />
			<param name="simple.server.lib.global.dir" value="${app.server.tomcat.lib.global.dir}" />
			<param name="simple.server.portal.dir" value="${app.server.tomcat.portal.dir}" />
			<param name="simple.server.start.executable" value="catalina${file.suffix.bat}" />
			<param name="simple.server.start.executable.arg.line" value="run" />
			<param name="simple.server.stop.executable" value="shutdown${file.suffix.bat}" />
		</antcall>

		<record action="stop" name="log" />

		<antcall target="evaluate-logs" />
	</target>
</project>