<?xml version="1.0" encoding="UTF-8"?>

<project basedir="." default="war" xmlns:ivy="antlib:org.apache.ivy.ant">

	<property file="build.properties" />
	<property name="build.version" value="1.1.17"/>

	<property name="dir.base" value="."/>
	<property name="dir.source" value="src"/>
	<property name="dir.schema" value="defs/schema"/>
	<property name="dir.build" value="build/" />
	<property name="hibernate.properties" value="${dir.schema}/hibernate.properties"/>
	<property name="dir.generated" value="${dir.build}/generated/src" />
	<property name="dir.compile.dest.domain" value="${dir.build}/generated/classes"/>
	<property name="dir.hibernate.mapping" value="defs/schema/hbm"/>
	<property name="dir.database" value="defs/sql"/>
	<property name="dir.dist" value="dist"/>
	<property name="dir.deps" value="lib"/>
	<property name="dir.lib" value="lib"/>
	<property name="dir.web" value="web"/>
	<property name="dir.web.lib" value="${dir.web}/WEB-INF/lib"/>
	
	<property name="env.src" value="development" />
	<property name="dir.env" value="environment"/>
	<property name="dir.env.src" value="${dir.env}/${env.src}"/>
	<property name="dir.env.src.defaults" value="${dir.env}/example"/>

    <property name="compile.debug" value="true" />
	<property name="compile.deprecation" value="true" />
	<property name="compile.optimize" value="true" />
	<property name="compile.nowarn" value="true" />
	<property name="compile.nowarn" value="true" />

	<property name="app.name" value="ieat" />
	<property name="app.path" value="/${app.name}" />
	<property name="app.home" value="/data/projects/ieat/web"/>
	<property name="app.home.backup" value="${app.home}.bak"/>
	
    <available file="${user.home}/.ant/lib/ivy.jar" property="ivy.installed"/>
    
	<tstamp>
		<format property="build.time" pattern="MM/dd/yyyy hh:mm aa z" />
	</tstamp>

	<property name="version.properties" value="version.properties" />
	<property name="version.txt.name" value="version.txt" />
		
	<!-- HyperJAXB 2 properties -->
	<property name="hjb.dir" value="hyperjaxb2-build"/>
	<property name="hjb.db.type" value="postgres"/>
	
	<path id="jaxb.lib.path">
		<fileset dir="${dir.web.lib}" includes="*.jar"/>
		<fileset dir="${dir.deps}" includes="*.jar"/>
		<fileset dir="${dir.lib}" includes="**/*.jar"/>
	</path>
  
	<path id="hibernatedoclet.lib.path">
		<fileset dir="${dir.web.lib}" includes="*.jar"/>
		<fileset dir="${dir.lib}" includes="**/*.jar"/>
	</path>
	
	<path id="schemaexport.lib.path">
		<path refid="compile.path"/>
	</path>
	
	<path id="schemaexport.path">
		<path refid="compile.path"/>
	</path>
	
	<path id="compile.path">
		<pathelement location="${dir.build}/${app.name}/WEB-INF/classes"/>
		<fileset dir="${dir.web.lib}" includes="*.jar"/>
		<fileset dir="${dir.lib}" includes="**/*.jar"/>
		<fileset dir="${dir.deps}" includes="*.jar"/>
	</path>
	<target name="help" description="Display build help">
		<echo>iEat v${build.version}</echo>
		<echo>To see list of targets, use ant -p</echo>
	</target>

	<target name="init">
		<mkdir dir="${dir.build}" />
		<mkdir dir="${dir.dist}" />
		<mkdir dir="${dir.generated}" />
	</target>

    <target name="ivy" unless="ivy.installed">
      <mkdir dir="${user.home}/.ant/lib"/>
      <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar"/>
      <fail message="Ivy has been installed. Run the build again"/>
    </target>

    <target name="resolve-dependencies" depends="ivy" description="Use ivy to resolve dependencies">
      <ivy:cachepath pathid="dir.deps">
        <dependency org="org.springframework" name="spring-web" rev="1.2.8" />
        <dependency org="org.springframework" name="spring-mock" rev="1.2.8"/>
        <dependency org="jmock" name="jmock" rev="1.0.1"/>
        <dependency org="com.sun.xml.bind" name="jaxb-xjc" rev="1.0.6"/>
        <dependency org="org.apache.lucene" name="lucene-core" rev="2.2.0"/>
        <dependency org="concurrent" name="concurrent" rev="1.3.4"/>
        <dependency org="org.apache.derby" name="derbytools" rev="10.1.3.1"/>
        <dependency org="org.apache.derby" name="derby" rev="10.1.3.1"/>
        <!-- Outdated dependancies -->
        <dependency org="javax.faces" name="jsf-api" rev="1.1_02"/>
        <dependency org="javax.transaction" name="jta" rev="1.1"/>
      </ivy:cachepath>
    </target>
		
	<target name="compile.domain" depends="resolve-dependencies,generate.domain" description="Compile the JAXB classes">
		<delete dir="${dir.compile.dest.domain}" failonerror="no"/>
		<mkdir dir="${dir.compile.dest.domain}"/>
		<javac srcdir="${dir.generated}"
				destdir="${dir.compile.dest.domain}"
				debug="${compile.debug}" 
				deprecation="${compile.deprecation}" 
				optimize="${compile.optimize}" 
				nowarn="${compile.nowarn}"
				target="1.6"
				source="1.6"
				fork="true"
				memoryinitialsize="256m"
				memorymaximumsize="400m"
				>
			<classpath refid="compile.path" />
		</javac>
	</target>
	
	<!-- HyperJAXB2 targets -->
	
	<target name="generate.domain" depends="init" 
			description="Generate JAXB domain objects from XML schemas">
		<delete failonerror="no">
			<fileset dir="${hjb.dir}/src/main/resources">
				<include name="*.xjb"/>
				<include name="*.xsd"/>
			</fileset>
		</delete>
		<copy todir="${hjb.dir}/src/main/resources">
			<fileset dir="defs/schema">
				<include name="core.xsd"/>
				<include name="ui.xsd"/>
			</fileset>
		</copy>
		<copy todir="${hjb.dir}/src/main/resources">
			<fileset dir="defs/schema">
				<include name="binding.xml"/>
			</fileset>
			<mapper type="glob" from="*.xml" to="*.xjb"/>
		</copy>
		<delete>
			<fileset dir="${hjb.dir}/src/main/java">
				<include name="**"/>
			</fileset>
		</delete>
		<delete failonerror="no">
			<fileset dir="${hjb.dir}/target/generated-sources/xjc">
				<include name="magoffin/matt/ieat/domain/**"/>
			</fileset>
		</delete>
		
        <mkdir dir="${hjb.dir}/src/main/java"/>
		<javac srcdir="${hjb.dir}/src/main/java"
				destdir="${hjb.dir}/src/main/java"
				debug="${compile.debug}" 
				deprecation="${compile.deprecation}" 
				optimize="${compile.optimize}" 
				nowarn="${compile.nowarn}"
				target="1.6"
				source="1.6">
			<!--classpath refid="unit-test.path" /-->
		</javac>				
		<ant antfile="build.xml" inheritall="false" target="generate-sources" dir="${hjb.dir}">
			<property name="hyperjaxb2.export.schema.properties.file" 
				value="${basedir}/${dir.database}/${hjb.db.type}/hibernate.properties"/>
		</ant>
		<delete>
			<fileset dir="${dir.generated}">
				<include name="magoffin/matt/ieat/domain/*.*"/>
				<include name="magoffin/matt/ieat/domain/impl/**"/>
			</fileset>
		</delete>
		<copy todir="${dir.generated}">
			<fileset dir="${hjb.dir}/target/generated-sources/xjc">
				<include name="magoffin/matt/ieat/domain/**"/>
			</fileset>
		</copy>
		<!-- TODO copy tofile="${dir.database}/create-tables.${hjb.db.type}.sql"
			file="${hjb.dir}/sql/schema.sql"/-->
	</target>
	
	<!--
		Generate database DDL for entity tables. Pass the hjb.db.type parameter
		to change the exported DDL SQL syntax using one of the sub-directory names
		within defs/sql, e.g.
		
		ant -Dhjb.db.type=mysql export.database.schema
		
		Will use the defs/sql/mysql/hibernate.properties environment properties
		and generate the defs/sql/mysql/create.sql DDL script.
	-->
	<target name="export.database.schema" depends="compile" 
		description="Generate RDBMS creation scripts via Hibernate">

		<taskdef name="schemaexport"
			classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
			classpathref="schemaexport.path"/>

		<mkdir dir="${dir.database}"/>
		
		<delete dir="${dir.build}/schema-export" failonerror="no"/>
		<mkdir dir="${dir.build}/schema-export"/>
		<unjar src="${dir.web.lib}/${app.name}-xml.jar" 
			dest="${dir.build}/schema-export"/>

		<schemaexport
			properties="${dir.database}/${hjb.db.type}/hibernate.properties"
			quiet="no"
			text="yes"
			drop="no"
			delimiter=";"
			output="${dir.database}/${hjb.db.type}/create.sql">
			<fileset dir="${dir.build}/schema-export">
				<include name="**/*.hbm.xml"/>
			</fileset>
		</schemaexport>

	</target>

	<target name="jar.domain" depends="generate.domain,compile.domain" description="Create source and binary JAR out of the JAXB classes">
		<!-- Generate the binary JAR with source attached -->
		<jar destfile="${dir.web.lib}/${app.name}-xml.jar">
			<fileset dir="${dir.compile.dest.domain}">
				<include name="magoffin/matt/ieat/domain/**"/>
			</fileset>
			<fileset dir="${dir.generated}">
				<include name="magoffin/matt/ieat/domain/**"/>
			</fileset>
		</jar>
	</target>
	
	<target name="generate-xml" description="Generate new JAXB jar from scratch"
		depends="generate.domain,compile.domain,jar.domain"/>

	<target name="prepare-war">

		<!-- Create build directories as needed -->
		<mkdir dir="${dir.build}/${app.name}" />

		<!-- Copy static content of this web application -->
		<copy todir="${dir.build}/${app.name}">
			<fileset dir="${dir.base}/${dir.web}">
				<exclude name="WEB-INF/web.xml"/>
				<exclude name="WEB-INF/classes/**"/>
			</fileset>
		</copy>

		<!-- Copy environment properties -->
		<copy todir="${dir.build}/${app.name}/WEB-INF/classes">
			<fileset dir="${dir.base}/environment/${env.src}"
				excludes="README.txt"/>
		</copy>
		
		<!-- Copy class resources -->
		<copy todir="${dir.build}/${app.name}/WEB-INF/classes">
			<fileset dir="${dir.source}" excludes="**/*.java"/>
		</copy>
		
	</target>

	<target name="compile" depends="resolve-dependencies" description="Compile source files">
		<mkdir dir="${dir.build}/${app.name}/WEB-INF/classes"/>
		<javac verbose="false" srcdir="${dir.source}"
				destdir="${dir.build}/${app.name}/WEB-INF/classes"
				debug="${compile.debug}" 
				deprecation="${compile.deprecation}" 
				optimize="${compile.optimize}" 
				nowarn="${compile.nowarn}"
				target="1.6"
				source="1.6">
			<classpath refid="compile.path" />
		</javac>
	</target>
	
	<target name="war" depends="prepare-war,compile" description="Create application WAR">
		<mkdir dir="${dir.build}"/>
		<war warfile="${dir.build}/${app.name}.war" 
			basedir="${dir.build}/${app.name}" 
			webxml="${dir.base}/${dir.web}/WEB-INF/web.xml" />
	</target>
  
	<target name="clean" description="Delete build files">
		<delete dir="${dir.generated}" />
		<delete dir="${dir.build}" />
	</target>
	
	<target name="demo" description="Create the Live Demo">
		
		<antcall target="prepare-war" inheritall="true">
			<param name="env.src" value="demo"/>
		</antcall>
		
		<antcall target="compile" inheritall="true"/>
		
		<mkdir dir="${dir.build}/${app.name}-demo"/>
		<copy todir="${dir.build}/${app.name}-demo">
			<fileset dir="${dir.base}/demo">
				<include name="jakarta-tomcat/**"/>
				<include name="*.sh"/>
				<include name="*.bat"/>
			</fileset>
		</copy>
		
		<!-- Expand database zip -->
		<unzip dest="${dir.build}/${app.name}-demo" src="${dir.base}/demo/db.zip"/>
		
		<!-- fix file permissions -->
		<chmod perm="755">
			<fileset dir="${dir.build}/${app.name}-demo">
				<include name="**/*.sh"/>
			</fileset>
		</chmod>
		
		<!-- create required empty directories -->
		<mkdir dir="${dir.build}/${app.name}-demo/jakarta-tomcat/logs"/>
		<mkdir dir="${dir.build}/${app.name}-demo/jakarta-tomcat/work"/>
		<mkdir dir="${dir.build}/${app.name}-demo/jakarta-tomcat/temp"/>
		
		<!-- copy Xalan to endorsed dir-->
		<mkdir dir="${dir.build}/${app.name}-demo/jakarta-tomcat/common/endorsed"/>
		<copy todir="${dir.build}/${app.name}-demo/jakarta-tomcat/common/endorsed">
			<fileset dir="${dir.base}/lib/xalan" includes="*.jar"/>
		</copy>
		
		<!-- add JavaMail, Derby, etc JARs -->
		<copy todir="${dir.build}/${app.name}-demo/jakarta-tomcat/common/lib">
			<fileset dir="${dir.base}/lib/javamail" includes="*.jar"/>
			<fileset dir="${dir.base}/lib/derby" includes="*.jar"/>
		</copy>
		
		<!-- copy app webapp -->
		<copy todir="${dir.build}/${app.name}-demo/jakarta-tomcat/webapps">
			<fileset dir="${dir.build}" includes="${app.name}/**"
				excludes="${app.name}/WEB-INF/classes/ieat.hbm.xml"/>
		</copy>
		<copy todir="${dir.build}/${app.name}-demo/jakarta-tomcat/webapps/${app.name}/WEB-INF">
			<fileset dir="${dir.base}/${dir.web}/WEB-INF" includes="web.xml"/>
		</copy>
		<copy todir="${dir.build}/${app.name}-demo/jakarta-tomcat/webapps/${app.name}/WEB-INF/classes">
			<fileset dir="${dir.base}/defs/sql/derby" includes="ieat.hbm.xml"/>
		</copy>
		
		<!-- copy readme -->
		<copy filtering="true" todir="${dir.build}/${app.name}-demo">
			<fileset dir="${dir.base}/defs/docs/demo"
				includes="README*.txt"/>
			<mapper type="glob" from="*.txt" to="*"/>
			<filterset>
				<filter token="VERSION" value="${build.version}" />
				<filter token="BUILD_DATE" value="${build.time}" />
			</filterset>
		</copy>
		
	</target>	
</project>
