<?xml version="1.0" encoding="UTF-8"?>

<!--
/**************************************************************************
 OmegaT - Java based Computer Assisted Translation (CAT) tool
 Copyright (C) 2000-2007 Keith Godfrey and Maxym Mykhalchuk
               2008-2010 Didier Briel, Alex Buloichik
               2012 Didier Briel
               2013 Didier Briel, Aaron Madlon-Kay
               2014 Didier Briel
               2015 Aaron Madlon-Kay, Didier Briel
               Home page: http://www.omegat.org/
               Support center: http://groups.yahoo.com/group/OmegaT/

 This file is part of OmegaT.

 OmegaT is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 OmegaT is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
-->

<project name="OmegaT" default="all" basedir=".">
    <property environment="env"/>
    <condition property="assets.dir" value="${env.OMEGAT_ASSETS_DIR}" else="..">
        <isset property="${env.OMEGAT_ASSETS_DIR}" />
    </condition>

    <description>Builds, tests, and runs the project OmegaT.</description>
    <import file="nbproject/build-impl.xml"/>
    <import file="nbproject/profiler-build-impl.xml"/>

    <fileset dir="lib" id="all-jars-fileset">
        <include name="**/*.jar"/>
    </fileset>
    <pathconvert pathsep=" " property="all-jars-path" refid="all-jars-fileset">
        <chainedmapper>
            <flattenmapper/>
            <globmapper from="*" to="lib/*"/>
        </chainedmapper>
    </pathconvert>

    <target name="-pre-init">
        <property name="iscc.exe" value="iscc.exe"/>
        <property file="src/org/omegat/Version.properties"/>
        <filter filtersfile="src/org/omegat/Version.properties"/>
        <filter token="all-jars-path" value="${all-jars-path}"/>
        <copy file="manifest-template.mf" tofile="manifest.mf" filtering="true"/>
        <condition property="updatenr"
                   value="0${update}"
                   else="${update}">
           <length string="${update}"
                   length="1"/>
        </condition>
        <condition property="distversion"
                   value="${version}"
                   else="${version}_${updatenr}">
           <equals arg1="${updatenr}" arg2="00"/>
        </condition>
    </target>

    <target name="-post-compile" depends="get-revision">
        <propertyfile file="${build.classes.dir}/org/omegat/Version.properties">
            <entry key="revision" value="${repo.revision}" />
        </propertyfile>
    </target>

    <!-- Set the repo.revision property from the first valid source in this order:
         1. src/org/omegat/Version.properties (only if not "dev", i.e. if this is
            an unversioned source distro made with `ant src`)
         2. This svn checkout's revision
         3. This git clone's SHA-1
         4. If none of the above, the value "unknown" -->
    <target name="get-revision" depends="get-source-revision,get-svn-revision,get-git-revision">
        <!-- Fallback in case value was not set from source, svn, or git -->
        <property name="repo.revision" value="unknown"/>
        <echo message="Detected revision: ${repo.revision}"/>
    </target>

    <target name="get-source-revision">
        <condition property="repo.revision" value="${revision}">
            <and>
                <isset property="revision"/>
                <not>
                    <equals arg1="${revision}" arg2="dev"/>
                </not>
            </and>
        </condition>
    </target>

    <available file=".svn" type="dir" property="svn.available"/>
    <target name="get-svn-revision" if="svn.available">
        <exec executable="svnversion" outputproperty="svn.revision" failifexecutionfails="false"/>
        <condition property="repo.revision" value="r${svn.revision}">
            <and>
                <isset property="svn.revision"/>
                <length string="${svn.revision}" trim="yes" length="0" when="greater"/>
            </and>
        </condition>
    </target>

    <available file=".git" type="dir" property="git.available"/>
    <target name="get-git-revision" if="git.available">
        <exec executable="git" outputproperty="git.sha1" failifexecutionfails="false">
            <arg value="rev-parse"/>
            <arg value="--short"/>
            <arg value="HEAD"/>
        </exec>
        <condition property="repo.revision" value="${git.sha1}">
            <and>
                <isset property="git.sha1"/>
                <length string="${git.sha1}" trim="yes" length="0" when="greater"/>
            </and>
        </condition>
    </target>

    <target name="-post-clean">
        <delete>
            <fileset dir="src" includes="**/*.class"/>
        </delete>
        <delete file="omegat.prefs"/>
        <delete file="filters.conf"/>
        <delete file="segmentation.conf"/>
        <delete file="log.txt"/>
        <delete file="manifest.mf"/>
        <delete dir="dist2"/>
    </target>

    <target name="-post-jar">
        <delete file="${dist.dir}/README.TXT"/>
        <!-- copy the native libraries to the distribution directory -->
        <copy todir="dist/native">
            <fileset dir="native"/>
        </copy>
        <!-- copy scripts to the distribution directory -->
        <copy todir="dist/scripts">
            <fileset dir="scripts"/>
        </copy>
    </target>

    <!-- Creates a zip with two localization packs -->
    <target name="l10n-pack" depends="init" description="Build a distribution of localizable files">
        <mkdir dir="dist2"/>
        <property name="l10n.project.dir" value="release/l10n-project"/>
        <property name="l10n.dir" value="${build.dir}/l10n"/>
        <mkdir dir="${l10n.dir}"/>

        <!-- minimal -->
        <property name="l10n.min" value="OmegaT_${distversion}_L10N_Minimal"/>
        <property name="l10n.min.dir" value="${l10n.dir}/${l10n.min}"/>
        <mkdir dir="${l10n.min.dir}"/>
        <copy todir="${l10n.min.dir}">
            <fileset dir="${l10n.project.dir}"/>
        </copy>
        <copy todir="${l10n.min.dir}/source">
            <fileset dir="."
                    includes="doc_src/en/InstantStartGuide.xml
                    doc_src/en/images/InstantGuide_1.png
                    doc_src/en/images/InstantGuide_2.png
                    src/**/Bundle.properties
                    release/win32-specific/CustomMessages.ini
                    scripts/properties/check_rules.properties
                    scripts/properties/spellcheck.properties
                    scripts/properties/svn_cleanup_selected.properties
                    scripts/properties/search_replace.properties
                    scripts/properties/replace_strip_tags.properties
                    scripts/properties/svn_clear_auth.properties
                    scripts/properties/strip_bidi_marks.properties
                    scripts/properties/nbsp.properties" />
        </copy>
        <copy file="release/readme.txt"
              tofile="${l10n.min.dir}/source/readme.utf8"
              encoding="UTF-8"
              outputencoding="UTF-8">
            <filterset begintoken="@@" endtoken="@@">
                <filter token="TRANSLATION_NOTICE"
                        value="This translation is the work of [insert your name], copyright© [insert year]."/>
            </filterset>
        </copy>
        <zip destfile="dist2/${l10n.min}.zip"
                basedir="${l10n.dir}" includes="${l10n.min}/**" />

        <!-- full -->
        <property name="l10n.max" value="OmegaT_${distversion}_L10N_Full"/>
        <property name="l10n.max.dir" value="${l10n.dir}/${l10n.max}"/>
        <mkdir dir="${l10n.max.dir}"/>
        <copy todir="${l10n.max.dir}">
            <fileset dir="${l10n.project.dir}"/>
        </copy>
        <copy todir="${l10n.max.dir}/source">
            <fileset dir="."
                    includes="doc_src/en/** src/**/Bundle.properties release/win32-specific/CustomMessages.ini
                    scripts/properties/check_rules.properties
                    scripts/properties/spellcheck.properties
                    scripts/properties/svn_cleanup_selected.properties
                    scripts/properties/search_replace.properties
                    scripts/properties/replace_strip_tags.properties
                    scripts/properties/svn_clear_auth.properties
                    scripts/properties/strip_bidi_marks.properties
                    scripts/properties/nbsp.properties"
                    excludes="doc_src/en/index.xml
                    doc_src/en/html/**
                    doc_src/en/pdf/**" />
        </copy>
        <copy file="release/readme.txt"
              tofile="${l10n.max.dir}/source/readme.utf8"
              encoding="UTF-8"
              outputencoding="UTF-8">
            <filterset begintoken="@@" endtoken="@@">
                <filter token="TRANSLATION_NOTICE"
                        value="This translation is the work of [insert your name], copyright© [insert year]."/>
            </filterset>
        </copy>
        <zip destfile="dist2/${l10n.max}.zip"
                basedir="${l10n.dir}" includes="${l10n.max}/**" />
    </target>

    <!-- Builds a source distribution -->
    <target name="src" depends="init,get-revision" description="Build source distribution">
        <mkdir dir="dist2"/>
        <!-- Create temporary Version.properties with current revision -->
        <tempfile property="temp.version.props" destdir="dist2" deleteonexit="true"/>
        <copy file="src/org/omegat/Version.properties" tofile="${temp.version.props}"/>
        <propertyfile file="${temp.version.props}">
            <entry key="revision" value="${repo.revision}"/>
        </propertyfile>
        <!-- Build zip -->
        <zip destfile="dist2/OmegaT_${distversion}${beta}_Source.zip">
            <fileset dir="."
                includes="docs/** images/** lib/** nbproject/** lib-mnemonics/** release/** src/** test/** gen/** manifest-template.mf build.xml native/** doc_src/** docs_devel/** scripts/**"
                excludes="**/nbproject/private/** **/build/** **/dist/** src/org/omegat/Version.properties" />
            <zipfileset file="${temp.version.props}" fullpath="src/org/omegat/Version.properties"/>
        </zip>
    </target>

    <!-- Copies all files to dist -->
    <target name="release" depends="docindexgen">
        <mkdir dir="dist"/>
        <copy todir="dist">
            <fileset dir="release" includes="*" excludes="readme.txt *-specific l10n-project ci"/>
        </copy>
        <copy todir="dist">
            <fileset dir="release/linux-specific"/>
            <fileset dir="release/win32-specific" includes="OmegaT.bat"/>
        </copy>
        <copy todir="dist"
              file="release/readme.txt">
            <!-- Remove TRANSLATION_NOTICE placeholder (from English readme) -->
            <filterset begintoken="@@" endtoken="@@">
                <filter token="TRANSLATION_NOTICE" value=""/>
            </filterset>
        </copy>
        <mkdir dir="dist/plugins"/>
        <copy todir="dist/plugins">
            <fileset dir="release/plugins-specific"/>
        </copy>
        <mkdir dir="dist/docs"/>
        <copy todir="dist/docs"> <!-- copy all files except English legalNotices.html -->
            <fileset dir="docs" excludes="en/legalNotices.html"/>
        </copy>
        <copy todir="dist/docs"> <!-- copy just English legalNotices.html -->
            <fileset dir="docs" includes="en/legalNotices.html"/>
            <!-- Remove TRANSLATION_NOTICE placeholder (from English manual) -->
            <filterset begintoken="@@" endtoken="@@">
                <filter token="TRANSLATION_NOTICE" value=""/>
            </filterset>
        </copy>
        <mkdir dir="dist/images"/>
        <copy todir="dist/images">
            <fileset dir="images"/>
        </copy>
        <mkdir dir="dist/lib"/>
        <copy todir="dist/lib">
            <fileset dir="lib" includes="**/*.txt" excludes="sources/**" />
        </copy>
    </target>

    <!-- Builds a ZIP distribution -->
    <target name="zip" description="Build Zip distro" depends="jar,release">
        <mkdir dir="dist2"/>
        <zip destfile="dist2/OmegaT_${distversion}${beta}_Without_JRE.zip">
            <zipfileset dir="dist" excludes="OmegaT OmegaT-JRE linux-install.sh omegat.kaptn OmegaT.exe OmegaT.iss OmegaT-JRE.iss OmegaT.l4J.ini l10n-project"/>
            <zipfileset dir="dist" includes="OmegaT linux-install.sh omegat.kaptn" filemode="755"/>
        </zip>
    </target>

    <!-- Builds a Linux (tar, bz2) distribution -->
   <target name="linux" description="Build Linux distro" depends="jar,release, check-jre-linux" if="jre-linux.exists">
        <mkdir dir="dist2"/>
        <mkdir dir="dist2/linux"/>
        <copy todir="dist2/linux/OmegaT">
            <fileset dir="dist" excludes="OmegaT OmegaT-JRE OmegaT.exe OmegaT.bat OmegaT.iss OmegaT-JRE.iss OmegaT.l4J.ini l10n-project"/>
        </copy>
        <copy file="dist/OmegaT-JRE" tofile="dist2/linux/OmegaT/OmegaT"/>
        <copy todir="dist2/linux/OmegaT/jre">
            <fileset dir="${assets.dir}/jre-linux"/>
        </copy>
        <tar destfile="dist2/OmegaT_${distversion}_Linux.tar">
            <tarfileset dir="dist2/linux" excludes="OmegaT/OmegaT OmegaT/linux-install.sh OmegaT/omegat.kaptn OmegaT/jre/bin/java"/>
            <tarfileset dir="dist2/linux" includes="OmegaT/OmegaT OmegaT/linux-install.sh OmegaT/omegat.kaptn OmegaT/jre/bin/java" filemode="755"/>
        </tar>
        <bzip2 zipfile="dist2/OmegaT_${distversion}${beta}_Linux.tar.bz2" src="dist2/OmegaT_${distversion}_Linux.tar"/>
   </target>

   <!-- Builds a Linux-64 (tar, bz2) distribution -->
   <target name="linux-64" description="Build Linux distro" depends="jar,release, check-jre-linux-64" if="jre-linux-64.exists">
        <mkdir dir="dist2"/>
        <mkdir dir="dist2/linux-64"/>
        <copy todir="dist2/linux-64/OmegaT">
            <fileset dir="dist" excludes="OmegaT OmegaT-JRE OmegaT.exe OmegaT.bat OmegaT.iss OmegaT-JRE.iss OmegaT.l4J.ini l10n-project"/>
        </copy>
        <copy file="dist/OmegaT-JRE" tofile="dist2/linux-64/OmegaT/OmegaT"/>
        <copy todir="dist2/linux-64/OmegaT/jre">
            <fileset dir="${assets.dir}/jre-linux-64"/>
        </copy>
        <tar destfile="dist2/OmegaT_${distversion}_Linux_64.tar">
            <tarfileset dir="dist2/linux-64" excludes="OmegaT/OmegaT OmegaT/linux-install.sh OmegaT/omegat.kaptn OmegaT/jre/bin/java"/>
            <tarfileset dir="dist2/linux-64" includes="OmegaT/OmegaT OmegaT/linux-install.sh OmegaT/omegat.kaptn OmegaT/jre/bin/java" filemode="755"/>
        </tar>
        <bzip2 zipfile="dist2/OmegaT_${distversion}${beta}_Linux_64.tar.bz2" src="dist2/OmegaT_${distversion}_Linux_64.tar"/>
   </target>


    <!-- Creates a Windows Installer -->
    <target name="do-win" depends="init,jar,zip" description="Creates a Windows Installer. PRE: Inno Setup (http://www.jrsoftware.org/isinfo.php) with all optional language packs must be installed and available on the path.
Custom strings from release\win32-specific must be added manually to the language files.">
        <copy todir="dist" file="release/win32-specific/OmegaT.exe"/>
        <copy todir="dist" file="release/win32-specific/OmegaT.l4J.ini"/>

        <delete file="dist/OmegaT.iss"/>
        <filter token="VERSION_NUMBER_SUBST" value="${distversion}${beta}"/>
        <copy todir="dist" file="release/win32-specific/OmegaT.iss" filtering="on"/>

        <mkdir dir="dist2"/>
        <exec executable="${iscc.exe}">
            <arg file="dist/OmegaT.iss"/>
        </exec>
    </target>

    <!-- Creates a Windows Installer with JRE-->
    <target name="do-win-jre" depends="check-jre" if="jre.exists" description="Creates a Windows Installer with JRE. PRE: Inno Setup (http://www.jrsoftware.org/isinfo.php) with all optional language packs must be installed and available on the path, and a JRE must be present in ${assets.dir}\\jre\\
Custom strings from release\win32-specific must be added manually to the language files.">
        <copy todir="dist" file="release/win32-specific/OmegaT.exe"/>
        <copy todir="dist" file="release/win32-specific/OmegaT.l4J.ini"/>

        <delete file="dist/OmegaT-JRE.iss"/>
        <filter token="VERSION_NUMBER_SUBST" value="${distversion}${beta}"/>
        <copy todir="dist" file="release/win32-specific/OmegaT-JRE.iss" filtering="on"/>
        <exec executable="${iscc.exe}">
            <arg file="dist/OmegaT-JRE.iss"/>
        </exec>
    </target>

    <!-- Builds a Mac distribution -->
    <target name="mac" description="Build Mac distro. PRE: A MacOS JRE must be present in ${assets.dir}/jre-mac-root/ and JavaAppLauncher must be in ${assets.dir}/ (obtain from AppBundler JAR)" depends="jar,release,check-mac-reqs">
        <!-- Build directories -->
        <mkdir dir="dist2"/>

        <filter token="VERSION_NUMBER_SUBST" value="${distversion}"/>
        <copy todir="build/" file="release/mac-specific/OmegaT.app/Contents/Info.plist" filtering="on"/>

        <zip destfile="dist2/OmegaT_${distversion}${beta}_Mac.zip">
            <zipfileset dir="dist" excludes="OmegaT OmegaT-JRE linux-install.sh omegat.kaptn OmegaT.exe OmegaT.bat OmegaT.iss OmegaT-JRE.iss OmegaT.l4J.ini l10n-project" prefix="OmegaT.app/Contents/Java"/>
            <zipfileset dir="release/mac-specific/" excludes="**/Info.plist,**/MacOS/OmegaT"/>
            <zipfileset dir="build/" includes="Info.plist" prefix="OmegaT.app/Contents/"/>
            <zipfileset dir="release/mac-specific/" includes="**/MacOS/OmegaT" filemode="755"/>
            <zipfileset dir="${assets.dir}/jre-mac-root/" filemode="755" prefix="OmegaT.app/Contents/PlugIns/jre.bundle/"/>
        </zip>
    </target>

	<target name="mac-signed" description="Build signed Mac distro; supply identity in `codesignIdentity` envar" depends="mac">
		<unzip src="dist2/OmegaT_${distversion}${beta}_Mac.zip" dest="build/mac-signed"/>
		<exec executable="codesign" failonerror="true">
			<arg line="--deep -s"/>
			<arg value="${env.codesignIdentity}"/>
			<arg value="build/mac-signed/OmegaT.app"/>
		</exec>
		<zip destfile="dist2/OmegaT_${distversion}${beta}_Mac_Signed.zip">
			<zipfileset dir="build/mac-signed/" excludes="**/MacOS/OmegaT,**/PlugIns/jre.bundle/**"/>
			<zipfileset dir="build/mac-signed/" includes="**/MacOS/OmegaT,**/PlugIns/jre.bundle/**" filemode="755"/>
		</zip>
	</target>

    <target name="check-win" depends="init">
        <condition property="os.windows">
            <and>
                <os family="windows"/>
                <available file="${iscc.exe}" filepath="${java.library.path}" />
            </and>
        </condition>
    </target>

    <target name="check-jre" depends="check-win" if="os.windows">
        <available file="${assets.dir}/jre" type="dir" property="jre.exists" />
    </target>

    <target name="check-jre-linux">
        <available file="${assets.dir}/jre-linux" type="dir" property="jre-linux.exists" />
    </target>

    <target name="check-jre-linux-64">
        <available file="${assets.dir}/jre-linux-64" type="dir" property="jre-linux-64.exists" />
    </target>


    <target name="check-mac-reqs">
        <available file="${assets.dir}/JavaAppLauncher" type="file" property="mac-java-stub.exists" />
        <fail message="There is no Java for MacOS in the ${assets.dir}/JavaAppLauncher" unless="mac-java-stub.exists" />
        <available file="${assets.dir}/jre-mac-root/Contents/Home/bin/java" type="file" property="mac-java-stub.exists1" />
        <fail message="There is no Java for MacOS in the ${assets.dir}/jre-mac-root/Contents/Home/bin/java" unless="mac-java-stub.exists1" />
        <available file="${assets.dir}/jre-mac-root/Contents/Home/lib/rt.jar" type="file" property="mac-java-stub.exists2" />
        <fail message="There is no Java for MacOS in the ${assets.dir}/jre-mac-root/Contents/Home/lib/rt.jar" unless="mac-java-stub.exists2" />
    </target>

    <target name="win" description="Build Win32 distro if on Windows. PRE: Inno Setup (http://www.jrsoftware.org/isinfo.php) with all optional language packs must be installed and available on the path, and for the JRE-version a JRE must be present in ${assets.dir}\\jre\\ "
                                        depends="check-win" if="os.windows">
        <ant target="do-win" />
        <ant target="do-win-jre" />
    </target>

    <!-- Creates WebStart package -->
    <target name="webstart" depends="init,jar">
        <ant dir="release/WebStart-specific" antfile="build.xml"/>
    </target>


    <!-- Builds all OmegaT products -->
    <target name="all" depends="l10n-pack,src,jar,zip,win,linux,linux-64, mac" />

    <target name="checksums" description="Compute the checksum for files"><!--depends="l10n-pack,src,jar,zip,win,linux,mac"-->
        <mkdir dir="dist2/checksums"/>

        <!-- Adjust the fileset according to what is needed to be checksumed -->
        <fileset dir="dist2" id="checksum_source">
            <include name="*.zip"/>
            <include name="*.exe"/>
        </fileset>

        <checksum todir="dist2/checksums" algorithm="SHA-512">
          <fileset refid="checksum_source"/>
        </checksum>
        <checksum todir="dist2/checksums" algorithm="MD5">
          <fileset refid="checksum_source"/>
        </checksum>
    </target>


    <target name="gen">
        <exec executable="xjc">
            <arg line="-d src"/>
            <arg line="-p gen.core.segmentation"/>
            <arg value="src/schemas/srx20.xsd"/>
        </exec>
        <exec executable="xjc">
            <arg line="-d src"/>
            <arg line="-p gen.core.filters"/>
            <arg value="src/schemas/filters.xsd"/>
        </exec>
        <exec executable="xjc">
            <arg line="-d src"/>
            <arg line="-p gen.core.tbx"/>
            <arg value="src/schemas/tbx.xsd"/>
        </exec>
        <exec executable="xjc">
            <arg line="-d src"/>
            <arg line="-p gen.core.project"/>
            <arg value="src/schemas/project_properties.xsd"/>
        </exec>
        <exec executable="xjc">
            <arg line="-d src"/>
            <arg line="-p gen.core.tmx14"/>
            <arg line="-b src/schemas/tmx14.xjb"/>
            <arg value="src/schemas/tmx14.xsd"/>
        </exec>
        <exec executable="xjc">
            <arg line="-d src"/>
            <arg line="-p gen.taas"/>
            <arg line="-b src/schemas/taas.xjb"/>
            <arg value="src/schemas/taas.xsd"/>
        </exec>
    </target>

    <target name="docindexgen" depends="jar">
        <mkdir dir="build/genclasses" />
        <javac srcdir="gen/src" destdir="build/genclasses">
            <classpath>
                <fileset dir="lib" includes="**/*.jar" />
                <pathelement path="build/classes" />
            </classpath>
        </javac>
        <copy todir="build/genclasses">
            <fileset dir="gen/src" excludes="**/*.java"/>
        </copy>

        <java classname="util.CreateDocIndexUtil">
            <classpath>
                <pathelement path="build/genclasses" />
                <pathelement path="build/classes" />
            	<pathelement path="lib/commons-io-2.4.jar" />
            </classpath>
        </java>
        <copy todir="docs">
            <fileset dir="release" includes="OmegaT-license.txt doc-license.txt changes.txt" />
        </copy>
    </target>

</project>
