<project name="DemoRP" default="dist">

  <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"/>
      <property name="dist.type" value="-infocard"/>
    </ant>

    <fileset id="openid4java-libs" dir="${openid4java.dir}">
        <include name="build/openid4java-infocard.jar"/>
        <include name="lib/*jar"/>
        <include name="lib/infocard/*jar"/>
        <include name="lib/optional/*jar"/>
    </fileset>

    <path id="classpath">
      <fileset dir="lib" includes="*.jar"/>
      <fileset refid="openid4java-libs"/>
      <fileset dir="${openid4java.lib.dir}" includes="extra/*jar"/>
    </path>

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

  </target>

  <target name="echo" description="echoproperties" depends="init">
    <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" flatten="true">
        <fileset dir="build" includes="${component.name}.jar"/>
        <fileset dir="lib" includes="*.jar"/>
      <fileset refid="openid4java-libs"/>
    </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"
            file="${local.config.properties}"
            overwrite="yes"/>
  </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/${component.name}.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/${component.name}.war" file="dist/${component.name}.war"/>
  </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="DemoRP"
      doctitle="DemoRP"
      source="${compile.source}">
      <bottom>
        <![CDATA[<i>Copyright &#169; 2006-2008 Sxip Identity Corp. All Rights Reserved.</i>]]></bottom>
      <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="/demorp" 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="/demorp"/>
  </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="/demorp"/>
  </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, javadoc, dist"
          description="Run all targets."/>

</project>
