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

<project default="all" name="jdiffchaser-conf">

	<property file="jdiffchaser.properties"/>
	
	<!-- Loading environment variables using env. prefix -->
	<property environment="env"/>

    <description>
        Generation and Compilation of castor files for an INCH distribution.
        The resulting jar file is : jdiffchaser-conf.jar
    </description> 

	<!-- project sources, used to compile CastorObject base class -->
    <property name="src" location="src"/>

	<!-- Source generation and compiling variables -->
    <property name="conf" 			value="conf"/>
    <property name="schema.dir"    value="schema"/>
    <property name="elements.root"  value="org.jdiffchaser"/>

	<!-- Source generation and compiling constants -->
    <property name="castor.sources" value="${schema.dir}/temp_src"/>
    <property name="castor.classes" value="${schema.dir}/temp_classes"/>
    <property name="castor.archive" value="dist/${ant.project.name}-${version}.jar"/>

	<!-- Source generation and compiling classpath -->
    <property name="compiler.home" value="${env.JAVA_HOME}"/>
	<path id="castor.lib">
        <pathelement location="lib/castor-0.9.5.4-xml.jar"/> 
        <pathelement location="lib/xerces-2.0.2.jar"/>                
	</path>

    <!-- Rebuilds the whole project -->
    <target name="all" depends="checkUpToDate,release" description="Generates and compiles the whole castor schemas"/>
    
    <target name="clean" description="cleans all output from this castor build">
    	<delete file="${castor.archive}"/>
    	<delete dir="${castor.classes}"/>
    	<delete dir="${castor.sources}"/>
    </target>
    
    <target name="checkUpToDate">
		<uptodate property="UpToDate" targetfile="${castor.archive}">
			<srcfiles dir="${schema.dir}" includes="*"/>
        	<srcfiles dir="${conf}" includes="*.properties"/>
        	<srcfiles dir="." includes="castorbuild.xml"/>
		</uptodate>
    </target>
    
    <target name="release" unless="UpToDate">
    	<antcall target="build"/>
    	<mkdir dir="dist"/>
    	<delete file="dist/${ant.project.name}.jar" quiet="true"/>
		<tstamp>
			<format locale="en" pattern="yyDDD-1" property="build.time" unit="millisecond"/>
		</tstamp>
		<echo message="Building jar ${castor.archive} using buildtime ${build.time}"/>
		<jar compress="true" jarfile="${castor.archive}" index="true">
		    <fileset dir="${castor.classes}">
		        <include name="**/*"/>
		    </fileset>
			<manifest>
				<attribute name="Built-By" value="${user.name}"/>
				<section name="org/jdiffchaser/">
					<attribute name="Component" value="Castor elements"/>
					<attribute name="Specification-Title" value="Castor elements"/>
					<attribute name="Specification-Vendor" value="jdiffchaser team"/>
					<attribute name="Implementation-Title" value="org.jdiffchaser"/>
					<attribute name="Implementation-Version" value="Build ${build.time}"/>
					<attribute name="Implementation-Vendor" value="jdiffchaser team"/>
				</section>
			</manifest>
		</jar>
    	<delete dir="${castor.classes}" failonerror="false"/>
    </target>

    <target name="build" depends="generate">
    	<delete dir="${castor.classes}"/>
    	<mkdir dir="${castor.classes}"/>
    	
        <javac fork="yes" executable="${compiler.home}/bin/javac" memoryInitialSize="128m" memoryMaximumSize="512m" debug="true" deprecation="no" destdir="${castor.classes}" srcdir="${src}" verbose="no" target="${java.target}" source="${java.source}">
            <classpath refid="castor.lib"/>
            <include name="**/CastorObject.java"/>
        </javac>
        <javac fork="yes" executable="${compiler.home}/bin/javac" memoryInitialSize="128m" memoryMaximumSize="512m" debug="true" deprecation="no" destdir="${castor.classes}" srcdir="${castor.sources}" verbose="no" target="${java.target}" source="${java.source}">
            <classpath refid="castor.lib"/>
        </javac>
    </target>
    
    
    <target name="generate">
    	<delete dir="${castor.sources}"/>
    	<mkdir dir="${castor.sources}"/>

        <buildcastor schema="conf"/>
    </target>


	<macrodef name="buildcastor">
		<attribute name="schema" description="contains the name of the schema ( without .xsd )"/>
		<attribute name="castor.package" description="contains the name of the castor package" default="@{schema}"/>
		<attribute name="elements.root" description="castor package prefix to use for generation" default="${elements.root}"/>
		<sequential>
			<echo message="Generating castor sources for schema @{schema}.xsd , using package @{elements.root}.@{castor.package}"/>
			<antcall target="check_schema">
				<param name="schema.file" value="${schema.dir}/@{schema}.xsd"/>
			</antcall>
	        <echo message="lib/castor-0.9.5.4-xml.jar"/>
	        <java classname="org.exolab.castor.builder.SourceGenerator" fork="yes" jvm="${compiler.home}/bin/java" failonerror="true">
	            <jvmarg value="-Xmx512M"/>
	            <arg line="-f -i ${schema.dir}/@{schema}.xsd -dest ${castor.sources} -package @{elements.root}.@{castor.package}"/>
	            <classpath>
	                <pathelement location="${conf}"/>
	                <path refid="castor.lib"/>                
	            </classpath>
	        </java>
		</sequential>
	</macrodef>
	
	<target name="check_schema">
		<available property="schema.available" file="${schema.file}" type="file"/>
		<fail message="Schema file ${schema.file} does not exists." unless="schema.available"/>
	</target>

</project>