<!--
   The contents of this file are subject to the Mozilla Public License
   Version 1.1 (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.mozilla.org/MPL/

   Software distributed under the License is distributed on an "AS IS"
   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
   License for the specific language governing rights and limitations
   under the License.

   The Original Code is anduril.nplus1.net code.
      
   The Initial Developer of the Original Code is n + 1, Inc.  
   Portions created by n + 1, Inc. are Copyright (C) 2003. 
   All Rights Reserved.
      
   Contributor(s): Brian Schenkenfelder and Craig McDaniel.
-->

<project name="anduril" default="build" basedir=".">

	<!-- import property file -->
	<property file="./conf/build.properties"/>

	<!-- set path info -->
	<path id="jboss.server.classpath">
		<fileset dir="${jboss.server.lib}">
			<include name="**/*.jar"/>
		</fileset>
	</path>
	<path id="jboss.client.classpath">
		<fileset dir="${jboss.client.lib}">
			<include name="**/*.jar"/>
		</fileset>
	</path>
	<path id="local.libs">
		<fileset dir="${base.lib}">
			<include name="**/*.jar"/>
		</fileset>
	</path>


	<target name="build">
		<echo>System Build Commands</echo>
		<echo>build-ejb       - builds the enterprise java beans</echo>
		<echo>build-web       - builds the web servlets</echo>
		<echo>package-ejb     - packages the enterpise java beans</echo>
		<echo>package-web     - packages the web into a war</echo>
		<echo>package-anduril - packages the application ear</echo>
		<echo>package-src     - creates tar ball of source code</echo>
		<echo>deploy          - deployes the application</echo>
		<echo>deploy-anduril  - deployes the application without building</echo>
		<echo>remove          - removes the application</echo>
		<echo> </echo>
		<echo>Database Commands</echo>
		<echo>build-db        - builds database</echo>
		<echo>test-data-db    - puts test data into database</echo>
		<echo>clean-db        - drops database tables</echo>
		<echo>deploy-db       - deploys the postgres xml file</echo>
		<echo> </echo>
		<echo>Test Commands</echo>
		<echo>build-tests     - build junit tests</echo>
		<echo>run-tests       - runs junit tests</echo>
		<echo> </echo>
		<echo>Misc Commands</echo>
		<echo>clean           - cleans the build directories</echo>
		<echo>docs            - build javadocs</echo>
	</target>

	<target name="build-ejb">
		<mkdir dir="${ejb.build}"/>
		<javac srcdir="${ejb.src}" destdir="${ejb.build}" compiler="javac1.4">
			<classpath>
				<path refid="jboss.server.classpath"/>
				<path refid="local.libs"/>
			</classpath>
		</javac>
	</target>

	<target name="build-web" depends="package-ejb">
	<mkdir dir="${web.build}"/>
	<javac srcdir="${web.src}" destdir="${web.build}" compiler="javac1.4">
			<classpath>
				<path refid="jboss.server.classpath"/>
				<path refid="jboss.client.classpath"/>
				<path refid="local.libs"/>
			</classpath>
		</javac>
	</target>

	<target name="package-ejb" depends="build-ejb">
		<mkdir dir="${base.dist}"/>
		<jar jarfile="${base.dist}/anduril-server.jar">
			<fileset dir="${ejb.build}"/>
			<fileset dir="${ejb.src}">
				<include name="**META-INF/**"/>
			</fileset>
			<fileset dir="${base.conf}">
				<include name="localhomefactory.properties"/>
			</fileset>
			<fileset dir="${base.lib}">
				<include name="postgresql.jar"/>
			</fileset>
		</jar>
		<jar jarfile="${base.lib}/anduril-client.jar">
			<fileset dir="${ejb.build}">
				<include name="**/common/**"/>
			</fileset>
			<fileset dir="${base.conf}">
				<include name="homefactory.properties"/>
			</fileset>
		</jar>
	</target>

	<target name="package-web" depends="build-web">
		<mkdir dir="${base.dist}"/>
		<war destfile="${base.dist}/anduril-web.war" webxml="${web.conf}/web.xml">
			<classes dir="${web.build}"/>
			<lib dir="${base.lib}"/>
			<webinf dir="${web.conf}">
				<exclude name="web.xml"/>
			</webinf>
			<zipfileset dir="${web.base}/velocity" prefix="velocity"/>
			<zipfileset dir="${web.base}/static"/>
		</war>
	</target>

	<target name="package-anduril" depends="package-web">
		<ear destfile="${base.dist}/anduril.ear" appxml="${base.conf}/application.xml">
			<fileset dir="${base.dist}">
				<include name="anduril-server.jar"/>
				<include name="anduril-web.war"/>
			</fileset>
		</ear>
	</target>

	<target name="package-src" depends="package-anduril, docs">
		<mkdir dir="${base.dist}"/>
		<tar destfile="${base.dist}/anduril.tar">
			<tarfileset dir="${basedir}" prefix="anduril">
				<include name="build.xml"/>
				<include name="README.txt"/>
				<include name="LICENSE"/>
				<include name="LEGAL"/>
			</tarfileset>
			<tarfileset dir="${base.src}" prefix="anduril/src">
				<exclude name="**/CVS"/>
			</tarfileset>
			<tarfileset dir="${base.doc}" prefix="anduril/doc">
				<exclude name="**/CVS"/>
			</tarfileset>
			<tarfileset dir="${base.dist}" prefix="anduril/dist">
				<exclude name="**/CVS"/>
			</tarfileset>
			<tarfileset dir="${base.conf}" prefix="anduril/conf">
				<exclude name="**/CVS"/>
			</tarfileset>
			<tarfileset dir="${base.lib}" prefix="anduril/lib">
				<exclude name="**/CVS"/>
			</tarfileset>
		</tar>
		<gzip zipfile="${base.dist}/anduril.tar.gz" src="${base.dist}/anduril.tar"/>
		<delete>
			<fileset dir="${base.dist}">
				<include name="anduril.tar"/>
			</fileset>
		</delete>
	</target>


	<target name="deploy" depends="package-anduril">
		<copy todir="${jboss.deploy}" file="${base.dist}/anduril.ear"/>
	</target>

	<target name="deploy-anduril">
		<copy todir="${jboss.deploy}" file="${base.dist}/anduril.ear"/>
	</target>

	<target name="deploy-db">
		<copy todir="${jboss.deploy}" file="${base.conf}/postgres-anduril-service.xml"/>
	</target>

	<target name="remove">
		<delete file="${jboss.deploy}/anduril.ear"/>
	</target>

	<target name="build-db">
		<exec dir="${sql.src}" executable="/usr/bin/psql">
			<arg line="-U ${sql.person} -f anduril-schema.sql anduril"/>
		</exec>
	</target>

	<target name="test-data-db">
		<exec dir="${sql.src}" executable="/usr/bin/psql">
			<arg line="-U ${sql.person} -f anduril-test-data.sql anduril"/>
		</exec>
	</target>

	<target name="clean-db">
		<exec dir="${sql.src}" executable="/usr/bin/psql">
			<arg line="-f anduril-clean.sql anduril"/>
			<arg line="-U ${sql.person} -f anduril-clean.sql anduril"/>
		</exec>
	</target>

	<target name="build-tests" depends="package-ejb">
		<mkdir dir="${test.build}"/>
		<javac srcdir="${test.src}" destdir="${test.build}" compiler="javac1.4">
			<classpath>
				<path refid="jboss.client.classpath"/>
				<path refid="local.libs"/>
			</classpath>
		</javac>
	</target>

	<target name="run-tests" depends="build-tests">
		<mkdir dir="${test.reports}"/>
		<junit printsummary="yes">
			<formatter type="plain"/>
			<batchtest todir="${test.reports}">
				<fileset dir="${test.build}">
					<include name="**/*"/>
				</fileset>
			</batchtest>
			<classpath>
				<path refid="jboss.client.classpath"/>
				<path refid="local.libs"/>
				<pathelement path="${test.build}"/>
			</classpath>
		</junit>
	</target>


	<target name="clean">
		<delete dir="${base.build}"/>
		<delete dir="${base.dist}"/>
		<delete dir="${test.reports}"/>
		<delete>
			<fileset dir="${base.lib}">
				<include name="anduril-client.jar"/>
			</fileset>
		</delete>
	</target>

	<target name="docs">
		<!-- Compile the javadoc in ${src} into ${doc} -->
		<mkdir dir="${base.doc}"/>
		<javadoc packagenames="net.nplus1.scart.*" destdir="${base.doc}">
			<fileset dir="${ejb.src}"/>
			<fileset dir="${web.src}"/>
			<fileset dir="${test.src}"/>
			<classpath>
				<path refid="jboss.server.classpath"/>
				<path refid="local.libs"/>
			</classpath>
		</javadoc>
	</target> 

</project>
