<?xml version="1.0"?>

<project name="ext-web-parent">
	<import file="../build-common.xml" />

	<property name="war.file" value="${ant.project.name}" />

	<target name="clean">
		<delete dir="classes" />
		<delete dir="tmp" />
		<delete file="${war.file}.war" failonerror="false" />
	</target>

	<target name="compile-common-lib">
		<copy todir="${classpath.jsp}">
			<fileset dir="${project.dir}/ext-impl/classes" />
			<fileset dir="${project.dir}/ext-service/classes" />
		</copy>

		<unjar src="${project.dir}/modules/portal-impl.jar" dest="${classpath.jsp}" />

		<unjar src="${project.dir}/lib/global/portal-kernel.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/global/portal-service.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/util-java.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/util-taglib.jar" dest="${classpath.jsp}" />

		<unjar src="${project.dir}/lib/development/activation.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/development/mail.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/global/portlet.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/ant.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/backport-concurrent.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/commons-collections.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/commons-fileupload.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/commons-lang.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/commons-logging.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/commons-math.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/displaytag.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/dom4j.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/jabsorb.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/jackrabbit-text-extractors.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/jcifs.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/jcommon.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/jdom.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/jfreechart.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/jstl.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/jstl-impl.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/log4j.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/openid4java.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/rome.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/struts.jar" dest="${classpath.jsp}" />
		<unjar src="${project.dir}/lib/portal/velocity.jar" dest="${classpath.jsp}" />
	</target>

	<target name="compile-common-jsp">
		<delete dir="${jspc.classes.dir}" />

		<java
			classname="org.apache.jasper.JspC"
			classpath="${jspc.classpath}"
			fork="true"
			newenvironment="true"
		>
			<arg line="${arg.line}" />
		</java>

		<javac
			classpath="${jspc.classpath}"
			compiler="${javac.compiler}"
			deprecation="${javac.deprecation}"
			destdir="${jspc.classes.dir}"
			nowarn="on"
			srcdir="${jspc.classes.dir}"
		/>
	</target>

	<target name="compile-jboss-tomcat">
		<if>
			<equals arg1="${app.server.version}" arg2="4.2" />
			<then>
				<property name="jspc.classes.dir" value="classes/${app.server.type}" />
				<property name="jspc.classpath" value="${env.JAVA_HOME}/jre/lib/rt.jar;${classpath.jsp};${app.server.dir}/lib/jboss-common.jar;${app.server.deploy.dir}/jboss-web.deployer/jasper-jdt.jar;${app.server.deploy.dir}/jboss-web.deployer/jbossweb.jar;${app.server.instance.dir}/lib/el-api.jar" />

				<if>
					<not>
						<available file="${classpath.jsp}" />
					</not>
					<then>
						<antcall target="compile-common-lib" />

						<unjar src="${app.server.instance.dir}/lib/jsp-api.jar" dest="${classpath.jsp}" />
						<unjar src="${app.server.instance.dir}/lib/servlet-api.jar" dest="${classpath.jsp}" />
						<unjar src="${app.server.deploy.dir}/jboss-web.deployer/jstl.jar" dest="${classpath.jsp}" />
					</then>
				</if>
			</then>
			<elseif>
				<equals arg1="${app.server.version}" arg2="5.0" />
				<then>
					<property name="jspc.classes.dir" value="classes/${app.server.type}" />
					<property name="jspc.classpath" value="${env.JAVA_HOME}/jre/lib/rt.jar;${classpath.jsp};${app.server.dir}/lib/jboss-logging-spi.jar;${app.server.deploy.dir}/jbossweb.sar/jasper-jdt.jar;${app.server.deploy.dir}/jbossweb.sar/jbossweb.jar;${app.server.dir}/common/lib/el-api.jar" />

					<if>
						<not>
							<available file="${classpath.jsp}" />
						</not>
						<then>
							<antcall target="compile-common-lib" />

							<unjar src="${app.server.dir}/common/lib/jsp-api.jar" dest="${classpath.jsp}" />
							<unjar src="${app.server.dir}/common/lib/servlet-api.jar" dest="${classpath.jsp}" />
							<unjar src="${app.server.deploy.dir}/jbossweb.sar/jstl.jar" dest="${classpath.jsp}" />
						</then>
					</if>
				</then>
			</elseif>
		</if>

		<antcall target="compile-common-jsp">
			<param name="arg.line" value="-classpath ${jspc.classpath} -d ${jspc.classes.dir} -webapp ${jsp.precompile.dir}" />
		</antcall>
	</target>

	<target name="compile-jonas-jetty" depends="merge">
		<property name="jspc.classes.dir" value="classes/${app.server.type}" />
		<property name="jspc.classpath" value="${env.JAVA_HOME}/jre/lib/rt.jar;${classpath.jsp};${app.server.dir}/lib/jetty/ext/commons-el.jar;${app.server.dir}/lib/jetty/ext/jasper-compiler.jar;${app.server.dir}/lib/jetty/ext/jasper-runtime.jar" />

		<if>
			<not>
				<available file="${classpath.jsp}" />
			</not>
			<then>
				<antcall target="compile-common-lib" />

				<unjar src="${app.server.dir}/lib/commons/j2ee/jsp-2_0.jar" dest="${classpath.jsp}" />
				<unjar src="${app.server.dir}/lib/commons/j2ee/servlet-2_4.jar" dest="${classpath.jsp}" />
			</then>
		</if>

		<antcall target="compile-common-jsp">
			<param name="arg.line" value="-d ${jspc.classes.dir} -webapp ${jsp.precompile.dir}" />
		</antcall>
	</target>

	<target name="compile-jonas-tomcat" depends="merge">
		<property name="jspc.classes.dir" value="classes/${app.server.type}" />
		<property name="jspc.classpath" value="${env.JAVA_HOME}/jre/lib/rt.jar;${classpath.jsp};${app.server.dir}/lib/catalina/common/lib/commons-el.jar;${app.server.dir}/lib/catalina/common/lib/jasper-compiler.jar;${app.server.dir}/lib/catalina/common/lib/jasper-runtime.jar" />

		<if>
			<not>
				<available file="${classpath.jsp}" />
			</not>
			<then>
				<antcall target="compile-common-lib" />

				<unjar src="${app.server.dir}/lib/commons/j2ee/jsp-2_0.jar" dest="${classpath.jsp}" />
				<unjar src="${app.server.dir}/lib/commons/j2ee/servlet-2_4.jar" dest="${classpath.jsp}" />
			</then>
		</if>

		<antcall target="compile-common-jsp">
			<param name="arg.line" value="-d ${jspc.classes.dir} -webapp ${jsp.precompile.dir}" />
		</antcall>
	</target>

	<target name="compile-resin" depends="merge">
		<property name="jspc.classes.dir" value="classes/${app.server.type}" />
		<property name="jspc.classpath" value="${env.JAVA_HOME}/jre/lib/rt.jar;${env.JAVA_HOME}/lib/tools.jar;${classpath.jsp};${app.server.dir}/lib/jaxws-api.jar;${app.server.dir}/lib/jms-11.jar;${app.server.dir}/lib/jsdk-15.jar;${app.server.dir}/lib/jstl-11.jar;${app.server.dir}/lib/jta-101.jar;${app.server.dir}/lib/resin.jar" />

		<if>
			<not>
				<available file="${classpath.jsp}" />
			</not>
			<then>
				<antcall target="compile-common-lib" />

				<unjar src="${project.dir}/lib/development/saxpath.jar" dest="${classpath.jsp}" />
				<unjar src="${project.dir}/lib/portal/axis.jar" dest="${classpath.jsp}" />
				<unjar src="${project.dir}/lib/portal/cas-client-core.jar" dest="${classpath.jsp}" />
				<unjar src="${project.dir}/lib/portal/commons-beanutils.jar" dest="${classpath.jsp}" />
				<unjar src="${project.dir}/lib/portal/commons-digester.jar" dest="${classpath.jsp}" />
				<unjar src="${project.dir}/lib/portal/commons-httpclient.jar" dest="${classpath.jsp}" />
				<unjar src="${project.dir}/lib/portal/portletbridge-core.jar" dest="${classpath.jsp}" />
				<unjar src="${project.dir}/lib/portal/portletbridge-portlet.jar" dest="${classpath.jsp}" />
				<unjar src="${project.dir}/lib/portal/simplecaptcha.jar" dest="${classpath.jsp}" />
				<unjar src="${project.dir}/lib/portal/spring.jar" dest="${classpath.jsp}" />
				<unjar src="${project.dir}/lib/portal/struts-el.jar" dest="${classpath.jsp}" />
				<unjar src="${project.dir}/lib/portal/urlrewrite.jar" dest="${classpath.jsp}" />
			</then>
		</if>

		<copy file="tmp/WEB-INF/web.xml" tofile="tmp/WEB-INF/web.xml.bak" />

		<replace file="tmp/WEB-INF/web.xml">
			<replacefilter token="&lt;resource-ref&gt;" value="" />
			<replacefilter token="&lt;/resource-ref&gt;" value="" />
		</replace>

		<delete dir="${jspc.classes.dir}" />

		<java
			classname="com.liferay.portal.tools.jspc.resin.BatchJspCompiler"
			classpath="${jspc.classpath}"
			fork="true"
			maxmemory="512m"
			newenvironment="true"
		>
			<jvmarg value="-Dexternal-properties=com/liferay/portal/tools/dependencies/portal-tools.properties" />
			<arg line="tmp ${jspc.classes.dir}" />
		</java>

		<move file="tmp/WEB-INF/web.xml.bak" tofile="tmp/WEB-INF/web.xml" overwrite="yes" />
	</target>

	<target name="compile-tomcat">
		<if>
			<equals arg1="${app.server.version}" arg2="5.5" />
			<then>
				<property name="jspc.classes.dir" value="classes/${app.server.type}" />
				<property name="jspc.classpath" value="${env.JAVA_HOME}/jre/lib/rt.jar;${classpath.jsp};${app.server.dir}/common/lib/commons-el.jar;${app.server.dir}/common/lib/jasper-compiler.jar;${app.server.dir}/common/lib/jasper-runtime.jar" />

				<if>
					<not>
						<available file="${classpath.jsp}" />
					</not>
					<then>
						<antcall target="compile-common-lib" />

						<unjar src="${app.server.dir}/common/lib/jsp-api.jar" dest="${classpath.jsp}" />
						<unjar src="${app.server.dir}/common/lib/servlet-api.jar" dest="${classpath.jsp}" />
					</then>
				</if>
			</then>
			<elseif>
				<equals arg1="${app.server.version}" arg2="6.0" />
				<then>
					<property name="jspc.classes.dir" value="classes/${app.server.type}" />
					<property name="jspc.classpath" value="${env.JAVA_HOME}/jre/lib/rt.jar;${classpath.jsp};${app.server.dir}/lib/jasper.jar;${app.server.dir}/lib/jasper-el.jar;${app.server.dir}/lib/jasper-jdt.jar;${app.server.bin.dir}/tomcat-juli.jar" />

					<if>
						<not>
							<available file="${classpath.jsp}" />
						</not>
						<then>
							<antcall target="compile-common-lib" />

							<unjar src="${app.server.dir}/lib/el-api.jar" dest="${classpath.jsp}" />
							<unjar src="${app.server.dir}/lib/jsp-api.jar" dest="${classpath.jsp}" />
							<unjar src="${app.server.dir}/lib/servlet-api.jar" dest="${classpath.jsp}" />
						</then>
					</if>
				</then>
			</elseif>
		</if>

		<antcall target="compile-common-jsp">
			<param name="arg.line" value="-d ${jspc.classes.dir} -webapp ${jsp.precompile.dir}" />
		</antcall>
	</target>

	<target name="war" depends="merge">
		<war
			basedir="tmp"
			destfile="${war.file}.war"
			excludes="WEB-INF/web.xml,WEB-INF/web.xml.original"
			webxml="tmp/WEB-INF/web.xml"
		/>
	</target>

	<target name="deploy" depends="merge">
		<antcall target="deploy-war" />

		<if>
			<equals arg1="${jsp.precompile}" arg2="on" />
			<then>
				<if>
					<equals arg1="${app.server.type}" arg2="jboss-tomcat" />
					<then>
						<antcall target="compile-jboss-tomcat" />

						<copy todir="${app.server.instance.dir}/work/jboss.web/localhost/_">
							<fileset dir="classes/${app.server.type}">
								<exclude name="**/*.jspc_error" />
								<exclude name="**/*.java" />
							</fileset>
						</copy>
					</then>
					<elseif>
						<equals arg1="${app.server.type}" arg2="jonas-jetty" />
						<then>
							<antcall target="compile-jonas-jetty" />

							<copy todir="${app.server.dir}/work">
								<fileset dir="classes/${app.server.type}">
									<exclude name="**/*.jspc_error" />
									<exclude name="**/*.java" />
								</fileset>
							</copy>
						</then>
					</elseif>
					<elseif>
						<equals arg1="${app.server.type}" arg2="jonas-tomcat" />
						<then>
							<antcall target="compile-jonas-tomcat" />

							<copy todir="${app.server.dir}/work/jonas/localhost/_">
								<fileset dir="classes/${app.server.type}">
									<exclude name="**/*.jspc_error" />
									<exclude name="**/*.java" />
								</fileset>
							</copy>
						</then>
					</elseif>
					<elseif>
						<equals arg1="${app.server.type}" arg2="resin" />
						<then>
							<antcall target="compile-resin" />

							<copy todir="${app.server.portal.dir}/WEB-INF/work">
								<fileset dir="classes/${app.server.type}">
									<exclude name="**/*.jspc_error" />
									<exclude name="**/*.java" />
								</fileset>
							</copy>
						</then>
					</elseif>
					<elseif>
						<equals arg1="${app.server.type}" arg2="tomcat" />
						<then>
							<antcall target="compile-tomcat" />

							<copy todir="${app.server.dir}/work/Catalina/localhost/_">
								<fileset dir="classes/${app.server.type}">
									<exclude name="**/*.jspc_error" />
									<exclude name="**/*.java" />
								</fileset>
							</copy>
						</then>
					</elseif>
				</if>
			</then>
		</if>
	</target>

	<target name="deploy-fast">
		<if>
			<equals arg1="${app.server.type}" arg2="tomcat" />
			<then>
				<copy todir="${app.server.portal.dir}">
					<fileset dir="docroot">
						<include name="**/*.css" />
						<include name="**/*.dtd" />
						<include name="**/*.gif" />
						<include name="**/*.jar" />
						<include name="**/*.jpg" />
						<include name="**/*.js" />
						<include name="**/*.jsp" />
						<include name="**/*.jspf" />
						<include name="**/*.png" />
						<include name="**/*.properties" />
						<include name="**/*.tld" />
						<include name="**/*.vm" />
						<include name="**/*.xml" />
					</fileset>
				</copy>
			</then>
		</if>
	</target>

	<target name="build-webxml">
		<java
			classname="com.liferay.portal.tools.WebXMLBuilder"
			classpathref="project.classpath"
			fork="true"
			newenvironment="true"
		>
			<jvmarg value="-Dexternal-properties=com/liferay/portal/tools/dependencies/portal-tools.properties" />
			<arg value="tmp/WEB-INF/web.xml.original" />
			<arg value="docroot/WEB-INF/web.xml" />
			<arg value="tmp/WEB-INF/web.xml" />
		</java>
	</target>

	<target name="merge" depends="compile">
		<if>
			<not>
				<uptodate
					srcfile="${project.dir}/modules/portal-web.war"
					targetfile="tmp"
				/>
			</not>
			<then>
				<delete dir="tmp" failonerror="false" />
				<mkdir dir="tmp" />

				<unjar
					dest="tmp"
					src="${project.dir}/modules/portal-web.war"
					overwrite="true"
				/>

				<copy todir="tmp" overwrite="true">
					<fileset
						dir="docroot"
						excludes="**/struts-config.xml,**/tiles-defs.xml,**/web.xml"
					/>
				</copy>

				<copy
					file="tmp/WEB-INF/web.xml"
					tofile="tmp/WEB-INF/web.xml.original"
				/>

				<replace file="tmp/WEB-INF/web.xml.original">
					<replacefilter
						token="/WEB-INF/struts-config.xml"
						value="/WEB-INF/struts-config.xml,/WEB-INF/struts-config-ext.xml"
					/>
				</replace>
			</then>
		</if>

		<copy todir="tmp" overwrite="false">
			<fileset
				dir="docroot"
				excludes="**/struts-config.xml,**/tiles-defs.xml,**/web.xml"
			/>
		</copy>

		<copy
			file="docroot/WEB-INF/struts-config.xml"
			tofile="tmp/WEB-INF/struts-config-ext.xml"
		/>

		<copy
			file="docroot/WEB-INF/tiles-defs.xml"
			tofile="tmp/WEB-INF/tiles-defs-ext.xml"
		/>

		<antcall target="build-webxml" />

		<touch>
			<fileset dir="tmp" />
		</touch>
	</target>
</project>
