<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="all" name="desktopgui">

	<property name="src" value="src"/>
	<property name="build" value="build"/>
	<property name="dist"  location="dist"/>
	<property name="jar" value="desktopgui.jar"/>
	<property name="javadoc" value="javadoc"/>
	<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="init">
	<mkdir dir="${build}"/>
	<mkdir dir="${build}/messages-src"/>
	<mkdir dir="${dist}"/>
	</target>

	<target name="clean">
		<delete dir="${build}"/>
		<delete dir="${dist}"/>
	</target>

        <target name="compile" depends="init">
            <javac debug="true" deprecation="on" source="${javac.version}" target="${javac.version}" 
                       release="${javac.release}"
                       includeAntRuntime="false"
                       encoding="UTF-8"
                       srcdir="${src}" destdir="${build}">
                <compilerarg line="${javac.compilerargs}" />
                <classpath>
                    <pathelement location="../../core/java/build/i2p.jar" />
                    <pathelement location="../../installer/lib/wrapper/all/wrapper.jar" />
                    <pathelement location="../../router/java/build/router.jar" />
                    <pathelement location="../systray/java/build/systray.jar" />
                </classpath>
            </javac>
        </target>

        <target name="bundle" unless="no.bundle" depends="init">
            <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>
            <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}">
                <compilerarg line="${javac.compilerargs}" />
            </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="compile, bundle, listChangedFiles" unless="jar.uptodate" >
		<!-- set if unset -->
		<property name="workspace.changes.tr" value="" />
                <!-- ideal for linux: 24x24, but transparency doesn't work -->
                <copy tofile="${build}/desktopgui/resources/images/logo.png" file="../../apps/routerconsole/jsp/themes/console/images/itoopie_xsm.png" />
                <copy todir="${build}/desktopgui/resources/images" file="images/itoopie_black_24.png" />
                <copy todir="${build}/desktopgui/resources/images" file="images/itoopie_white_24.png" />
		<jar basedir="${build}" excludes="messages-src/**" destfile="${dist}/${jar}">
			<manifest>
				<attribute name="Main-Class" value="net.i2p.desktopgui.Main"/>
				<attribute name="${manifest.classpath.name}" value="i2p.jar router.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="${dist}/${jar}" >
            <srcfiles dir= "." includes="build/**/*.class" />
        </uptodate>
        <condition property="shouldListChanges" >
            <and>
                <not>
                    <isset property="jar.uptodate" />
                </not>
                <isset property="git.available" />
            </and>
        </condition>
    </target>

    <target name="javadoc">
        <mkdir dir="${build}" />
        <mkdir dir="${build}/${javadoc}" />
        <javadoc 
            sourcepath="${src}" destdir="${build}/${javadoc}" 
            packagenames="*" 
            use="true" 
            splitindex="true" 
            windowtitle="Desktopgui">
            <classpath>
                <pathelement location="../../router/java/build/router.jar" />
                <pathelement location="../../core/java/build/i2p.jar" />
            </classpath>
        </javadoc>
    </target>

    <target name="poupdate">
        <!-- 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="dist" depends="jar" />	
	<target name="all" depends="jar" />
</project>

