<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<!--
	Builds bundled and source distribution of Pluto, archiving it in zip, gzip and bzip2 files.
	Prerequisites for this build includes an installed Java 5+ JDK, Maven 2.0+ and Ant 1.6+.
	
	Run the binary build using the command line:
		ant -f dist-build.xml
				
	The bundled distribution is done in the following way:
	1. Downloads Tomcat from Apache and unzips it into a working directory. To this distribution, the
		build:
		a. Adds emptySessionPath="true" to server.xml.
		b. Adds a pluto and tomcat users with pluto and manager roles to tomcat-users.xml.
	2. Runs 'mvn install' and 'mvn pluto:install' to create the	Pluto driver and testsuite 
		and installs them into the Tomcat dist in the working directory.
	3. Archives the altered Tomcat dist with Pluto into a zip, gzip and bzip2 file.
	
	All built distributions end up in target/dist.
	
	After unarchiving the built distribution into a directory, invoke the startup script in the bin
	directory and browse to: http://localhost:8080/pluto/portal/
	
	Login as 'pluto' (password=pluto) to the portal. You will need to login again into the manager
	application linked at the bottom of the Pluto Admin page in order to upload portlet application
	war files to be able to deploy them to Pluto using the admin portlet. The 'tomcat' (password=tomcat)
	user can also be used to deploy portlets.					
-->
<project name="PlutoDistributions" default="bundle-dist">

    <property name="tomcat.major.version" value="7" description="Major version of Tomcat to deploy Pluto war files."/>
    <property name="tomcat.full.version" value="7.0.21" description="Full version of Tomcat to deploy Pluto war files."/>

    <xmlproperty file="pom.xml"/> <!-- picks up pluto build version from pom file -->
    <property name="pluto.version" value="${project.version}" description="Version of Pluto to build"/>

	<property name="dist.basedir" value="target/dist" description="Base working directory"/>
	<property name="base.name" value="apache-tomcat-${tomcat.full.version}"/>
	<property name="dist.dir" value="${dist.basedir}/${base.name}"/>
	<property name="pluto.name" value="pluto-${pluto.version}"/>
	<property name="pluto.dir" value="${dist.basedir}/${pluto.name}"/>
	<property name="unzip.file" value="${dist.basedir}/${base.name}.tar.gz"/>
	<property name="tar.file" value="${dist.basedir}/${base.name}.tar"/>

	<target name="bundle-dist" depends="prepare-bundle-dist,run-maven" description="Creates zip, gzip, and bzip2 distributions">
		<!-- Copy over jars needed to deploy custom portlets -->
		<copy file="pluto-util/target/pluto-util-${pluto.version}.jar" todir="${dist.dir}/PlutoDomain"/>			

		<!-- Zip everything up -->
		<zip destfile="${dist.basedir}/${pluto.name}-bundle.zip">
			<zipfileset prefix="${pluto.name}" dir="${dist.dir}" includes="**/*"/>
		</zip>

		<antcall target="tar-nocompress"/>
		
		<gzip src="${dist.basedir}/${pluto.name}-bundle.tar" 
			destfile="${dist.basedir}/${pluto.name}-bundle.tar.gz"/>
		
		<bzip2 src="${dist.basedir}/${pluto.name}-bundle.tar" 
			destfile="${dist.basedir}/${pluto.name}-bundle.tar.bz2"/>
				
		<antcall target="clean"/>	
	</target>
	
	<target name="tar-nocompress" description="Creates tar bundled distribution">
		<tar destfile="${dist.basedir}/${pluto.name}-bundle.tar">
			<tarfileset prefix="${pluto.name}" dir="${dist.dir}" mode="755" username="pluto" group="pluto">
		    	<include name="bin/*.sh"/>
			</tarfileset>
			<tarfileset prefix="${pluto.name}" dir="${dist.dir}" username="pluto" group="pluto">
				<include name="**/*"/>
				<exclude name="bin/*.sh"/>
			</tarfileset>
		</tar>		
	</target>
	
	<target name="prepare-bundle-dist">
		<exec executable="mvn" vmlauncher="false" dir="${basedir}" failonerror="true">
			<arg line="clean:clean"/>
		</exec>
		<mkdir dir="${dist.basedir}"/>

		<get src="http://archive.apache.org/dist/tomcat/tomcat-${tomcat.major.version}/v${tomcat.full.version}/bin/${base.name}.tar.gz"
		     dest="${unzip.file}"
		/>

		<gunzip src="${unzip.file}"
			dest="${dist.basedir}"
			description="Creates tar from tar.gz tomcat dist"
		/>

		<untar src="${tar.file}"
			dest="${dist.basedir}"
			description="Untars tomcat dist"
		/>		

		<!-- Copy over README -->
		<copy file="README" todir="${dist.dir}"/>

        <!-- Copy over additional licenses from pluto and testsuite web app-->
        <copy todir="${dist.dir}" flatten="true">
            <fileset dir="${basedir}/pluto-testsuite">
                <include name="src/main/webapp/META-INF/*-LICENSE"/>
            </fileset>
		</copy>
        <copy todir="${dist.dir}" flatten="true">
            <fileset dir="${basedir}/pluto-portal">
                <include name="src/main/webapp/META-INF/*-LICENSE"/>
            </fileset>
        </copy>

         <!-- Add sessionCookiePath="/" to root Context in context.xml
             (Tomcat 6.0.27+ replacement for emptySessionPath="true" attribute of Connector element in server.xml) -->
		<replace file="${dist.dir}/conf/context.xml"
			token="&lt;Context&gt;" 
			value="&lt;Context sessionCookiePath=&quot;/&quot;&gt;" 
			summary="true"
		/>
		
		<!-- Add 'pluto' and 'tomcat' users to tomcat-users.xml -->
		<replace file="${dist.dir}/conf/tomcat-users.xml"
			token="&lt;/tomcat-users&gt;" 
			value="&lt;user name=&quot;tomcat&quot; password=&quot;tomcat&quot; roles=&quot;tomcat,pluto,manager&quot; /&gt;${line.separator}&lt;user name=&quot;pluto&quot; password=&quot;pluto&quot; roles=&quot;pluto,manager&quot; /&gt;${line.separator}&lt;/tomcat-users&gt;" 
			summary="true"
		/>
	</target>

	<target name="run-maven" description="Runs maven2 goals">
		<exec executable="mvn" vmlauncher="false" dir="${basedir}" failonerror="true">
			<arg line="install"/>
		</exec>
		<exec executable="mvn" vmlauncher="false" dir="${basedir}" failonerror="true">
			<arg line="org.apache.portals.pluto:maven-pluto-plugin:${pluto.version}:install -DinstallDir=${basedir}/${dist.dir}"/>
		</exec>
	</target>
		
	<target name="clean">
		<delete dir="${dist.dir}"/>
		<delete file="${unzip.file}"/>	
		<delete file="${tar.file}"/>	
	</target>

</project>
