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

<project basedir="." default="help">

	<property file="build.properties" />

	<property name="dir.base" value="."/>
	<property name="dir.source" value="src"/>
	<property name="dir.schema" value="defs/schema"/>
	<property name="dir.build" value="build/ant" />
	<property name="dir.build.cvs" value="build/cvs"/>
	<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.lib" value="lib"/>
	<property name="dir.web" value="web"/>
	<property name="dir.web.lib" value="${dir.web}/WEB-INF/lib"/>
	
	<property name="compile.debug" value="true" />
	<property name="compile.deprecation" value="false" />
	<property name="compile.optimize" value="true" />
	<property name="compile.nowarn" value="true" />
	
	<property name="build.version" value="1.1.17"/>

	<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"/>
	
	<property name="env.src" value="example" />
	<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="cvs.root" value="/data/cvs"/>
	<property name="cvs.compressionlevel" value="6" />
	<property name="cvs.module.name" value="ieatdb"/>
	
	<path id="path.svn">
		<fileset dir="${dir.lib}/svn" includes="*.jar"/>
	</path>
	
	<taskdef resource="svntask.properties" classpathref="path.svn"/>
	<property name="svn.tag" value="1.0"/>
	<property name="svn.path" value="/tags/${svn.tag}"/>
	<property name="svn.url" 
		value="https://ieat.svn.sourceforge.net/svnroot/ieat/ieat${svn.path}"/>
	<property name="svn.username" value=""/>
	<property name="svn.password" value=""/>
	<property name="svn.revision" value="HEAD"/>
	<property name="svn.dir.build" value="${dir.build}/svn"/>
	<property name="svn.env.src" value="example"/>
	
	<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.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"/>
	</path>
	
	<target name="init">
		<mkdir dir="${dir.build}" />
		<mkdir dir="${dir.dist}" />
		<mkdir dir="${dir.generated}" />
	</target>

	<target name="check-has-env">
		<available file="${dir.env}" type="dir" property="has.env" />
	</target>
	
	<target name="compile.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.5"
				source="1.5"
				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>
		
		<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.5"
				source="1.5">
			<!--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" 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" description="Compile source files">
		<mkdir dir="${dir.build}/${app.name}/WEB-INF/classes"/>
		<javac 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.5"
				source="1.5">
			<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="help" description="Display build help">
		<echo>iEat v${build.version}</echo>
	</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="${svn.tag}" />
				<filter token="BUILD_DATE" value="${build.time}" />
			</filterset>
		</copy>
		
	</target>	

	<!-- 
		Subversion build targets 
		
		Build from SVN by first checking out the code using svn-checkout. Pass the tag
		to build with -Dsvn.tag=X, e.g.
		
		ant -Dsvn.tag=1.2 svn-checkout
		
		It helps to define the svn.dir.build environment property to a directory OUTSIDE
		the dir.build directory, so the checked-out sources are not deleted after every
		build. Then you can switch to a new release using the svn-switch task, e.g.
		
		ant -Dsvn.tag=1.3 svn-switch
		
		To perform a release, do
		
		ant -Dsvn.tag=X (svn-checkout or svn-switch) svn-src-release svn-bin-release svn-demo-release
		
	-->

	<target name="svn-checkout" description="Checkout sources from SVN">
		<delete dir="${svn.dir.build}"/>
		<mkdir dir="${svn.dir.build}"/>
		<svn username="${svn.username}" password="${svn.password}">
			<checkout url="${svn.url}" destPath="${svn.dir.build}"
				revision="${svn.revision}"/>
		</svn>
	</target>
	
	<target name="svn-update" description="Update sources previously checked out from SVN">
		<svn username="${svn.username}" password="${svn.password}">
			<update dir="${svn.dir.build}" revision="${svn.revision}"/>
		</svn>
	</target>
	
	<target name="svn-switch" 
		description="Switch sources previously checked out from SVN to another branch">
		<svn username="${svn.username}" password="${svn.password}">
			<switch path="${svn.dir.build}" url="${svn.url}" revision="${svn.revision}"/>
		</svn>
	</target>
	
	<!-- Copy environment properties (to catch local env) -->
	<target name="svn-prepare">
		<mkdir dir="${svn.dir.build}/${dir.env.src}"/>
		<copy todir="${svn.dir.build}/${dir.env.src}">
			<fileset dir="${dir.base}/${dir.env.src}" includes="**" excludes="README*"/>
		</copy>
	</target>

	<target name="svn-build" depends="clean,svn-prepare" description="Build from SVN tag versions">
		
		<property name="app.archive.name" value="${app.name}-${svn.tag}"/>
		
		<echo />
		<echo message="Building application version ${svn.tag} (${svn.revision})..."/>

		<ant dir="${svn.dir.build}" target="war" inheritall="true">
			<property name="build.version" value="${svn.tag}"/>
			<property name="dir.build" value="${dir.build}"/>
			<property name="app.name" value="${app.archive.name}"/>
			<property name="env.src" value="${svn.env.src}"/>
		</ant>
	</target>
	
	<target name="svn-demo-release" description="Create the Live Demo from SVN sources"
		depends="clean,init">
		
		<property name="app.archive.name" value="${app.name}-${svn.tag}"/>
		<property name="demo.build.dir" value="${app.archive.name}-livedemo"/>
		
		<!-- call the demo target on the code checked out from SVN -->
		<ant dir="${svn.dir.build}" inheritall="true" target="demo">
			<property name="dir.build" value="${dir.build}"/>
			<property name="build.version" value="${svn.tag}"/>
			<property name="env.src" value="demo"/>
		</ant>
		
		<!-- copy licenses -->
		<echo message="Copying licenses" />
		<mkdir dir="${dir.build}/${demo.build.dir}/licenses"/>
		<copy todir="${dir.build}/${demo.build.dir}/licenses" flatten="true">
			<fileset dir="${svn.dir.build}/defs/docs/licenses" includes="**/*.txt"/>
			<mapper type="glob" from="*.txt" to="*"/>
			<filterset>
				<filter token="VERSION" value="${svn.tag}" />
				<filter token="BUILD_DATE" value="${build.time}" />
			</filterset>
		</copy>
		
		<!-- copy demo build -->
		<echo message="Copying demo build into SVN tar structure"/>
		<copy todir="${dir.build}/${demo.build.dir}">
			<fileset dir="${dir.build}/${app.name}-demo" includes="**"/>
		</copy>
		
		<!-- fix file permissions -->
		<chmod perm="755">
			<fileset dir="${dir.build}/${demo.build.dir}">
				<include name="**/*.sh"/>
			</fileset>
		</chmod>
		
		<!-- tar it up -->
		<tar destfile="${dir.dist}/${app.archive.name}-livedemo.tbz2" 
				compression="bzip2" longfile="gnu">
			<tarfileset dir="${dir.build}" mode="775">
				<include name="${demo.build.dir}/**/*.sh"/>
			</tarfileset>
			<tarfileset dir="${dir.build}">
				<include name="${demo.build.dir}/**"/>
				<exclude name="${demo.build.dir}/**/*.sh"/>
			</tarfileset>
		</tar>
	</target>

	<target name="svn-src-release" 
		description="Build source release TAR from SVN tag versions">

		<property name="app.archive.name" 
			value="${app.name}-${svn.tag}"/>
		<property name="release-source-dir-name"
			value="${app.archive.name}-src"/>
		<property name="release-source-dir" 
			value="${dir.build}/${release-source-dir-name}" />
		<property name="release-source-name" 
			value="${dir.dist}/${app.archive.name}-src.tbz2" />

		<echo message="Building source release ${release-source-name}" />

		<mkdir dir="${dir.dist}"/>
		<delete dir="${release-source-dir}" />
		<delete file="${release-source-name}" />

		<echo message="Creating source README" />
		<copy file="${svn.dir.build}/defs/docs/release/README.src.txt" 
			tofile="${release-source-dir}/README" filtering="true">
			<filterset>
				<filter token="VERSION" value="${svn.tag}" />
				<filter token="BUILD_DATE" value="${build.time}" />
			</filterset>
		</copy>

		<echo message="Copying source to tar base directory" />
		<copy todir="${release-source-dir}">
			<fileset dir="${svn.dir.build}">
				<include name="**" />
				<include name=".*" />
				<exclude name="build/**" />
				<exclude name="dist/**" />
				<exclude name="${dir.env}/local/**" />
			</fileset>
		</copy>

		<tar tarfile="${release-source-name}" compression="bzip2" 
			longfile="gnu">
			<tarfileset dir="${dir.build}" 
				includes="${release-source-dir-name}/**" />
		</tar>
	</target>
	
	<!-- create binary release -->
	<target name="svn-bin-release" description="Build binary release TAR from SVN tag versions"
		depends="svn-build">
		
		<property name="release-dir-name"
			value="${app.archive.name}"/>
		<property name="release-dir" 
			value="${dir.build}/${release-dir-name}" />
		<property name="release-name" 
			value="${dir.dist}/${app.archive.name}.tbz2" />

		<echo message="Building binary release ${release-name}" />

		<delete dir="${release-dir}" />
		<delete file="${release-name}" />

		<echo message="Creating README" />
		<copy file="${svn.dir.build}/defs/docs/release/README.bin.txt" 
			tofile="${release-dir}/README" filtering="true">
			<filterset>
				<filter token="VERSION" value="${svn.tag}" />
				<filter token="BUILD_DATE" value="${build.time}" />
			</filterset>
		</copy>

		<echo message="Creating NOTES" />
		<copy filtering="true" todir="${release-dir}">
			<fileset dir="${svn.dir.build}/defs/docs/release/notes"
				includes="notes-${svn.tag}.txt"/>
			<mapper type="merge" to="NOTES"/>
			<filterset>
				<filter token="VERSION" value="${svn.tag}" />
				<filter token="BUILD_DATE" value="${build.time}" />
			</filterset>
		</copy>

		<!-- create setup scripts -->
		<echo message="Creating setup scripts" />
		<mkdir dir="${release-dir}/setup/sql"/>
		<copy filtering="false" todir="${release-dir}/setup/sql">
			<fileset dir="${svn.dir.build}/defs/sql"
				includes="**"/>
		</copy>
		
		<!-- copy JDBC drivers -->
		<echo message="Copying JDBC drivers" />
		<mkdir dir="${release-dir}/setup/lib"/>
		<copy todir="${release-dir}/setup/lib">
			<fileset dir="${svn.dir.build}/lib/jdbc"
				includes="**"/>
		</copy>

		<!-- copy JavaMail libraries -->
		<echo message="Copying Java Mail JARs" />
		<mkdir dir="${release-dir}/setup/lib/javamail"/>
		<copy todir="${release-dir}/setup/lib/javamail">
			<fileset dir="${svn.dir.build}/lib/javamail"
				includes="**"/>
		</copy>

		<!-- copy Xalan/Xerces libraries -->
		<echo message="Copying Xalan/Xerces JARs" />
		<mkdir dir="${release-dir}/setup/lib/xalan"/>
		<copy todir="${release-dir}/setup/lib/xalan">
			<fileset dir="${svn.dir.build}/lib/xalan"
				includes="**"/>
		</copy>

		<!-- copy misc README files -->
		<echo message="Copying README files" />
		<copy filtering="true" todir="${release-dir}">
			<fileset dir="${svn.dir.build}/defs/docs"
				includes="README-*.txt"/>
			<mapper type="glob" from="*.txt" to="*"/>
			<filterset>
				<filter token="VERSION" value="${svn.tag}" />
				<filter token="BUILD_DATE" value="${build.time}" />
			</filterset>
		</copy>
		
		<!-- copy licenses -->
		<echo message="Copying licenses" />
		<mkdir dir="${release-dir}/licenses"/>
		<copy todir="${release-dir}/licenses" flatten="true">
			<fileset dir="${svn.dir.build}/defs/docs/licenses" includes="**/*.txt"/>
			<mapper type="glob" from="*.txt" to="*"/>
			<filterset>
				<filter token="VERSION" value="${svn.tag}" />
				<filter token="BUILD_DATE" value="${build.time}" />
			</filterset>
		</copy>
		
		<!-- copy war, removing version from name -->
		<copy file="${dir.build}/${app.archive.name}.war" 
			tofile="${release-dir}/${app.name}.war"/>

		<!-- tar it alll up -->
		<mkdir dir="${dir.dist}"/>
		<tar tarfile="${release-name}" compression="bzip2" longfile="gnu">
			<tarfileset dir="${dir.build}" 
				includes="${release-dir-name}/**">
			</tarfileset>
		</tar>

	</target>
	
</project>
