<?xml version="1.0"?>

<project name="portal" basedir="." default="compile" xmlns:antelope="antlib:ise.antelope.tasks">
	<property name="project.dir" value="." />

	<import file="build-common.xml" />

	<target name="all">
		<antcall target="clean" />
		<antcall target="start" />
		<antcall target="deploy" />
	</target>

	<target name="clean">
		<ant dir="classes" target="clean" inheritAll="false" />

		<ant dir="portal-service" target="clean" inheritAll="false" />

		<ant dir="util-bridges" target="clean" inheritAll="false" />
		<ant dir="util-java" target="clean" inheritAll="false" />
		<ant dir="util-taglib" target="clean" inheritAll="false" />

		<ant dir="portal-impl" target="clean" inheritAll="false" />

		<ant dir="portal-web" target="clean" inheritAll="false" />

		<ant dir="sql" target="clean" inheritAll="false" />

		<delete dir="${doc.dir}" />

		<delete>
			<fileset dir="." includes="*.ear,*.jar,*.war,*.zip" />
		</delete>

		<delete includeemptydirs="true" failonerror="false">
			<fileset
				dir="${app.server.bin.dir}"
				includes="*.log*"
			/>
		</delete>

		<delete failonerror="false">
			<fileset
				dir="${liferay.home}/data/hsql"
				includes="${database.name}.*"
			/>
		</delete>

		<if>
			<equals arg1="${app.server.type}" arg2="glassfish" />
			<then>
				<delete dir="${app.server.instance.dir}/applications/liferay-portal" />
			</then>
		</if>

		<if>
			<equals arg1="${app.server.type}" arg2="tomcat" />
			<then>
				<delete failonerror="false">
					<fileset
						dir="${app.server.dir}/conf/Catalina/localhost"
						includes="*-hook.xml,*-portlet.xml"
					/>
				</delete>

				<ant dir="support-tomcat" target="clean" inheritAll="false" />
			</then>
		</if>

		<delete failonerror="false">
			<fileset
				dir="${app.server.classes.portal.dir}"
				includes="portal-*.properties,system-*.properties"
			/>
		</delete>

		<delete includeemptydirs="true" failonerror="false">
			<fileset
				dir="${app.server.deploy.dir}"
				includes="*-ext/**,*-ext.war/**,*-hook/**,*-hook.war/**,*-layouttpl/**,*-layouttpl.war/**,*-portlet/**,*-portlet.war/**,*-theme/**,*-theme.war/**,*-web/**,*-web.war/**"
			/>
		</delete>

		<delete failonerror="false">
			<fileset
				dir="${app.server.lib.global.dir}"
				excludes="${jdbc.drivers}"
			/>
		</delete>

		<delete failonerror="false">
			<fileset
				dir="${app.server.lib.portal.dir}"
				excludes="${jdbc.drivers}"
			/>
		</delete>

		<if>
			<equals arg1="${clean.log.dir}" arg2="true" />
			<then>
				<delete dir="${app.server.log.dir}" />
			</then>
		</if>

		<if>
			<not>
				<and>
					<equals arg1="${app.server.type}" arg2="tomcat" />
					<antelope:endswith string="${app.server.portal.dir}" with="/portal-web/docroot" />
				</and>
			</not>
			<then>
				<delete dir="${app.server.portal.dir}" />
				<delete file="${app.server.portal.dir}" />
			</then>
		</if>

		<if>
			<equals arg1="${clean.temp.dir}" arg2="true" />
			<then>
				<delete dir="${app.server.temp.dir}" />
			</then>
		</if>

		<if>
			<equals arg1="${clean.work.dir}" arg2="true" />
			<then>
				<delete dir="${app.server.work.dir}" />
			</then>
		</if>

		<antcall target="print-current-time" />
	</target>

	<target name="compile">
		<ant dir="portal-service" target="compile" inheritAll="false" />

		<ant dir="util-bridges" target="compile" inheritAll="false" />
		<ant dir="util-java" target="compile" inheritAll="false" />
		<ant dir="util-taglib" target="compile" inheritAll="false" />

		<ant dir="portal-impl" target="compile" inheritAll="false" />
	</target>

	<target name="deploy">
		<if>
			<or>
				<equals arg1="${app.server.type}" arg2="glassfish" />
				<equals arg1="${app.server.type}" arg2="websphere" />
			</or>
			<then>
				<delete dir="${app.server.portal.dir}" />
				<delete file="${app.server.portal.dir}" />
			</then>
		</if>

		<if>
			<and>
				<equals arg1="${app.server.type}" arg2="tomcat" />
				<not>
					<available file="${app.server.tomcat.dir}/lib/catalina.jar" />
				</not>
			</and>
			<then>
				<fail>
.

Tomcat is not installed in ${app.server.tomcat.dir}. If you already have Tomcat
installed, make sure the property "$${app.server.tomcat.dir}" points to your
Tomcat installation. If you wish to automatically install Tomcat into
${app.server.tomcat.dir}, then run "ant -buildfile build-dist.xml unzip-tomcat".
				</fail>
			</then>
		</if>

		<ant dir="portal-service" target="deploy" inheritAll="false" />

		<ant dir="util-bridges" target="deploy" inheritAll="false" />
		<ant dir="util-java" target="deploy" inheritAll="false" />
		<ant dir="util-taglib" target="deploy" inheritAll="false" />

		<ant dir="portal-impl" target="deploy" inheritAll="false" />

		<ant dir="portal-web" target="deploy" inheritAll="false" />

		<copy todir="${liferay.home}/data/hsql">
			<fileset dir="sql">
				<include name="${database.name}.properties" />
				<include name="${database.name}.script" />
			</fileset>
		</copy>

		<antcall target="deploy-properties" />

		<copy todir="${app.server.lib.global.dir}">
			<fileset dir="lib/development" includes="hsql.jar,jtds.jar,mysql.jar,postgresql.jar" />
			<fileset dir="lib/global" />
		</copy>

		<if>
			<isset property="jdbc.drivers.optional.dir" />
			<then>
				<copy todir="${app.server.lib.global.dir}">
					<fileset
						dir="${jdbc.drivers.optional.dir}"
						includes="*.jar"
					/>
				</copy>
			</then>
		</if>

		<copy todir="${app.server.lib.portal.dir}">
			<fileset dir="lib/portal" />
		</copy>

		<if>
			<equals arg1="${app.server.type}" arg2="jboss" />
			<then>
				<if>
					<not>
						<available file="${app.server.lib.global.dir}/module.xml" />
					</not>
					<then>
						<copy
							file="tools/servers/jboss/modules/module.xml"
							tofile="${app.server.lib.global.dir}/module.xml"
						/>
					</then>
				</if>

				<delete>
					<fileset
						dir="${app.server.lib.portal.dir}"
						includes="eclipselink.jar"
					/>
				</delete>
			</then>
		</if>

		<if>
			<equals arg1="${app.server.type}" arg2="jetty" />
			<then>
				<if>
					<not>
						<available file="${app.server.dir}/contexts/root.xml" />
					</not>
					<then>
						<copy
							file="tools/servers/jetty/contexts/root.xml"
							tofile="${app.server.dir}/contexts/root.xml"
						/>
					</then>
				</if>

				<copy todir="${app.server.lib.global.dir}">
					<fileset
						dir="lib/development"
						includes="activation.jar,jta.jar,mail.jar,persistence.jar"
					/>
				</copy>
			</then>
		</if>

		<if>
			<equals arg1="${app.server.type}" arg2="jonas" />
			<then>
				<delete>
					<fileset
						dir="${app.server.lib.portal.dir}"
						includes="equinox.jar,osgi.core.jar,xalan.jar,xercesImpl.jar,xml-apis.jar"
					/>
				</delete>
			</then>
		</if>

		<if>
			<equals arg1="${app.server.type}" arg2="resin" />
			<then>
				<copy todir="${app.server.lib.global.dir}">
					<fileset
						dir="lib/development"
						includes="saxpath.jar"
					/>
				</copy>
			</then>
		</if>

		<if>
			<equals arg1="${app.server.type}" arg2="tomcat" />
			<then>
				<if>
					<not>
						<available file="${app.server.dir}/conf/Catalina/localhost/${app.server.tomcat.portal.context}.xml" />
					</not>
					<then>
						<copy
							file="tools/servers/tomcat/conf/Catalina/localhost/ROOT.xml"
							tofile="${app.server.dir}/conf/Catalina/localhost/${app.server.tomcat.portal.context}.xml"
							overwrite="true"
						/>
					</then>
				</if>

				<ant dir="support-tomcat" target="deploy" inheritAll="false" />

				<copy todir="${app.server.lib.endorsed.dir}" flatten="true">
					<fileset
						dir="lib"
						includes="${endorsed.libraries}"
					/>
				</copy>

				<if>
					<not>
						<equals arg1="${app.server.tomcat.lib.portal.dir}" arg2="${app.server.tomcat.lib.global.dir}" />
					</not>
					<then>
						<delete failonerror="false">
							<fileset
								dir="${app.server.lib.portal.dir}"
								includes="ccpp.jar"
							/>
						</delete>
					</then>
				</if>

				<copy todir="${app.server.lib.global.dir}">
					<fileset
						dir="lib/development"
						includes="activation.jar,jms.jar,jta.jar,mail.jar,persistence.jar"
					/>
				</copy>

				<copy todir="${app.server.temp.dir}/liferay/com/liferay/portal/deploy/dependencies">
					<fileset
						dir="lib/development"
						includes="resin.jar,script-10.jar"
					/>
				</copy>

				<delete>
					<fileset
						dir="${app.server.lib.portal.dir}"
						includes="tomcat-juli.jar"
					/>
				</delete>
			</then>
		</if>

		<mkdir dir="${app.server.log.dir}" />
		<mkdir dir="${app.server.temp.dir}" />
		<mkdir dir="${app.server.work.dir}" />

		<if>
			<or>
				<equals arg1="${app.server.type}" arg2="glassfish" />
				<equals arg1="${app.server.type}" arg2="websphere" />
			</or>
			<then>
				<tstamp>
					<format property="tstamp.value" pattern="yyyyMMddkkmmssSSS" />
				</tstamp>

				<jar
					basedir="${app.server.portal.dir}"
					jarfile="${tstamp.value}"
				/>

				<delete dir="${app.server.portal.dir}" />

				<move file="${tstamp.value}" tofile="${app.server.portal.dir}" />
			</then>
		</if>

		<if>
			<equals arg1="${app.server.type}" arg2="websphere" />
			<then>
				<exec dir="${app.server.websphere.bin.dir}" executable="cmd.exe">
					<arg line="/c startServer.bat server1" />
				</exec>

				<echo>Installing liferay-portal.war.</echo>

				<echo file="wsadmin.py">
def isAppInstalled(appName):
	appNames = AdminApp.list()

	if len(appNames) > 0:
		for curAppName in appNames.split('\n'):
			curAppName = curAppName.rstrip()

			if curAppName.find(appName) >= 0:
				return 1

	return 0

if isAppInstalled('DefaultApplication'):
	print AdminApp.uninstall('DefaultApplication')

	print AdminConfig.save()

appManager = AdminControl.queryNames('type=ApplicationManager,process=server1,*')

if isAppInstalled('liferay-portal'):
	print AdminControl.invoke(appManager, 'stopApplication', 'liferay-portal')

	print AdminApp.update('liferay-portal', 'app', '[-contents ${app.server.websphere.instance.dir}/wsadmin-deploy/liferay-portal.war -contextroot / -operation update]')
else:
	print AdminApp.install('${app.server.websphere.instance.dir}/wsadmin-deploy/liferay-portal.war', '[-appname liferay-portal -contextroot /]')

print AdminConfig.save()

print AdminControl.invoke(appManager, 'startApplication', 'liferay-portal')
				</echo>

				<exec dir="${app.server.websphere.bin.dir}" executable="cmd.exe">
					<arg line="/c wsadmin.bat -f ${basedir}\wsadmin.py" />
				</exec>

				<delete file="wsadmin.py" />

				<exec dir="${app.server.websphere.bin.dir}" executable="cmd.exe">
					<arg line="/c stopServer.bat server1" />
				</exec>
			</then>
		</if>

		<antcall target="print-current-time" />
	</target>

	<target name="deploy-properties">
		<copy todir="${app.server.classes.portal.dir}" overwrite="true">
			<fileset dir="portal-impl/src" includes="portal-*.properties,system-*.properties" />
		</copy>
	</target>

	<target name="dist">
		<ant antfile="build-dist.xml" target="dist" inheritAll="false" />
	</target>

	<target name="doc">
		<antcall target="dtddoc" />
		<antcall target="javadoc" />
		<antcall target="javadoc-all" />
		<ant dir="util-taglib" target="taglibdoc" inheritAll="false" />
	</target>

	<target name="dtddoc">
		<taskdef classname="DTDDoc.DTDDocTask" classpathref="lib.classpath" name="DTDDoc" />

		<!--<antelope:stringutil string="${lp.version}" property="lp.version.dtd">
			<replace regex="\." replacement="_" />
		</antelope:stringutil>-->

		<property name="doc.definitions.dir" value="${doc.dir}/definitions" />

		<DTDDoc
			destDir="${doc.definitions.dir}"
			docTitle = "Liferay ${lp.version} Definitions"
			showFixmeTags="false"
			showHiddenTags="false"
			sourceDir="${definitions.dir}"
		>
			<include name="liferay-*${lp.version.dtd}.dtd" />
		</DTDDoc>
	</target>

	<target name="jar">
		<ant dir="portal-service" target="jar" inheritAll="false" />

		<ant dir="util-bridges" target="jar" inheritAll="false" />
		<ant dir="util-java" target="jar" inheritAll="false" />
		<ant dir="util-taglib" target="jar" inheritAll="false" />

		<ant dir="portal-impl" target="jar" inheritAll="false" />

		<ant dir="portal-web" target="war" inheritAll="false" />

		<ant dir="support-tomcat" target="jar" inheritAll="false" />
	</target>

	<target name="javadoc">
		<property name="doc.java.dir" value="${doc.dir}/javadocs" />

		<mkdir dir="${doc.java.dir}" />

		<javadoc
			breakiterator="yes"
			classpathref="project.classpath"
			destdir="${doc.java.dir}"
			doctitle="Liferay ${lp.version} Public API Documentation"
			header="&lt;b&gt;Liferay ${lp.version}&lt;/b&gt;"
			maxmemory="1024m"
			noqualifier="java.*"
			overview="portal-service/src/portal-service-overview.html"
			stylesheetfile="${project.dir}/tools/javadoc.css"
			useexternalfile="yes"
			use="yes"
			windowtitle="Liferay ${lp.version} API"
		>
			<doclet name="com.liferay.tools.doclets.standard.Standard" path="${project.dir}/lib/development/liferay-doclet.jar">
				<param name="-linksource" />
			</doclet>

			<group title="Portal Services" packages="com.liferay.portal*:com.liferay.counter*:com.liferay.documentlibrary*:com.liferay.mail*" />
			<group title="Portal Kernel" packages="com.liferay.portal.kernel*" />
			<group title="Portlet Services" packages="com.liferay.portlet*" />
			<group title="Portlet Bridges" packages="com.liferay.util.bridges*" />

			<link href="http://docs.liferay.com/portlet-api/2.0/javadocs" />
			<link href="http://java.sun.com/javase/6/docs/api" />
			<link href="http://docs.liferay.com/portal/${lp.version.major}/javadocs" />

			<packageset dir="portal-service/src" />
			<packageset dir="util-bridges/src" />
			<packageset dir="util-java/src" />

			<tag name="generated" description="" />
		</javadoc>
	</target>

	<target name="javadoc-all">
		<property name="doc.java.dir" value="${doc.dir}/javadocs-all" />

		<mkdir dir="${doc.java.dir}" />

		<javadoc
			breakiterator="yes"
			classpathref="project.classpath"
			destdir="${doc.java.dir}"
			doctitle="Liferay ${lp.version} Internal API Documentation"
			header="&lt;b&gt;Liferay ${lp.version}&lt;/b&gt;"
			maxmemory="1024m"
			noqualifier="java.*"
			overview="portal-impl/src/portal-impl-overview.html"
			stylesheetfile="${project.dir}/tools/javadoc.css"
			useexternalfile="yes"
			use="yes"
			windowtitle="Liferay ${lp.version} Internal API"
		>
			<doclet name="com.liferay.tools.doclets.standard.Standard" path="${project.dir}/lib/development/liferay-doclet.jar">
				<param name="-linksource" />
			</doclet>

			<group title="Portal Services" packages="com.liferay.portal.service*" />
			<group title="Portlet Services" packages="com.liferay.portlet*" />

			<link href="http://docs.liferay.com/portlet-api/2.0/javadocs" />
			<link href="http://java.sun.com/javase/6/docs/api" />

			<packageset dir="portal-impl/src" />
			<packageset dir="portal-service/src" />
			<packageset dir="util-bridges/src" />
			<packageset dir="util-java/src" />
			<packageset dir="util-taglib/src" />

			<tag name="generated" description="ServiceBuilder generated this class. Modifications in this class will be overwritten the next time it is generated." />
		</javadoc>
	</target>

	<target name="prepare-generated-files">
		<ant antfile="build-dist.xml" target="prepare-generated-files" inheritAll="false" />
	</target>

	<target name="record-svn-revision">
		<exec dir="${project.dir}" executable="cmd.exe" os="${os.windows}" outputproperty="svn.info.output">
			<arg line="/c svn info" />
		</exec>

		<script classpathref="project.classpath" language="beanshell">
			String svnInfoOutput = project.getProperty("svn.info.output");

			int x = svnInfoOutput.indexOf("Revision: ");

			x = svnInfoOutput.indexOf(" ", x);

			int y = svnInfoOutput.indexOf("Node ", x);

			String revision = svnInfoOutput.substring(x, y).trim();

			project.setProperty("svn.info.revision", revision);
		</script>

		<echo file="svn-revision">Revision: ${svn.info.revision}
Username: ${svn.username}
Password: ${svn.password}
Portal: ${project.dir}
Plugins: ${lp.plugins.dir}</echo>
	</target>

	<target name="start">
		<antcall target="compile" />

		<ant dir="util-taglib" target="jar" inheritAll="false" />

		<ant dir="sql" target="build-db" inheritAll="false" />
		<ant dir="sql" target="rebuild-hypersonic" inheritAll="false" />

		<ant dir="portal-impl" target="build-themes" inheritAll="false" />

		<antcall target="jar" />

		<antcall target="print-current-time" />
	</target>

	<target name="test">
		<ant antfile="build-test.xml" target="test" inheritAll="false" />
	</target>

	<target name="test-integration">
		<ant antfile="build-test.xml" target="test-integration" inheritAll="false" />
	</target>

	<target name="test-unit">
		<ant antfile="build-test.xml" target="test-unit" inheritAll="false" />
	</target>

	<target name="update-plugins-to-svn-revision">
		<loadfile property="svn.info.revision.valid" srcfile="svn-revision" />

		<script classpathref="project.classpath" language="beanshell">
			String svnInfoOutput = project.getProperty("svn.info.revision.valid");

			int x = svnInfoOutput.indexOf("Revision: ");

			x = svnInfoOutput.indexOf(" ", x);

			int y = svnInfoOutput.indexOf("Username:", x);

			String revision = svnInfoOutput.substring(x, y).trim();

			project.setProperty("svn.info.revision", revision);
		</script>

		<exec dir="${lp.plugins.dir}" executable="cmd.exe" os="${os.windows}">
			<arg line="/c svn --username ${svn.username} --password ${svn.password} update -r ${svn.info.revision}" />
		</exec>
	</target>

	<target name="validate-xml">
		<xmlvalidate failonerror="no">
			<fileset dir="${project.dir}"
				excludes="**/dependencies/**,**/ehcache/**"
				includes="**/liferay-*.xml,**/web.xml"
			/>
			<xmlcatalog refid="dtd.catalog" />
		</xmlvalidate>
	</target>
</project>
