<project name="jcalendar" default="build.all" basedir=".">
    <description>
        This is the build file for Greef JCalendar.
    </description>

    <property file="build.properties"/>

    <property name="project.name" value="${ant.project.name}"/>
    <!-- set global properties for this build -->
    <property name="src" location="src"/>
    <property name="lib" location="lib"/>
    <property name="lib.build" location="${lib}/build"/>
    <property name="lib.runtime" location="${lib}/runtime"/>
    <property name="classes" location="classes"/>
    <property name="release" location="release"/>
    <property name="docs" location="docs"/>
    <property name="jar" location="${lib.runtime}/${project.name}.jar"/>
    <property name="zip" location="${release}/${project.name}.zip"/>
    <property name="src.zip" location="${release}/${project.name}-src.zip"/>

    <property name="jnlp.codebase" value="http://www.java.net/download/javadesktop/jcalendar/demo"/>

    <!-- project libraries -->
    <path id="libs">
        <fileset dir="${lib.build}">
            <include name="**/*.jar"/>
            <include name="**/*.zip"/>
        </fileset>
        <fileset dir="${lib.runtime}">
            <include name="**/*.jar"/>
            <include name="**/*.zip"/>
        </fileset>
    </path>

    <!-- pattern set for non-test classes -->
    <patternset id="non-test-classes">
        <include name="**/*.class"/>
        <exclude name="**/com/greef/test/**"/>
        <exclude name="test/**"/>
        <exclude name="**/*Test*"/>
        <exclude name="**/.dependency-info/**"/>
    </patternset>

    <!-- pattern set for backup-files -->
    <patternset id="backup-files">
        <include name="**/*.bak"/>
        <include name="**/*~"/>
        <include name="**/.#*"/>
        <include name="**/.dependency-info/**"/>
    </patternset>

    <!-- the initialization target -->
    <target name="init" description="initialization">
        <!-- Create the time stamp -->
        <tstamp/>
    </target>

    <target name="compile.classes" depends="init" description="compile the source">
        <mkdir dir="${classes}"/>
        <mkdir dir="${lib.build}"/>
        <mkdir dir="${lib.runtime}"/>

        <copy todir="${classes}">
            <fileset dir="${src}">
                <include name="**/*.properties"/>
                <include name="**/*.gif"/>
                <include name="**/*.jpg"/>
            </fileset>
        </copy>

        <!-- Compile the java code from ${src} into ${build} -->
        <javac srcdir="${src}" destdir="${classes}">
            <classpath refid="libs"/>
        </javac>
    </target>

    <target name="compile.all" depends="compile.classes" description="compile all"/>

    <target name="build.jar" depends="clean.classes, compile.all, clean.jar"
            description="creates the jar containing the classes">
        <mkdir dir="${release}"/>
        <jar destfile="${jar}">
            <fileset dir="${classes}">
                <patternset refid="non-test-classes"/>
            </fileset>
            <manifest>
                <attribute name="Main-Class" value="com.greef.ui.calendar.JCalendarDemo"/>
            </manifest>
        </jar>
    </target>

    <target name="build.zip" depends="build.jar, generate.javadocs"
            description="make the release archive">
        <copy todir="${release}">
            <fileset dir="res">
                <include name="**/*"/>
            </fileset>
        </copy>

        <replace file="${release}/${project.name}.jnlp" >
            <replacefilter token="@codebase@" value="${jnlp.codebase}"/>
        </replace>

        <zip destfile="${zip}">
            <fileset dir="${release}">
                <include name="${project.name}.jnlp"/>
            </fileset>
            <fileset dir="${lib.runtime}">
                <include name="*.jar"/>
            </fileset>
            <zipfileset dir="${docs}" prefix="docs">
                <include name="**/*"/>
            </zipfileset>
        </zip>
    </target>

    <target name="build.src.zip" description="archive the sources">
        <mkdir dir="${release}"/>

        <zip destfile="${src.zip}">
            <fileset dir="${basedir}">
                <include name="**/*"/>
                <exclude name="*.iml"/>
                <exclude name="*.ipr"/>
                <exclude name="*.iws"/>
                <exclude name="release"/>
                <exclude name="release/**/*"/>
                <exclude name="classes"/>
                <exclude name="classes/**"/>
                <exclude name="docs"/>
                <exclude name="docs/**"/>
                <exclude name="www/demo"/>
                <exclude name="www/demo/**"/>
            </fileset>
        </zip>
    </target>

    <target name="build.all" depends="clean.all, build.zip, build.src.zip"/>

    <target name="clean.classes" description="clean up builds">
        <delete dir="${classes}" excludes="${classes}" failonerror="false" />
    </target>

    <target name="clean.jar" description="deletes the jar file">
        <delete file="${jar}"/>
    </target>

    <target name="clean.zip" description="deletes the release zip file">
        <delete file="${zip}"/>
    </target>

    <target name="clean.src.zip" description="deletes the sources zip file">
        <delete file="${src.zip}"/>
    </target>

    <target name="clean.backup" description="deletes backup filess">
        <delete>
            <fileset dir="${basedir}" defaultexcludes="no">
                <patternset refid="backup-files"/>
            </fileset>
        </delete>
    </target>

    <target name="clean.all" depends="clean.backup, clean.classes,
        clean.jar, clean.zip, clean.src.zip"
        description="clean up builds and backups"/>

    <target name="test.junit" depends="compile.all" description="run the JUnit tests">
        <junit printsummary="yes" haltonfailure="yes" haltonerror="yes">
            <classpath>
                <pathelement location="${classes}"/>
                <path refid="libs"/>
            </classpath>

            <formatter type="plain" usefile="false"/>

            <test name="com.greef.util.IntervalTestCase"/>
            <test name="com.greef.util.IntervalsTestCase"/>
            <test name="com.greef.ui.calendar.CalendarTableModelTestCase"/>

        </junit>
    </target>

    <target name="generate.javadocs" description="creates Javadoc">
        <mkdir dir="${docs}/api"/>
        <javadoc sourcepath="${src}" destdir="${docs}/api" packagenames="com.*"
            windowtitle="Greef JCalendar"/>
    </target>

    <target name="upload.release" depends="build.all">
        <!-- creates the release directory -->
        <sshexec trust="true"
                host="${host.name}" username="${host.user}" password="${host.pass}"
	            command="mkdir ${host.basedir}/release/${release.version}"/>

        <!-- uploads the release files -->
        <scp trust="true" password="${host.pass}" file="${zip}"
                remotetofile="${host.user}@${host.name}:${host.basedir}/release/${release.version}/${project.name}-${release.version}.zip"/>
        <scp trust="true" password="${host.pass}" file="${src.zip}"
                remotetofile="${host.user}@${host.name}:${host.basedir}/release/${release.version}/${project.name}-src-${release.version}.zip"/>

        <!-- uploads the demo files -->
        <scp trust="true" password="${host.pass}"
             todir="${host.user}@${host.name}:${host.basedir}/demo">
            <fileset dir="${lib.runtime}">
                <include name="**/*.jar"/>
            </fileset>
            <fileset dir="${release}">
                <include name="**/*.jnlp"/>
            </fileset>
        </scp>

    </target>

</project>