<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="antlib:org.apache.tools.ant"
	 name="snow-master" default="help" basedir=".">
    <description>Compiling and packaging Snow</description>

    <property file="build.properties"/>

    <property name="build.dir" 
	      value="${basedir}/build"/>
    <property name="build.classes.dir" 
	      value="${build.dir}/bin"/>
    <property name="src.dir" 
	      value="${basedir}/src"/>
    <property name="lib.dir" 
	      value="${basedir}/lib"/>
    <property name="dist.dir" 
	      value="${basedir}/dist"/>
    <property name="snow.jar.path"
	      value="${dist.dir}/snow.jar"/>
    <property name="snow.ext.dir"
	      value="${basedir}/ext"/>
	
    <target name="help">
      <echo>Main Ant targets:
 snow.compile  
   -- compile SNOW to ${build.classes.dir}.
 snow.jar      
   -- create packaged ${snow.jar.path}.
 snow.source.zip snow.source.tar
   -- create source distributions in ${dist.dir}.
 snow.clean 
   -- remove SNOW intermediate files
 snow.test
   -- run SNOW's JUnit tests</echo>
    </target>

    <patternset id="snow.source.java">
      <include name="**/*.java"/>
    </patternset>

    <patternset id="snow.source.lisp">
      <include name="snow/**/*.lisp"/>
      <include name="snow/**/*.asd"/>
    </patternset>

    <patternset id="snow.objects">
      <include name="snow/**/*.class"/>
      <include name="snow/**/*.cls"/> 
      <include name="snow/**/*.abcl"/>
      <patternset refid="snow.source.lisp"/>
    </patternset>
    
    <path id="snow.classpath.dist">
      <pathelement location="${snow.jar.path}"/>
    </path>
    
    <path id="snow.classpath.build">
      <pathelement location="${build.classes.dir}"/>
      <fileset dir="${lib.dir}">
        <include name="**/*.jar"/>
      </fileset>
    </path>

    <target name="snow.compile" depends="snow.compile.lisp">
      <echo>Compiled SNOW with Java version: ${java.version}</echo>
    </target>

    <target name="snow.init">
      <tstamp>
	<format property="build" pattern="EEE MMM dd yyyy HH:mm:ss zzz"/>
      </tstamp>

      <tstamp>
	<format property="build.stamp" pattern="yyyymmdd-HHmm"/>
      </tstamp>

      <!--- antversion fails in ant 1.7.1 <antversion property="ant.version" 
	                                              atleast="1.7"/> -->
      <property name="java.path"
		value="${java.home}/bin/java"/>
    </target>

    <target name="snow.compile.java" 
	    depends="snow.init">
      <mkdir dir="${build.dir}"/>
      <mkdir dir="${build.classes.dir}"/>
      <javac destdir="${build.classes.dir}"
	     classpathref="snow.classpath.build"
	     debug="true"
	     target="1.6"
	     failonerror="true">
	<src path="${src.dir}/java"/>
	<patternset refid="snow.source.java"/>
      </javac>
      <echo message="${build}" 
	    file="${build.classes.dir}/snow/build"/>
    </target>

    <target name="snow.copy.lisp">
      <copy todir="${build.classes.dir}" preservelastmodified="yes">
	<fileset dir="${src.dir}/lisp">
	  <patternset refid="snow.source.lisp"/>
	</fileset>
	<fileset dir="${lib.dir}">
	  <patternset>
	    <include name="**/*" />
	    <exclude name="**/*.jar"/>
	  </patternset>
	</fileset>
      </copy>
    </target>

    <target name="snow.compile.lisp" 
	    depends="snow.copy.lisp,snow.compile.java">
      <java classpathref="snow.classpath.build"
	    fork="true"
	    failonerror="true"
	    classname="org.armedbear.lisp.Main"
	    dir="${build.classes.dir}">
	<arg value="--noinit"/>
	<arg value="--load"/>
	<arg value="snow/compile-system.lisp"/>
      </java>
    </target>

    <target name="snow.jar.uptodate" depends="snow.compile">
      <uptodate property="snow.jar.uptodate.p" targetfile="${snow.jar.path}">
	<srcfiles dir="${build.classes.dir}">
	  <patternset refid="snow.objects"/>
	</srcfiles>
      </uptodate>
    </target>

    <target name="snow.jar" depends="snow.jar.uptodate"
	    unless="snow.jar.uptodate.p">
      <mkdir dir="${dist.dir}"/>
      <jar destfile="${snow.jar.path}"
	   compress="true"
	   basedir="${build.classes.dir}">
	<manifest>
	  <attribute name="Main-Class" value="snow.Snow"/>
	</manifest>
      </jar>
    </target>
    
    <target name="snow.run" depends="snow.jar">
      <java fork="true"
	    classpathref="snow.classpath.dist"
	    classname="snow.Snow">
      </java>
    </target>

    <target name="snow.clean">
      <delete dir="${build.dir}"/>
      <delete file="${snow.jar.path}"/>
    </target>

    <target name="snow.dist" depends="snow.jar">
      <copy file="${snow.jar.path}"
	    toFile="${dist.dir}/snow.jar"/>
    </target>

    <target name="snow.distclean" depends="snow.clean">
      <delete dir="${dist.dir}"/>
    </target>

    <patternset id="snow.dist.misc"
		description="Additional includes in the source distributions relative to basedir">
      <include name="build.xml"/>
      <include name="COPYING"/>
    </patternset>

    <patternset id="snow.source.misc"
		description="Additional includes in the source distribution relative to source root">
    </patternset>
    
    <target name="snow.source.prepare">
      <property name="snow.source.eol" value="asis"/>
      <echo>Using snow.source.eol='${snow.source.eol}' to drive
      source code line-ending transformations.</echo>
      <property name="snow.build.src.dir"
		value="${build.dir}/snow-src"/>
      <mkdir dir="${snow.build.src.dir}/src"/>
      <fixcrlf srcdir="${src.dir}" 
	       eol="${snow.source.eol}"
	       destdir="${snow.build.src.dir}/src"
	       preservelastmodified="true">
	<patternset refid="snow.source.java"/>
	<patternset refid="snow.source.lisp"/>
	<patternset refid="snow.source.misc"/>
      </fixcrlf>
      <fixcrlf srcdir="${basedir}" 
	       eol="${snow.source.eol}"
	       destdir="${snow.build.src.dir}"
	       preservelastmodified="true">
	<patternset refid="snow.dist.misc"/>
      </fixcrlf>
    </target>

    <target name="snow.source.tar" depends="snow.source.prepare">
      <mkdir dir="${dist.dir}"/>
      <tar destfile="${dist.dir}/snow-src.tar.gz"
	   compression="gzip">
	<tarfileset dir="${build.dir}">
	  <include name="snow-src/**"/>
	</tarfileset>
      </tar>
    </target>

    <target name="snow.source.zip" depends="snow.source.prepare">
      <mkdir dir="${dist.dir}"/>
      <zip destfile="${dist.dir}/snow-src.zip"
	   compress="true">
	<zipfileset dir="${snow.build.src.dir}" prefix="snow-src"/>
      </zip>
    </target>

    <!-- Testing -->

    <property name="snow.test.dir" value="${basedir}/test"/>

    <property name="snow.test.src.dir" value="${snow.test.dir}/src"/>
    <property name="snow.test.classes.dir" value="${snow.test.dir}/bin"/>

    <patternset id="snow.test.source.java">
      <!-- For now, we list tests explicitly, because we have to
           enumerate them later to the JUnit test runner. -->
      <include name="snow/BindingTest.java"/>
    </patternset>

    <path id="snow.test.compile.classpath">
      <pathelement location="${snow.test.dir}/lib/junit.jar"/>
      <path refid="snow.classpath.build" />
    </path>

    <target name="snow.test.compile" depends="snow.compile">
      <mkdir dir="${snow.test.classes.dir}"/>
      <javac destdir="${snow.test.classes.dir}"
	     classpathref="snow.test.compile.classpath"
	     debug="true"
	     target="1.6">
	<src path="${snow.test.src.dir}"/>
	<patternset refid="snow.test.source.java"/>
      </javac>
    </target>

    <path id="snow.test.run.classpath">
      <path refid="snow.test.compile.classpath"/>
      <pathelement location="${snow.test.classes.dir}"/>
      <path refid="snow.classpath.build" />
    </path>

    <target name="snow.test" depends="snow.test.java"/>
	
    <target name="snow.test.java" depends="snow.test.compile">
      <java fork="true"
	    classpathref="snow.test.run.classpath"
	    classname="org.junit.runner.JUnitCore">
	<arg value="snow.BindingTest"/>
      </java>
    </target>

</project>

