<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="all" name="i2psnark">
    <target name="all" depends="clean, build" />
    <target name="build" depends="builddep, jar, war" />
    <target name="builddep">
        <!-- run from top level build.xml to get dependencies built -->
    </target>
    <condition property="depend.available">
        <typefound name="depend" />
    </condition>
    <target name="depend" if="depend.available">
        <depend
            cache="../../../build"
            srcdir="./src" 
            destdir="./build/obj" >
            <!-- Depend on classes instead of jars where available -->
            <classpath>
                <pathelement location="../../../core/java/build/obj" />
                <pathelement location="../../../core/java/build/gnu-getopt.jar" />
                <pathelement location="../../ministreaming/java/build/obj" />
                <pathelement location="../../jetty/jettylib/javax.servlet.jar" />
                <!-- jsp-api.jar only present for debian builds -->
                <pathelement location="../../jetty/jettylib/jsp-api.jar" />
                <!-- following jars only for standalone builds -->
                <pathelement location="../../desktopgui/dist/desktopgui.jar" />
            </classpath>
        </depend>
    </target>

    <!-- only used if not set by a higher build.xml -->
    <property name="javac.compilerargs" value="" />
    <property name="javac.version" value="1.8" />
    <property name="javac.release" value="8" />
    <property name="require.gettext" value="true" />
    <property name="manifest.classpath.name" value="Class-Path" />

    <condition property="no.bundle">
        <isfalse value="${require.gettext}" />
    </condition>

    <target name="compile" depends="depend">
        <mkdir dir="./build" />
        <mkdir dir="./build/obj" />
        <javac 
            srcdir="./src" 
            debug="true" deprecation="on" source="${javac.version}" target="${javac.version}" 
            release="${javac.release}"
            destdir="./build/obj" 
            encoding="UTF-8"
            includeAntRuntime="false" >
            <compilerarg line="${javac.compilerargs}" />
            <classpath>
                <pathelement location="../../../core/java/build/i2p.jar" />
                <!-- gnu-getopt.jar only present for debian builds -->
                <pathelement location="../../../core/java/build/gnu-getopt.jar" />
                <pathelement location="../../ministreaming/java/build/mstreaming.jar" />
                <pathelement location="../../jetty/jettylib/javax.servlet.jar" />
                <!-- jsp-api.jar only present for debian builds -->
                <pathelement location="../../jetty/jettylib/jsp-api.jar" />
                <!-- following jars only for standalone builds -->
                <pathelement location="../../jetty/jettylib/jetty-i2p.jar" />
                <pathelement location="../../systray/java/build/systray.jar" />
                <pathelement location="../../jetty/jettylib/org.mortbay.jetty.jar" />
                <pathelement location="../../jetty/jettylib/jetty-util.jar" />
                <pathelement location="../../desktopgui/dist/desktopgui.jar" />
            </classpath>
        </javac>
    </target>

    <target name="listChangedFiles" depends="jarUpToDate" if="shouldListChanges" >
        <exec executable="git" outputproperty="workspace.changes" errorproperty="mtn.error2" failifexecutionfails="false" >
            <arg value="status" />
            <arg value="-s" />
            <arg value="--porcelain" />
            <arg value="-uno" />
            <arg value=".." />
        </exec>
        <!-- trim flags -->
        <exec executable="sed" inputstring="${workspace.changes}" outputproperty="workspace.changes.sed" errorproperty="mtn.error2" failifexecutionfails="false" >
            <arg value="-e" />
            <arg value="s/^[MTADRCU ]*//" />
        </exec>
        <!-- \n in an attribute value generates an invalid manifest -->
        <exec executable="tr" inputstring="${workspace.changes.sed}" outputproperty="workspace.changes.tr" errorproperty="mtn.error2" failifexecutionfails="false" >
            <arg value="-s" />
            <arg value="[:space:]" />
            <arg value="," />
        </exec>
    </target>

    <target name="jar" depends="builddep, compile, jarUpToDate, listChangedFiles" unless="jar.uptodate" >
        <!-- set if unset -->
        <property name="workspace.changes.tr" value="" />
        <jar destfile="./build/i2psnark.jar" basedir="./build/obj" includes="**/*.class" excludes="**/web/* **/messages_*.class, **/standalone/*">
            <manifest>
                <attribute name="Main-Class" value="org.klomp.snark.CommandLine" />
                <attribute name="${manifest.classpath.name}" value="i2p.jar mstreaming.jar streaming.jar" />
                <attribute name="Implementation-Version" value="${full.version}" />
                <attribute name="Built-By" value="${build.built-by}" />
                <attribute name="Build-Date" value="${build.timestamp}" />
                <attribute name="Base-Revision" value="${workspace.version}" />
                <attribute name="Workspace-Changes" value="${workspace.changes.tr}" />
                <attribute name="X-Compile-Source-JDK" value="${javac.version}" />
                <attribute name="X-Compile-Target-JDK" value="${javac.version}" />
            </manifest>
        </jar>
    </target>

    <target name="jarUpToDate">
        <uptodate property="jar.uptodate" targetfile="build/i2psnark.jar" >
            <srcfiles dir= "build/obj" includes="**/*.class" excludes="**/web/* **/messages_*.class" />
        </uptodate>
        <condition property="shouldListChanges" >
            <and>
                <not>
                    <isset property="jar.uptodate" />
                </not>
                <not>
                    <isset property="war.uptodate" />
                </not>
                <isset property="git.available" />
            </and>
        </condition>
    </target>    

    <!-- Ideally we would include
      -  only include the servlet, everything else is in the jar.
      -  However, the wrapper.config classpath in existing installs
      -  does not include i2psnark.jar.
      -  So we must continue to duplicate everything in the war.
          <classes dir="./build/obj" includes="**/I2PSnarkServlet*.class" />
     -->
    <target name="war" depends="jar, bundle, warUpToDate, listChangedFiles" unless="war.uptodate" > 
        <!-- set if unset -->
        <property name="workspace.changes.tr" value="" />
        <copy todir="build/resources/.resources" >
            <fileset dir="../resources/" />
        </copy>
        <!-- mime.properties must be in with the classes -->
        <copy file="../mime.properties" todir="build/obj/org/klomp/snark/web" />
        <war destfile="../i2psnark.war" webxml="../web.xml" >
          <!-- include only the web stuff, as of 0.7.12 the router will add i2psnark.jar to the classpath for the war -->
          <classes dir="./build/obj" includes="**/web/*" />
            <fileset dir="build/resources/" />
            <manifest>
                <attribute name="Implementation-Version" value="${full.version}" />
                <attribute name="Built-By" value="${build.built-by}" />
                <attribute name="Build-Date" value="${build.timestamp}" />
                <attribute name="Base-Revision" value="${workspace.version}" />
                <attribute name="Workspace-Changes" value="${workspace.changes.tr}" />
                <attribute name="X-Compile-Source-JDK" value="${javac.version}" />
                <attribute name="X-Compile-Target-JDK" value="${javac.version}" />
            </manifest>
        </war>
    </target>

    <target name="warUpToDate">
        <uptodate property="war.uptodate" targetfile="../i2psnark.war" >
            <srcfiles dir= "." includes="build/obj/org/klomp/snark/web/*.class ../resources/**/* ../web.xml" />
            <srcfiles dir= "../resources" />
        </uptodate>
    </target>

    <target name="bundle" depends="compile" unless="no.bundle">
        <mkdir dir="build/messages-src" />
        <!-- Update the messages_*.po files.
             We need to supply the bat file for windows, and then change the fail property to true -->
        <exec executable="sh" osfamily="unix" failifexecutionfails="true" failonerror="${require.gettext}" >
            <env key="JAVA_HOME" value="${java.home}" />
            <arg value="./bundle-messages.sh" />
        </exec>
        <exec executable="sh" osfamily="mac" failifexecutionfails="true" failonerror="${require.gettext}" >
            <arg value="./bundle-messages.sh" />
        </exec>
        <!-- multi-lang is optional -->
        <exec executable="sh" osfamily="windows" failifexecutionfails="false" >
            <arg value="./bundle-messages.sh" />
        </exec>
        <javac source="${javac.version}" target="${javac.version}" 
               release="${javac.release}"
               includeAntRuntime="false"
               encoding="UTF-8"
               srcdir="build/messages-src" destdir="build/obj">
            <compilerarg line="${javac.compilerargs}" />
        </javac>
    </target>

    <target name="poupdate" depends="builddep, compile">
        <!-- Update the messages_*.po files.  -->
        <!-- set if unset -->
        <property name="lg2" value="" />
        <exec executable="sh" osfamily="unix" failifexecutionfails="true" failonerror="true" >
            <env key="LG2" value="${lg2}" />
            <arg value="./bundle-messages.sh" />
            <arg value="-p" />
        </exec>
        <exec executable="sh" osfamily="mac" failifexecutionfails="true" failonerror="true" >
            <env key="LG2" value="${lg2}" />
            <arg value="./bundle-messages.sh" />
            <arg value="-p" />
        </exec>
        <exec executable="sh" osfamily="windows" failifexecutionfails="true" failonerror="true" >
            <env key="LG2" value="${lg2}" />
            <arg value="./bundle-messages.sh" />
            <arg value="-p" />
        </exec>
    </target>

    <target name="standalone" depends="standalone_prep">
       <!-- doesn't support file permissions
        <zip destfile="build/i2psnark-standalone.zip">
          <zipfileset dir="./build/i2psnark/" fullpath="i2psnark" />
        </zip>
       -->
        <exec executable="zip" dir="build" failifexecutionfails="true" failonerror="true" >
            <arg value="-r" />
            <arg value="i2psnark-standalone.zip" />
            <arg value="i2psnark" />
        </exec>
    </target>

    <!-- make a fat jar for standalone -->
    <target name="standalone_jar" depends="war">
      <!-- set if unset -->
      <property name="workspace.changes.tr" value="" />
      <jar destfile="build/i2psnark-standalone.jar">
        <fileset dir="build/obj" includes="**/standalone/*.class" />
        <zipfileset src="build/i2psnark.jar" />
        <zipfileset src="../../../core/java/build/i2p.jar" />
      <!-- without this we get a warning about 'no JSP support' but that's it
        <zipfileset src="../../jetty/jettylib/jasper-runtime.jar"  />
       -->
        <zipfileset src="../../jetty/jettylib/javax.servlet.jar" />
        <zipfileset src="../../jetty/jettylib/jetty-continuation.jar" />
        <zipfileset src="../../jetty/jettylib/jetty-deploy.jar" />
        <zipfileset src="../../jetty/jettylib/jetty-http.jar" />
        <zipfileset src="../../jetty/jettylib/jetty-i2p.jar" />
        <zipfileset src="../../jetty/jettylib/jetty-io.jar" />
        <zipfileset src="../../jetty/jettylib/jetty-security.jar"  />
        <zipfileset src="../../jetty/jettylib/jetty-servlet.jar" />
        <zipfileset src="../../jetty/jettylib/jetty-util.jar" />
        <zipfileset src="../../jetty/jettylib/jetty-webapp.jar" />
        <zipfileset src="../../jetty/jettylib/jetty-xml.jar" />
        <zipfileset src="../../jetty/jettylib/org.mortbay.jetty.jar"  />
        <zipfileset src="../../ministreaming/java/build/mstreaming.jar" />
        <zipfileset src="../../streaming/java/build/streaming.jar" />
        <zipfileset src="../../systray/java/build/systray.jar" />
        <zipfileset src="../../../build/jbigi.jar" />
        <zipfileset src="../../desktopgui/dist/desktopgui.jar" />
        <!-- Countries translations. The i2psnark translations are in the war but it's easier to put these here -->
        <!-- 300KB just to translate "Brazil", but why not... -->
      <!--
        <fileset dir="../../routerconsole/java/build/obj" includes="net/i2p/router/countries/*.class" />
       -->
        <manifest>
            <attribute name="Main-Class" value="org.klomp.snark.standalone.RunStandalone"/>
            <attribute name="Implementation-Version" value="${full.version}" />
            <attribute name="Built-By" value="${build.built-by}" />
            <attribute name="Build-Date" value="${build.timestamp}" />
            <attribute name="Base-Revision" value="${workspace.version}" />
            <attribute name="Workspace-Changes" value="${workspace.changes.tr}" />
            <attribute name="X-Compile-Source-JDK" value="${javac.version}" />
            <attribute name="X-Compile-Target-JDK" value="${javac.version}" />
            <!--
               Suppress JNI warning in JRE 24+, and eventual restriction
               See https://openjdk.org/jeps/472
             -->
            <attribute name="Enable-Native-Access" value="ALL-UNNAMED" />
            <!-- this is so Jetty will report its version correctly -->
            <section name="org/eclipse/jetty/server/" >
                <attribute name="Implementation-Vendor" value="Eclipse.org - Jetty" />
                <attribute name="Implementation-Version" value="${jetty.ver}" />
            </section>
        </manifest>
      </jar>
    </target>

    <!-- add css, image, and js files for standalone snark to the war -->
    <target name="standalone_war" depends="war">
        <mkdir dir="build/standalone-resources/.resources/themes/" />
        <copy todir="build/standalone-resources/.resources/themes/" >
            <fileset dir="../resources/themes/" />
        </copy>

        <replace dir="build/standalone-resources/.resources/themes"
            summary="true"
            token="url(/themes/console/dark/images/"
            value="url(/i2psnark/.resources/themes/dark/images/" >
            <include name="**/*.css" />
        </replace>
        <replace dir="build/standalone-resources/.resources/themes"
            summary="true"
            token="url(../../console/light/images/"
            value="url(/i2psnark/.resources/themes/light/images/" >
            <include name="**/*.css" />
        </replace>
        <replace dir="build/standalone-resources/.resources/themes"
            summary="true"
            token="url(/themes/console/light/images/"
            value="url(/i2psnark/.resources/themes/light/images/" >
            <include name="**/*.css" />
        </replace>
        <replace dir="build/standalone-resources/.resources/themes"
            summary="true"
            token="url(/themes/console/images/transparent.gif"
            value="url(/i2psnark/.resources/themes/ubergine/images/transparent.gif" >
            <include name="**/*.css" />
        </replace>
        <replace dir="build/standalone-resources/.resources/themes"
            summary="true"
            token="url(/themes/console/images/info/"
            value="url(/i2psnark/.resources/themes/ubergine/images/" >
            <include name="**/*.css" />
        </replace>
        <replace dir="build/standalone-resources/.resources/themes"
            summary="true"
            token="url(/themes/console/images/buttons/"
            value="url(/i2psnark/.resources/icons/" >
            <include name="**/*.css" />
        </replace>

        <!-- Rather than pulling in all the console theme images, let's just specify the ones we need -->
        <copy file="../../routerconsole/jsp/themes/console/images/transparent.gif"
              todir="build/standalone-resources/.resources/themes/ubergine/images" />
        <copy file="../../routerconsole/jsp/themes/console/dark/images/header.png"
              todir="build/standalone-resources/.resources/themes/dark/images" />
        <copy file="../../routerconsole/jsp/themes/console/light/images/header.png"
              todir="build/standalone-resources/.resources/themes/light/images" />
        <copy file="../../routerconsole/jsp/themes/console/images/info/errortriangle.png"
              todir="build/standalone-resources/.resources/themes/ubergine/images" />
        <copy file="../../routerconsole/jsp/themes/console/images/buttons/search.png"
              todir="build/standalone-resources/.resources/icons" />

        <mkdir dir="build/standalone-resources/.resources/js" />
        <copy file="../../routerconsole/jsp/js/ajax.js" todir="build/standalone-resources/.resources/js" />

        <zip destfile="../i2psnark.war" update="true" duplicate="preserve" >
            <fileset dir="build/standalone-resources" />
        </zip>
    </target>

    <target name="standalone_prep" depends="standalone_jar, standalone_war">
        <delete dir="./build/i2psnark" />
        <mkdir dir="./build/i2psnark" />
        <copy file="../launch-i2psnark" todir="./build/i2psnark/" />
        <chmod type="file" file="./build/i2psnark/launch-i2psnark" perm="+x" />
        <copy file="../launch-i2psnark.bat" todir="./build/i2psnark/" />
        <mkdir dir="./build/i2psnark/contexts" />
        <copy file="../standalone-context.xml" tofile="./build/i2psnark/contexts/context.xml" />
        <mkdir dir="./build/i2psnark/docroot" />
        <copy file="../standalone-index.html" tofile="./build/i2psnark/docroot/index.html" />
        <mkdir dir="./build/i2psnark/webapps" />
        <copy file="../i2psnark.war" tofile="./build/i2psnark/webapps/i2psnark.war" />
        <copy file="../jetty-i2psnark.xml" tofile="./build/i2psnark/jetty-i2psnark.xml" />
        <copy file="../i2psnark-appctx.config" tofile="./build/i2psnark/i2psnark-appctx.config" />
        <copy file="./build/i2psnark-standalone.jar" tofile="./build/i2psnark/i2psnark.jar" />
        <copy file="../readme-standalone.txt" tofile="./build/i2psnark/readme.txt" />
        <!-- temp so announces work -->
        <copy file="../../../installer/resources/hosts.txt" tofile="./build/i2psnark/hosts.txt" />
        <copy todir="./build/i2psnark/licenses" >
            <fileset dir="../../../licenses" includes="LICENSE-GPLv2.txt, ABOUT-Jetty.html" />
        </copy>
        <mkdir dir="./build/i2psnark/logs" />
    </target>

    <target name="clean">
        <delete dir="./build" />
        <delete file="../i2psnark.war" />
    </target>
    <target name="cleandep" depends="clean">
    </target>
    <target name="distclean" depends="clean">
    </target>
</project>
