<project name="J-FTP" default="jars" basedir=".">

  <!-- Load any local properties -->
  <property file="${user.home}/.ant.properties"/> 
  <property file="local.properties"/>

<!-- ========== Compiler Defaults ========================================= -->

  <!-- CVSROOT used by cruise control can checkout source code -->
  <property name="cvs.repository" value=":pserver:anonymous@cvs.j-ftp.sourceforge.net:/cvsroot/j-ftp"/>

  <!-- CVS Project for cruise control to check out --> 
  <property name="cvs.package" value="j-ftp"/>

  <!-- Should Java compilations set the 'debug' compiler option? -->
  <property name="compile.debug"           value="true"/>

  <!-- Should Java compilations set the 'deprecation' compiler option? -->
  <property name="compile.deprecation"     value="false"/>

  <!-- Should Java compilations set the 'optimize' compiler option? -->
  <property name="compile.optimize"        value="true"/>

  <!-- define this in local.properties to set it to a different value -->
  <property name="jftp.version"            value="0.00"/>

  <property name="junit.lib"               value="lib"/>
  <property name="junit.jar"               value="${junit.lib}/junit.jar"/>
  <property name="log4j.lib"               value="lib"/>
  <property name="log4j.jar"               value="${log4j.lib}/log4j.jar"/>

  <!-- Construct compile classpath -->
  <path id="compile.classpath">
    <pathelement location="build/classes"/>
    <pathelement location="${log4j.jar}"/>
    <pathelement location="lib/"/>
    <fileset dir="lib">
        <include name="*.jar"/>
    </fileset>
  </path>

    <!-- Construct doc compile classpath -->
  <path id="compile.docclasspath">
    <pathelement location="build/jars/jftp.jar"/>
    <pathelement location="${log4j.jar}"/>
    <pathelement location="lib/"/>
    <fileset dir="lib">
        <include name="*.jar"/>
    </fileset>
  </path>

<!-- ========== Executable Targets ======================================== -->

  <target name="prepare" depends="detect" description="Prepare build directory">
    <mkdir dir="build"/>
    <mkdir dir="doc"/>
    <mkdir dir="doc/javadoc"/>
    <mkdir dir="build/classes"/>
    <mkdir dir="build/jars"/>
  </target>

  <target name="compile" depends="prepare" description="Compile shareable components">
    <javac  srcdir="src/java"
           destdir="build/classes"
	     debug="${compile.debug}"
       deprecation="${compile.deprecation}"
          optimize="${compile.optimize}"
	  nowarn="true">
      <classpath refid="compile.classpath"/>
      <exclude name="**/Log4JLogger.java" unless="log4j.present"/>
  </javac>
      <copy todir="build/classes">
      <fileset dir="lib">
        <include name="**/*"/>
      </fileset>
      </copy>
  </target>
  
  <target name="panel" depends="prepare" description="Compile Custom">
    <javac  srcdir="src/java/net/sf/jftp/gui"
	   destdir="build/classes"
             debug="${compile.debug}"
       deprecation="${compile.deprecation}"
          optimize="${compile.optimize}">
      <classpath refid="compile.classpath"/>
      <include name="**/LocalDir.java"/>
  </javac>       
  </target>
  
  <target name="compiledoc" depends="prepare" description="Compile doc components">
    <javac  srcdir="doc/"
           destdir="build/classes"
             debug="${compile.debug}"
     deprecation="${compile.deprecation}"
          optimize="${compile.optimize}">
      <classpath refid="compile.docclasspath"/>
      <exclude name="**/Log4JLogger.java" unless="log4j.present"/>
    </javac>       
  </target>

  <target name="javadoc" description="Generate JavaDoc" depends="prepare">
    <javadoc packagenames="net.sf.jftp.*" sourcepath="src/java"
            defaultexcludes="yes" destdir="doc/javadoc" author="true"
            version="true" windowtitle="JFtp"/>
  </target>

  <target name="run" description="Run the JFTP Command" depends="jars">
      <!-- this will execute the jtfp program in the build directory
      I figured this would be best because we could execute from the
      classes or the jar and still get our host list -->
    <java dir="build" jar="build/jars/jftp.jar" fork="true"/>
  </target>

  <target name="clean" description="Clean build and distribution directories">
    <!-- Delete the two directories for compiled code
        the cfg settings are in the build/cfg directory
        so they won't be lost -->
  	<delete file="build/jars/jftp.jar"/>
    <delete dir="build/jars"/>
    <delete dir="build/classes"/>
  </target>

  <target name="jars" depends="clean, compile" description="Create Executable Jar">
    <jar compress="true" jarfile="build/jars/jftp.jar"
            basedir="build/classes"
            manifest="src/meta-inf/manifest.mf">
      <zipfileset dir="src/images/" prefix="images">
        <include name="**"/>
      </zipfileset>
      <zipfileset dir="." prefix="docs">
        <include name="readme"/>
        <include name="CHANGELOG"/>
        <include name="TODO"/>
        <include name="doc/nfsinfo"/>
      </zipfileset>
      <zipfileset dir="src/xml/" prefix="JESKTOP-INF">
        <include name="*.xml"/>
      </zipfileset>
      <zipfileset dir="src/resources/">
        <include name="*.properties"/>
      </zipfileset>
    </jar>
  </target>

  <target name="signjar">
    <signjar alias="cdemon" jar="build/jars/jftp.jar"
     keystore="../cdemon" storepass="keytool!"/>
    <delete dir="build/classes"/>
  </target>
  
  <!-- Completely build all dists -->
  <target name="dist" depends="jars, signjar" description="Generates J-Ftp downloadables">
    <mkdir dir="dist"/>

    <copy todir="dist">
      <fileset dir="build/jars">
        <include name="*.jar"/>
      </fileset>
    </copy>

    <patternset id="jftp.source">
       <include name="src/**"/>
       <include name="lib/*"/>
       <include name="build.xml"/>
       <include name="readme"/>
    </patternset>

    <zip zipfile="dist/jftp-${jftp.version}-src.zip">
      <fileset dir=".">
        <patternset refid="jftp.source"/> 
      </fileset>
    </zip>         

    <tar longfile="gnu" tarfile="dist/jftp-${jftp.version}-src.tar" >
      <tarfileset dir="." username="jftp" group="jftp">
        <patternset refid="jftp.source"/> 
      </tarfileset>
    </tar>

    <gzip zipfile="dist/jftp-${jftp.version}-src.tar.gz"
          src="dist/jftp-${jftp.version}-src.tar"/>
    <delete file="dist/jftp-${jftp.version}-src.tar"/>
  </target>

  <target name="detect" depends="detect.display"
   description="Display configuration and conditional compilation flags">
  </target>

  <target name="detect.display">
    <available property="log4j.present" classname="org.apache.log4j.Category" classpath="${log4j.jar}" />
    <available property="junit.present" classname="junit.framework.TestCase" classpath="${junit.jar}" />
    <echo message="+-------------------------------------------------------" />
    <echo message="| Build environment for ${ant.project.name} ${jftp.version}" />
    <echo message="| " />
    <echo message="| Note: " />
    <echo message="|   If ${property.name} is displayed for a library " />
    <echo message="|   instead of 'true', that library is not present." />
    <echo message="+-------------------------------------------------------" />
    <echo message="" />
    <echo message="Environment:" />
    <echo message="  Java home                     ${java.home}" />
    <echo message="  Java version                  ${ant.java.version}" />
    <echo message="  Ant version                   ${ant.version}" />
    <echo message="  Compiler type                 ${build.compiler}" />
    <echo message="" />
    <echo message="Build options:" />
    <echo message="  Generate debugging info       ${compile.debug}" />
    <echo message="  Display deprecation info      ${compile.deprecation}" />
    <echo message="  Optimize                      ${compile.optimize}" />
    <echo message="" />
    <echo message="Optional libraries:" />
    <echo message="  JUnit                         ${junit.present}" />
    <echo message="  Log4J                         ${log4j.present}" />
    <echo message="" />
    <echo message="Library locations:" />
    <echo message="  JUnit jar                     ${junit.jar}" />
    <echo message="  Log4J jar                     ${log4j.jar}" />
  </target>

<!-- ========== Cruise Control (Continuous Integration) tasks ============= -->
  <target name="checkout" description="Update package from CVS">
    <cvs cvsroot="${cvs.repository}" package="${cvs.package}" dest=".."/>
  </target>

  <target name="masterbuild" depends="checkout, compile, jars, javadoc" description="Cruise control master build"/>

  <target name="cleanbuild" depends="clean, masterbuild" description="Cruise control clean build"/>
</project>
