<project name="infocard-op" default="dist" basedir=".">

  <property file="local.properties"/>
  <property file="project.properties"/>

  <target name="local_config" >
    <condition property="local.config.properties"
               value="../conf/${local.config}">
      <and>
        <isset property="local.config"/>
        <not>
          <equals arg1="" arg2="${local.config}"/>
        </not>
      </and>
    </condition>
  </target>

  <target name="echo_local_config" depends="local_config" if="local.config.properties">
    <echo message="Using local config: ${local.config.properties}" />
  </target>


  <target name="init" depends="local_config, echo_local_config">

    <echo message="Building openid4java library..."/>
    <ant antfile="build.xml" dir="${openid4java.dir}" target="jar" inheritall="false">
      <property file="${openid4java.dir}/project.properties"/>
    </ant>

    <path id="classpath">
      <fileset dir="lib" includes="*.jar"/>
      <filelist dir="${openid4java.dir}/build">
        <file name="openid4java.jar"/>
      </filelist>
      <filelist dir="${openid4java.lib.dir}">
        <file name="commons-codec-1.3.jar"/>
        <file name="httpclient-4.0.jar"/>
        <file name="httpcore-4.0.1.jar"/>
        <file name="commons-logging-1.03.jar"/>
        <file name="nekohtml-1.9.7.jar"/>
        <file name="xercesImpl-2.8.1.jar"/>
      </filelist>
      <filelist dir="${openid4java.lib.dir}/extra">
        <file name="servlet-api-2.4.jar"/>
        <file name="svnant-1.0.0.jar"/>
        <file name="svnClientAdapter-1.0.0.jar"/>
        <file name="svnjavahl-1.0.0.jar"/>
      </filelist>
      <fileset dir="${openid4java.lib.dir}/optional" includes="*jar"/>
      <fileset dir="${openid4java.lib.dir}/xri" includes="*jar"/>
    </path>

    <path id="classpath.test">
      <path refid="classpath"/>
      <pathelement location="build/classes"/>
    </path>

  </target>

  <target name="echo"
          depends="init"
          description="echo properties set by 'init'; useful for debugging">
    <echoproperties/>
  </target>

  <target name="launch_browser" if="browser.cmd">
    <fail unless=".html.file.to.browse"/>
    <exec command="${browser.cmd} ${.html.file.to.browse}" spawn="yes"/>
  </target>

  <target name="compile" depends="init" description="Compile the source code.">
    <mkdir dir="build/classes"/>
    <javac
            srcdir="src"
            destdir="build/classes"
            classpathref="classpath"
            debug="${compile.debug}"
            debuglevel="${compile.debuglevel}"
            optimize="${compile.optimize}"
            deprecation="${compile.deprecation}"
            target="${compile.target}"
            source="${compile.source}"
            />
  </target>

  <target name="compile_test" depends="compile">
    <mkdir dir="build/test/classes"/>
    <javac
            srcdir="test/unit/src"
            destdir="build/test/classes"
            classpathref="classpath.test"
            debug="${compile.debug}"
            debuglevel="${compile.debuglevel}"
            optimize="${compile.optimize}"
            deprecation="${compile.deprecation}"
            target="${compile.target}"
            source="${compile.source}"
            />
    <javac
            srcdir="test/functional/src"
            destdir="build/test/classes"
            classpathref="classpath.test"
            debug="${compile.debug}"
            debuglevel="${compile.debuglevel}"
            optimize="${compile.optimize}"
            deprecation="${compile.deprecation}"
            target="${compile.target}"
            source="${compile.source}"
            />
  </target>

  <target name="jar" depends="compile"
          description="Package the binary code into a jar file.">
    <jar basedir="build/classes" destfile="build/${component.name}.jar"/>
  </target>

  <target name="war_folder" depends="jar">
    <delete dir="build/war"/>
    <mkdir dir="build/war"/>
    <copy todir="build/war">
      <fileset dir="www">
        <exclude name="WEB-INF/web.xml"/>
      </fileset>
    </copy>
    <copy todir="build/war/WEB-INF/lib">
      <fileset dir="lib" includes="*.jar"/>
      <fileset dir="build" includes="${component.name}.jar"/>
    </copy>

    <taskdef resource="svntask.properties" classpathref="classpath"/>
    <svn javahl="false">
        <status path="." lastchangedrevisionproperty="svn.rev.local"/>
    </svn>
    <echo>Last Changed Rev (local): : ${svn.rev.local}</echo>
    <propertyfile file="build/war/VERSION">
        <entry key="Version" value="${svn.rev.local}"/>
    </propertyfile>

    <svn javahl="false" >
      <diff outFile="local.diff"/>
    </svn>
    <condition property="local.changes">
        <length file="local.diff" when="greater" length="0"/>
    </condition>
    <delete file="local.diff"/>

    <svn javahl="false">
      <diff oldTargetRevision="${svn.rev.local}" newTargetRevision="HEAD" outFile="head.diff"/>
    </svn>
    <condition property="head.changes">
        <length file="head.diff" when="greater" length="0"/>
    </condition>
    <delete file="head.diff"/>
    
  </target>

  <target name="copy_local_config" if="local.config.properties">
      <copy tofile="build/war/WEB-INF/classes/config.properties"
            overwrite="yes"
            file="${local.config.properties}" />
  </target>

  <target name="war" depends="war_folder, copy_local_config"
          description="Package the whole application as a war file.">
    <war destfile="build/${component.name}.war" webxml="www/WEB-INF/web.xml"
         duplicate="fail">
      <fileset dir="build/war"/>
    </war>
  </target>

  <target name="dist" depends="war"
          description="Create distributable artifacts.">
    <mkdir dir="dist"/>
    <copy file="build/${component.name}.war" todir="dist"/>
  </target>

  <target name="release" depends="dist"
          description="Copies WAR file to release folder.">
    <fail if="local.config.properties"
          message="Local configuration enabled; can't build release target."/>
    <fail if="local.changes"
          message="Local changes exist; can't build release target."/>
    <fail if="head.changes"
          message="Changes exist between BASE and HEAD; try 'svn up' or 'ant old_release'."/>
    <copy tofile="../../webapps/op.war" file="dist/${component.name}.war" />
  </target>

  <target name="old_release" depends="dist"
          description="Copies WAR file to release folder; no svn HEAD check.">
    <fail if="local.config.properties"
          message="Local configuration enabled; can't build release target."/>
    <fail if="local.changes"
          message="Local changes exist; can't build release target."/>
    <copy tofile="../../webapps/op.war" file="dist/${component.name}.war" />
  </target>

  <target name="test" depends="compile_test" description="Run test code.">
    <delete dir="build/test/xml"/>
    <mkdir dir="build/test/xml"/>
    <junit printsummary="on" fork="yes" dir="www"
           failureproperty="test.junit.failed">
      <classpath>
        <path refid="classpath.test"/>
        <path location="build/test/classes"/>
        <path location="www/WEB-INF/classes"/>
      </classpath>
      <formatter type="xml"/>
      <batchtest todir="build/test/xml">
        <fileset dir="test/unit/src">
          <include name="**/*Test.java"/>
        </fileset>
      </batchtest>
    </junit>
    <delete dir="build/test/html"/>
    <mkdir dir="build/test/html"/>
    <junitreport todir="build/test/html">
      <fileset dir="build/test/xml">
        <include name="*.xml"/>
      </fileset>
      <report todir="build/test/html"/>
    </junitreport>
    <antcall target="launch_browser">
      <param name=".html.file.to.browse" value="build/test/html/index.html"/>
    </antcall>
    <fail if="test.junit.failed" message="Unit Tests Failed"/>
  </target>

  <target name="javadoc" depends="compile"
          description="Create JavaDoc documentation">
    <mkdir dir="build/apidoc"/>
    <javadoc
            sourcepath="src"
            destdir="build/apidoc"
            classpathref="classpath"
            packagenames="*"
            access="private"
            version="yes"
            use="yes"
            author="yes"
            windowtitle="Infocard OpenID Provider"
            doctitle="Infocard OpenID Provider"
            source="${compile.source}">
      <bottom>
        <![CDATA[<i>Copyright &#169; 2006-2008 Sxip Identity Corp. All Rights Reserved.</i>]]></bottom>
      <link href="../common/build/apidoc" resolvelink="yes"/>
      <link href="http://bldbsf01.sxip.com/javadoc/jdk/1.5/"/>
      <link href="http://bldbsf01.sxip.com/javadoc/spring/2.0/"/>
      <link href="http://bldbsf01.sxip.com/javadoc/jug/2.0.0/"/>
      <link href="http://bldbsf01.sxip.com/javadoc/servlet/2.4/"/>
    </javadoc>
    <antcall target="launch_browser">
      <param name=".html.file.to.browse" value="build/apidoc/index.html"/>
    </antcall>
  </target>


  <target name="tomcat_set_home_from_env" if="env.CATALINA_HOME"
          unless="tomcat.home.dir">
    <property name="tomcat.home.dir" value="${env.CATALINA_HOME}"/>
  </target>

  <target name="check_tomcat" depends="tomcat_set_home_from_env">
    <echo level="info">Tomcat home directory: ${tomcat.home.dir}</echo>
    <fail unless="tomcat.home.dir"
          message="Tomcat home directory not set! Set either the CATALINA_HOME environment variable or the tomcat.home.dir property"/>
    <available file="${tomcat.home.dir}" type="dir"
               property="tomcat.home.dir.exists"/>
    <fail unless="tomcat.home.dir.exists"
          message="The Tomcat home directory does not exist!"/>

    <path id="classpath.tomcat">
      <pathelement location="${java.home}/../lib/tools.jar"/>
      <fileset dir="${tomcat.home.dir}/bin">
        <include name="*.jar"/>
      </fileset>
      <fileset dir="${tomcat.home.dir}/server/lib">
        <include name="*.jar"/>
      </fileset>
      <fileset dir="${tomcat.home.dir}/common/lib">
        <include name="*.jar"/>
      </fileset>
    </path>
  </target>

  <target name="define_tomcat" depends="check_tomcat">
    <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"
             classpathref="classpath.tomcat"/>
    <taskdef name="list" classname="org.apache.catalina.ant.ListTask"
             classpathref="classpath.tomcat"/>
    <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask"
             classpathref="classpath.tomcat"/>
    <taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask"
             classpathref="classpath.tomcat"/>
    <taskdef name="roles" classname="org.apache.catalina.ant.RolesTask"
             classpathref="classpath.tomcat"/>
    <taskdef name="start" classname="org.apache.catalina.ant.StartTask"
             classpathref="classpath.tomcat"/>
    <taskdef name="stop" classname="org.apache.catalina.ant.StopTask"
             classpathref="classpath.tomcat"/>
    <taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask"
             classpathref="classpath.tomcat"/>
  </target>

  <target name="deploy" depends="define_tomcat, dist"
          description="Deploy to local Tomcat">
    <deploy url="${tomcat.manager.url}" username="${tomcat.manager.username}"
            password="${tomcat.manager.password}"
            path="/op" war="dist/${component.name}.war"/>
  </target>

  <target name="reload" depends="define_tomcat"
          description="Reload on local Tomcat">
    <reload url="${tomcat.manager.url}" username="${tomcat.manager.username}"
            password="${tomcat.manager.password}"
            path="/op"/>
  </target>

  <target name="undeploy" depends="define_tomcat"
          description="Undeploy from local Tomcat">
    <undeploy url="${tomcat.manager.url}" username="${tomcat.manager.username}"
              password="${tomcat.manager.password}"
              path="/op"/>
  </target>

  <target name="redeploy" depends="undeploy, deploy"
          description="Redeploy on local Tomcat."/>

  <target name="list" depends="define_tomcat"
          description="List installed Tomcat applications">
    <list url="${tomcat.manager.url}" username="${tomcat.manager.username}"
          password="${tomcat.manager.password}"/>
  </target>

  <target name="clean" description="Remove all generated files">
    <delete dir="build"/>
    <delete dir="dist"/>
  </target>

  <target name="all" depends="clean, test, javadoc, dist"
          description="Run all targets."/>

</project>
