<project name="doc" default="all" basedir="." xmlns:if="ant:if">
  <property name="gwt.root" location=".." />
  <property name="project.tail" value="doc" />
  <import file="${gwt.root}/common.ant.xml" />

  <property.ensure name="gwt.user.jar" location="${gwt.build.lib}/gwt-user.jar" />
  <!-- Platform shouldn't matter here, just picking one -->
  <property.ensure name="gwt.dev.jar" location="${gwt.build.lib}/gwt-dev.jar" />

  <!--
    *** Note that if the USER_SOURCE_PATH paths are updated,
    the fileset dependencies in the outofdate tags in the
    javadoc target must be updated as well.
  -->
  <path id="USER_SOURCE_PATH">
    <pathelement location="${gwt.root}/user/src" />
    <pathelement location="${gwt.root}/user/javadoc" />
    <pathelement location="${gwt.root}/user/super" />
    <pathelement location="${gwt.root}/dev/core/src" />
    <pathelement location="${gwt.root}/dev/core/super" />
    <pathelement location="${project.build}/../user/jakarta-src" />
    <pathelement location="${project.build}/jakarta-src" />
  </path>

  <path id="USER_CLASS_PATH">
    <pathelement location="${gwt.dev.jar}" />
    <pathelement location="${gwt.user.jar}" />
    <pathelement location="${gwt.tools.lib}/junit/junit-4.8.2.jar" />
    <pathelement location="${gwt.tools.lib}/javax/validation/validation-api-1.0.0.GA.jar" />
    <pathelement location="${gwt.tools.lib}/javax/validation/validation-api-1.0.0.GA-sources.jar" />
    <pathelement location="${gwt.tools.lib}/selenium/selenium-java-client-driver.jar" />
    <pathelement location="${gwt.tools.lib}/jakarta-servlet/jakarta.servlet-api-5.0.0/jakarta.servlet-api-5.0.0.jar" />
  </path>

  <path id="DOC_PATH">
    <pathelement location="./src" />
    <pathelement location="${gwt.build.lib}/gwt-doctool.jar" />
    <path refid="USER_SOURCE_PATH" />
  </path>

  <!-- Make an extra copy of this example just for javadocs, so that it can be modified to use jakarta, and the rewriting of the RPC class can point at it -->
  <target name="jakartaJavadocSources">
    <copy file="${gwt.root}/user/javadoc/com/google/gwt/examples/rpc/server/AdvancedExample.java" todir="${project.build}/jakarta-src/com/google/gwt/examples/rpc/server/jakarta/" encoding="${javac.encoding}">
      <filterchain>
        <tokenfilter>
          <replacestring from="package com.google.gwt.examples.rpc.server" to="package com.google.gwt.examples.rpc.server.jakarta"/>
          <replacestring from="com.google.gwt.user.server.rpc.RPC" to="com.google.gwt.user.server.rpc.jakarta.RPC"/>
          <replacestring from="javax.servlet" to="jakarta.servlet"/>
        </tokenfilter>
      </filterchain>
    </copy>
  </target>

  <!-- Rebuild the javadoc if a source file is newer than the index-->
  <target name="javadoc" depends="jakartaJavadocSources">
    <outofdate>
      <sourcefiles>
        <fileset file="./src/gwt-user.html" />
        <fileset dir="${project.build}/../user/jakarta-src">
          <include name="**/*.java" />
        </fileset>
        <fileset dir="${gwt.root}/user/src">
          <include name="**/*.java" />
        </fileset>
        <fileset dir="${gwt.root}/user/javadoc">
          <include name="**/*.java" />
        </fileset>
        <fileset dir="${gwt.root}/user/super">
          <include name="**/*.java" />
        </fileset>
        <fileset dir="${gwt.root}/dev/core/src">
          <include name="**/*.java" />
        </fileset>
        <fileset dir="${gwt.root}/dev/core/super">
          <include name="**/*.java" />
        </fileset>
      </sourcefiles>
      <targetfiles>
        <!--
          only checks one output file, will not rebuild other
          files if this one is up to date
        -->
        <pathelement path="${project.build}/javadoc/index.html" />
      </targetfiles>
      <sequential>
        <echo>Building javadoc</echo>
        <antcall target="makeJavadoc" />
      </sequential>
    </outofdate>
  </target>

  <!-- Really rebuild the javadoc -->
  <target name="makeJavadoc">
    <java classpathref="DOC_PATH" classname="com.google.doctool.custom.FindPackages" fork="yes" failonerror="true">
      <arg value="${gwt.root}" />
    </java>
    <property file="${gwt.root}/build/out/packages.properties" />
    <javadoc classpathref="DOC_PATH" failonerror="true" maxmemory="1g"
             destdir="${project.build}/javadoc"
             source="${javac.release}"
             encoding="UTF-8"
             access="package"
             packagenames="${USER_PKGS}"
             sourcefiles="${USER_CLASSES}"
             use="true"
             windowtitle="GWT Javadoc"
             doctitle="GWT API Reference"
             header="GWT ${gwt.version}"
             linkoffline="https://docs.oracle.com/en/java/javase/11/docs/api/ validation-package-list">
      <!-- suppress errors that don't break the output, we should refine this to just suppress things we haven't fixed yet -->
      <arg value="-Xdoclint:none" />
      <classpath refid="USER_CLASS_PATH" />
      <sourcepath refid="USER_SOURCE_PATH" />

      <taglet name="com.google.doctool.custom.TipTaglet">
        <path refid="DOC_PATH" />
      </taglet>
      <taglet name="com.google.doctool.custom.IncludeTaglet">
        <path refid="DOC_PATH" />
      </taglet>
      <taglet name="com.google.doctool.custom.ExampleTaglet">
        <path refid="DOC_PATH" />
      </taglet>
    </javadoc>
  </target>

  <target name="emul-doc">
    <outofdate>
      <sourcefiles>
        <fileset dir="${gwt.root}/user/super/com/google/gwt/emul">
          <include name="**/*.java" />
        </fileset>
      </sourcefiles>
      <targetfiles>
        <pathelement path="${project.build}/emul-ezt/fragment.html" />
      </targetfiles>
      <sequential>
        <echo>Building JRE emulation summary</echo>
        <java classpathref="DOC_PATH" classname="com.google.doctool.custom.FindPackages" fork="yes" failonerror="true">
          <arg value="${gwt.root}" />
        </java>
        <property file="${gwt.root}/build/out/packages.properties" />
        <javadoc classpathref="USER_CLASS_PATH" failonerror="true"
                 sourcepath="${gwt.root}/user/super/com/google/gwt/emul:${gwt.root}/dev/core/super/com/google/gwt/dev/jjs/intrinsic"
                 encoding="UTF-8"
                 access="public"
                 source="${javac.release}"
                 packagenames="${JAVA_PKGS}"
                 docletpath="${project.build}/../build_tools/doctool/bin"
                 doclet="com.google.doctool.custom.JavaEmulSummaryDoclet">
          <arg value="-outfile" />
          <arg value="${project.build}/emul-ezt/fragment.html" />
        </javadoc>
      </sequential>
    </outofdate>
  </target>

  <target name="build" depends="javadoc, emul-doc" />
</project>
