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

<project name="qz" default="distribute" basedir=".">
    <property file="ant/project.properties"/>
    <import file="ant/platform-detect.xml"/>
    <import file="ant/javafx.xml"/>
    <import file="ant/signing.xml"/>
    <import file="ant/apple/installer.xml"/>
    <import file="ant/linux/installer.xml"/>
    <import file="ant/windows/installer.xml"/>

    <!-- Fixes some odd empty directory issues -->
    <defaultexcludes remove="**/.DS_Store"/>

    <target name="distribute" depends="init,quick-clean,build-jar,override-authcert,include-assets,whitelist-certs">
        <echo level="info">Process complete</echo>
    </target>

    <target name="init">
        <condition property="codesign.windows" value="true">
            <and>
                <isset property="target.os.windows"/>
                <isset property="signing.tsaurl"/>
            </and>
        </condition>
        <condition property="codesign.linux" value="true">
            <and>
                <isset property="target.os.linux"/>
                <isset property="signing.tsaurl"/>
            </and>
        </condition>

        <echo message="Building ${project.filename} using JDK ${ant.java.version}"/>
    </target>

    <target name="clean" depends="init">
        <delete dir="${out.dir}" includeemptydirs="true" defaultexcludes="false"/>
    </target>

    <target name="quick-clean">
        <!-- Cleanup lingering files (for other OS installers) -->
        <delete includeemptydirs="true" defaultexcludes="false" failonerror="false">
            <fileset dir="${out.dir}">
                <include name="**/**"/>
                <exclude name="**/jlink/**"/>
                <exclude name="**/javafx-*/**"/>
            </fileset>
        </delete>
    </target>

    <target name="compile-socket" depends="init">
        <mkdir dir="${build.dir}/${project.filename}"/>

        <!-- find the pdfbox jar -->
        <path id="pdfbox.found">
            <first>
                <fileset dir="lib/printing/">
                    <include name="pdfbox*.jar"/>
                </fileset>
            </first>
        </path>
        <pathconvert property="pdfbox.path" refid="pdfbox.found"/>

        <javac destdir="${build.dir}/${project.filename}" source="${javac.source}" target="${javac.target}" includeantruntime="false" encoding="UTF-8">
            <src path="${src.dir}"/>
            <classpath>
                <!-- prefer bouncycastle from pdfbox over others -->
                <path id="plugin.override">
                    <pathelement path="${pdfbox.path}"/>
                </path>
                <fileset dir="lib">
                    <include name="**/*.jar"/>
                </fileset>
                <fileset dir="${java.home}">
                    <include name="**/*.jar"/>
                </fileset>
            </classpath>
            <compilerarg value="-Xlint:-options"/>
        </javac>

        <!-- Include non-class files from src in build directory -->
        <copy todir="${build.dir}/${project.filename}">
            <fileset dir="${src.dir}" excludes="**/*.java"/>
        </copy>

        <copy todir="${dist.dir}">
            <fileset file="LICENSE.txt"/>
        </copy>
    </target>

    <target name="build-jar" depends="download-javafx,compile-socket,copy-libs,externalize-libs">
        <echo level="info">Building jar</echo>

        <!-- Strip jlink-incompat files -->
        <echo level="info">Stripping jlink-incompatible files</echo>
        <jar compress="${jar.compress}" index="${jar.index}" destfile="${dist.dir}/${project.filename}.jar" duplicate="preserve">
            <fileset dir="${build.dir}/${project.filename}"/>
            <!-- Some root level files will cause jlink to fail, they need to be cleaned up -->
            <fileset dir="${out.dir}/libs-temp" excludes="*.class,LICENSE,jetty-dir.css,about.html,Log4j-*"/>
            <manifest>
                <attribute name="Application-Name" value="${project.name}"/>
                <attribute name="Main-Class" value="qz.App"/>
                <attribute name="Permissions" value="all-permissions"/>
                <attribute name="Multi-Release" value="true"/>
            </manifest>
        </jar>
        <delete dir="${out.dir}/libs-temp" includeemptydirs="true" defaultexcludes="false"/>
    </target>

    <target name="externalize-libs" if="separate.static.libs" depends="copy-dylibs,copy-dlls,copy-solibs">
        <!-- Strip embedded, native resources -->
        <delete>
            <fileset dir="${out.dir}/libs-temp">
                <include name="**/*.jnilib"/>
                <include name="**/*.dylib"/>
                <include name="**/*.dll"/>
                <include name="**/*.so"/>
                <include name="**/*.a"/>
            </fileset>
        </delete>

        <!-- Delete empty files-->
        <delete includeemptydirs="true" defaultexcludes="false">
            <fileset dir="${out.dir}/libs-temp">
                <and>
                    <size value="0"/>
                    <type type="dir"/>
                </and>
            </fileset>
        </delete>
    </target>

    <target name="copy-libs" depends="download-javafx">
        <!-- Copy jfx libs-->
        <mkdir dir="${dist.dir}/libs"/>
        <copy todir="${dist.dir}/libs" flatten="true">
            <fileset dir="${target.fx.dir}">
                <include name="**/*.${target.libext}"/>
            </fileset>
        </copy>

        <!-- Copy platform independent third-party libs -->
        <mkdir dir="${out.dir}/libs-temp"/>
        <unzip dest="${out.dir}/libs-temp" overwrite="false">
            <fileset dir="${basedir}/lib">
                <include name="**/*.jar"/>
                <exclude name="**/javafx*"/>
            </fileset>
        </unzip>
        <!-- Copy platform specific target javafx version -->
        <echo level="info">Copying ${target.fx.dir}</echo>
        <unzip dest="${out.dir}/libs-temp" overwrite="false">
            <fileset dir="${target.fx.dir}">
                <include name="**/*.jar"/>
            </fileset>
        </unzip>
        <!-- Remove files that can/did collide during unpacking -->
        <delete includeemptydirs="true" failonerror="false">
            <fileset dir="${out.dir}/libs-temp">
                <include name="LICENSE*"/>
                <include name="README*"/>
                <include name="**/META-INF/**"/>
                <!-- Used by SecurityInfo.getMavenVersions() -->
                <exclude name="**/META-INF/maven/**"/>
                <!-- Required by log4j -->
                <exclude name="**/META-INF/**/apache/logging/**"/>
                <exclude name="**/META-INF/services/**"/>
            </fileset>
        </delete>
    </target>

    <!-- install override.crt for "community" branded builds -->
    <target name="override-authcert" if="authcert.use">
        <echo level="info">Bundling with manual cert for signing auth: ${authcert.use}</echo>
        <!-- See also: Constants.OVERRIDE_CERT -->
        <property description="suppress-property-warning" name="authcert.use" value="override.crt"/>
        <copy file="${authcert.use}" tofile="${dist.dir}/override.crt" overwrite="true"/>
    </target>

    <!-- install certs to "whitelist" directory for whitelabel builds -->
    <target name="whitelist-certs" depends="include-assets" if="whitelist.use">
        <echo level="info">Copying certificate(s) to dist/whitelist: ${whitelist.use}</echo>
        <!-- See also: Constants.WHITELIST_CERT_DIR -->

        <mkdir dir="${dist.dir}/whitelist"/>
        <property description="suppress property warning" name="whitelist.use" value=""/>
        <copy file="${whitelist.use}" todir="${dist.dir}/whitelist" overwrite="true"/>
    </target>

    <target name="include-assets" depends="init" unless="dist.minimal">
        <echo level="info">Copying resource files to output</echo>

        <!-- Create the demo folder -->
        <copy todir="${dist.dir}/demo">
            <fileset file="sample.html"/>
            <fileset dir="${basedir}" includes="css/**"/>
            <fileset dir="${basedir}" includes="fonts/**"/>
            <fileset dir="${basedir}" includes="js/**/*.js"/>
            <fileset dir="${basedir}" includes="assets/**">
                <exclude name="**/branding/"/>
            </fileset>
        </copy>
    </target>

    <target name="download-jre" unless="jre.skip">
        <condition property="target.os" value="windows">
            <isset property="target.os.windows"/>
        </condition>
        <condition property="target.os" value="mac">
            <isset property="target.os.mac"/>
        </condition>
        <property description="target.os default" name="target.os" value="linux"/>

        <echo level="info">Downloading and bundling the jre for ${target.os}</echo>
        <java jar="${dist.dir}/${project.filename}.jar" fork="true" failonerror="true">
            <arg value="jlink"/>
            <arg value="--platform"/>
            <arg value="${target.os}"/>
            <arg value="--arch"/>
            <arg value="${target.arch}"/>
        </java>
    </target>

    <target name="nsis" depends="get-target-arch,target-os-windows,distribute,download-jre,build-exe"/>
    <target name="pkgbuild" depends="get-target-arch,target-os-mac,distribute,download-jre,build-pkg"/>
    <target name="dmg" depends="get-target-arch,target-os-mac,distribute,download-jre,build-dmg"/>
    <target name="makeself" depends="get-target-arch,target-os-linux,distribute,download-jre,build-run"/>


</project>