<?xml version="1.0"?>

<project name="OpenID4Java Library" default="jar" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant">

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

    <target name="dist_type">
        <condition property="dist.type" value="">
            <not><isset property="dist.type"/></not>
        </condition>
        <condition property="include.xri">
            <or>
                <equals arg1="-xri" arg2="${dist.type}"/>
                <equals arg1="-full" arg2="${dist.type}"/>
            </or>
        </condition>
      <!--
              <condition property="include.infocard">
                  <or>
                      <equals arg1="-infocard" arg2="${dist.type}"/>
                      <equals arg1="-full" arg2="${dist.type}"/>
                  </or>
              </condition>
      -->
    </target>

    <target name="set_full_dist">
        <property name="dist.type" value="-full"/>
    </target>

    <target name="create_paths" depends="dist_type">
        <path id="classpath">
            <fileset dir="${lib.dir}">
                <include name="**/*.jar"/>
                <exclude name="lib/xri/**" unless="include.xri"/>
                <exclude name="lib/infocard/**"/>
            </fileset>
        </path>
    </target>

    <target name="prepare" depends="create_paths">
        <mkdir dir="${build}"/>
    </target>

    <target name="clean" depends="prepare"
            description="Removes all build artifacts">
        <delete dir="${build}" />
        <delete dir="${apidoc}"/>
        <delete dir="target" />
        <delete file="${component.name}-${version}.tar.gz"/>
    </target>

    <target name="echoproperties" depends="create_paths"
            description="Displays properties; 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="prepare" description="Compiles main code">
        <mkdir dir="${classes}"/>
        <javac destdir="${classes}" source="${jvm.ver}" target="${jvm.ver}"
               includeAntRuntime="no" debug="${debug}" deprecation="yes">
            <src path="${src}"/>
            <classpath refid="classpath"/>
            <exclude name="**/LocalXriResolver.java" unless="include.xri"/>
            <exclude name="**/infocard/**"/>
        </javac>
        <copy todir="${classes}">
            <fileset dir="${src}" includes="**/*.properties"/>
            <fileset dir="${src}" includes="**/*.xsd"/>
        </copy>
    </target>

    <target name="get-version">
        <taskdef resource="svntask.properties" classpathref="classpath"/>
        <svn>
            <status path="." lastchangedrevisionproperty="svn.rev"/>
        </svn>
        <property file="VERSION"/>
        <condition value="${Version}" else="${component.ver}.${svn.rev}"
                   property="version">
            <equals arg1="" arg2="${svn.rev}"/>
        </condition>
        <echo>Version: ${version}</echo>
    </target>

    <target name="jar" depends="compile, get-version"
            description="Packages compiled class files into a jar archive">
        <jar jarfile="${build}/${component.name}${dist.type}.jar">
            <fileset dir="${classes}"/>
            <manifest>
                <section name="openid4java">
                    <attribute name="Specification-Title" value="OpenID Authentication"/>
                    <attribute name="Specification-Version" value="2.0,1.1"/>
                    <attribute name="Specification-Vendor" value="openid.net"/>
                    <attribute name="Implementation-Title" value="openid4java"/>
                    <attribute name="Implementation-Version" value="${version}"/>
                </section>
            </manifest>
        </jar>
    </target>

    <target name="dist" depends="clean, jar, apidoc"
            description="Packages official distributable archive">
        <propertyfile file="VERSION">
            <entry key="Version" value="${version}"/>
        </propertyfile>
        <move file="${build}/${component.name}${dist.type}.jar"
              tofile="${basedir}/${component.name}${dist.type}-${component.ver}.jar"/>
        <delete dir="${build}"/>
        <delete file="${component.name}${dist.type}-${version}.tar.gz"/>
        <tar destfile="${component.name}${dist.type}-${version}.tar.gz" compression="gzip">
            <tarfileset dir="${basedir}" prefix="${component.name}${dist.type}-${version}">
                <exclude name="lib/xri/**" unless="include.xri"/>
                <exclude name="lib/infocard/**"/>
                <exclude name="*ipr"/>
                <exclude name="*iml"/>
                <exclude name="*iws"/>
                <exclude name="libsrc/**"/>
                <exclude name="lib/ignored/**"/>
                <exclude name="lib/test"/>
                <exclude name="target"/>
                <exclude name="testLog.txt"/>
                <exclude name=".classpath"/>
                <exclude name=".project"/>
                <exclude name=".settings"/>
                <exclude name="*tar.gz"/>
            </tarfileset>
        </tar>
        <delete file="${component.name}${dist.type}-${component.ver}.jar"/>
        <delete dir="${apidoc}"/>
        <delete file="VERSION"/>
    </target>

    <target name="dist-xri"
            description="Packages distributable archive with local XRI resolver support">
        <property name="dist.type" value="-xri"/>
        <antcall target="dist"/>
    </target>

    <target name="dist-full"
            description="Packages full distributable archive (local XRI resolver and OpenID-Infocard support)">
        <property name="dist.type" value="-full"/>
        <antcall target="dist"/>
    </target>

    <target name="release"
            description="Packages all distributable archives">
        <antcall target="dist"/>
        <antcall target="clean"/>
        <antcall target="dist-xri"/>
        <antcall target="clean"/>
      <!--
              <antcall target="dist-infocard"/>
              <antcall target="clean"/>
      -->
        <antcall target="dist-full"/>
        <antcall target="clean"/>
    </target>

    <target name="apidoc" depends="prepare"
            description="Builds Javadoc documentation">
        <mkdir dir="${apidoc}"/>
        <javadoc
            failonerror="yes"
            sourcepath="${src}"
            overview="${src}/overview.html"
            packagenames="*"
            destdir="${apidoc}"
            classpathref="classpath"
            access="private"
            use="yes"
            version="yes"
            author="yes"
            windowtitle="OpenID4Java Library" >
            <doctitle>OpenID4Java Library</doctitle>
            <bottom>
                <![CDATA[<i>Copyright 2006-2008 Sxip Identity Corporation</i>]]>
            </bottom>
        </javadoc>
        <antcall target="launch_browser">
            <param name=".html.file.to.browse" value="${apidoc}/index.html"/>
        </antcall>
    </target>

    <target name="maven-antlib" description="retreive dependencies with maven">
        <path id="maven.lib.path">
            <fileset dir="${basedir}/lib/extra/" includes="maven-ant-tasks*.jar" />
        </path>
        <typedef resource="org/apache/maven/artifact/ant/antlib.xml"
            uri="antlib:org.apache.maven.artifact.ant"
            classpathref="maven.lib.path"
        />
        <artifact:pom id="project" file="pom.xml" />
        <artifact:dependencies verbose="true" pathId="classpath.test" pomRefId="project" useScope="test"/>
    </target>

    <target name="compile_test" depends="compile, maven-antlib"
            description="Compiles test classes">
        <mkdir dir="${classes.test}"/>
        <javac srcdir="${test.src}"
               excludes="org/openid4java/infocard/**"
               destdir="${classes.test}"
               source="${jvm.ver}"
               target="${jvm.ver}"
               debug="true"
               deprecation="true"
               fork="true"
               includeAntRuntime="false">
            <classpath>
                <pathelement location="${classes}"/>
                <path refid="classpath.test"/>
            </classpath>
        </javac>
        <copy todir="${classes.test}">
            <fileset dir="${test.src}" includes="**/*.properties"/>
            <fileset dir="${test.src}" includes="**/*.html"/>
        </copy>
    </target>

    <target name="test" depends="set_full_dist, compile_test"
            description="Runs tests">
        <delete dir="${build}/test/data"/>
        <copy todir="${build}/test/data">
            <fileset dir="${test.data}" />
        </copy>
        <replace token="SERVLET_PORT" value="${test.servlet.port}"
                dir="${build}/test/data" />
        <delete dir="${build}/test/junit/xml"/>
        <mkdir dir="${build}/test/junit/xml"/>
        <junit printsummary="yes" fork="no" failureproperty="test.failed">
            <sysproperty key="YADIS_TEST_DATA" value="${build}/test/data"/>
            <sysproperty key="SERVLET_PORT" value="${test.servlet.port}"/>
            <sysproperty key="TEST_DATA" value="${test.src}/org/openid4java/"/>
            <classpath>
                <pathelement location="${classes.test}"/>
                <pathelement location="${classes}"/>
                <path refid="classpath.test"/>
            </classpath>
            <!--<formatter type="xml"/>-->
            <formatter type="plain"/>
            <batchtest fork="true" todir="${build}/test/junit/xml">
                <formatter type="xml"/>
                <fileset dir="${test.src}">
                    <include name="**/*Test.java"/>
                    <exclude name="**/Abstract*.java"/>
                    <exclude name="**/OpenIDTokenTest.java"/>
                </fileset>
            </batchtest>
        </junit>
        <delete dir="${build}/test/junit/html"/>
        <mkdir dir="${build}/test/junit/html"/>
        <junitreport todir="${build}/test/junit/html">
            <fileset dir="${build}/test/junit/xml">
                <include name="*.xml"/>
            </fileset>
            <report todir="${build}/test/junit/html"/>
        </junitreport>
        <antcall target="launch_browser">
            <param name=".html.file.to.browse"
                   value="build/test/junit/html/index.html"/>
        </antcall>
        <fail if="test.failed" message="Unit Tests Failed"/>
    </target>

    <target name="sync-pom-version" description="set the version in pom.xml to properties version">
        <echo>set version to ${component.ver}</echo>
        <replaceregexp byline="false">
            <regexp pattern="&lt;artifactId>openid4java-parent&lt;/artifactId>(\s*)&lt;version>([^&lt;]*)&lt;/version>"/>
            <substitution expression="&lt;artifactId>openid4java-parent&lt;/artifactId>\1&lt;version>${component.ver}&lt;/version>"/>
            <fileset dir="." includes="**/pom.xml"/>
       </replaceregexp>
    </target>
</project>
