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

<project name="qz" default="distribute" basedir=".">

    <target name="distribute" depends="init,clean,sign-jar,include-assets">
        <zip destfile="${zip.file}" basedir="${dist.applet.dir}"/>

        <echo message="Process complete" />
    </target>

    <target name="init">
        <property environment="env"/>
        <property file="ant/project.properties"/>

        <!-- Your custom code signing properties here. Feel free to change. -->
        <!-- The project will default to qz.ks if it doesn't exist -->
        <property file="${basedir}/../private/private.properties"/>

        <!-- The default code signing properties which uses qz.ks. -->
        <!-- Please leave this value the netbeans default as a fallback. -->
        <property file="ant/private/private.properties"/>

        <echo message="Building ${build.applet.name} / ${build.socket.name} using JDK ${ant.java.version}" />
    </target>

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

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

        <javac destdir="${build.applet.dir}" source="${javac.applet.source}" target="${javac.applet.target}" includeantruntime="false">
            <src path="${src.dir}"/>
            <classpath>
                <path id="plugin.override">
                    <pathelement path="${lib.dir}/jre/plugin.jar" />
                </path>
                <fileset dir="${lib.dir}">
                    <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.applet.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java"/>
        </copy>
    </target>

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

        <javac destdir="${build.socket.dir}" source="${javac.applet.source}" target="${javac.socket.target}" includeantruntime="false">
            <src path="${src.dir}"/>
            <classpath>
                <path id="plugin.override">
                    <pathelement path="${lib.dir}/jre/plugin.jar" />
                </path>
                <fileset dir="${lib.dir}">
                    <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.socket.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java"/>
        </copy>
    </target>

    <target name="build-jar" depends="build-applet-jar,build-socket-jar" />

    <target name="build-applet-jar" depends="compile-applet">
        <echo>Building Jar for Applet use</echo>

        <copy todir="${dist.applet.dir}/${lib.dir}">
            <fileset dir="${lib.dir}" includes="**/*.jar" excludes="ws/*.*,**/plugin.jar"/>
        </copy>

        <manifestclasspath property="manifest.main.applet.classpath" jarfile="${dist.applet.jar}">
            <classpath>
                <fileset dir="${dist.applet.dir}/${lib.dir}" includes="*.jar"/>
            </classpath>
        </manifestclasspath>

        <jar compress="${jar.compress}" index="${jar.index}" destfile="${dist.applet.jar}" duplicate="preserve">
            <fileset dir="${build.applet.dir}" excludes="org/joor/*.*,qz/ws/*.*"/>
            <indexjars>
                <fileset dir="${dist.applet.dir}/${lib.dir}" includes="*.jar"/>
            </indexjars>
            <manifest>
                <attribute name="Application-Name" value="${manifest.application.name}"/>
                <attribute name="Main-Class" value="${manifest.main.applet.class}"/>
                <attribute name="Permissions" value="${manifest.permissions}"/>
                <attribute name="Codebase" value="${manifest.codebase}"/>
                <attribute name="Caller-Allowable-Codebase" value="${manifest.caller.allowable.codebase}"/>
                <attribute name="Application-Library-Allowable-Codebase" value="${manifest.application.allowable.codebase}"/>
                <attribute name="Class-Path" value="${manifest.main.applet.classpath}"/>
            </manifest>
        </jar>
    </target>

    <target name="build-socket-jar" depends="compile-socket">
        <echo>Building Jar for Socket use</echo>
        <jar compress="${jar.compress}" index="${jar.index}" destfile="${dist.socket.jar}" duplicate="preserve">
            <fileset dir="${build.socket.dir}"/>
            <zipgroupfileset dir="${lib.dir}" includes="**/*.jar" excludes="**/plugin.jar"/>
            <manifest>
                <attribute name="Application-Name" value="${manifest.application.name}"/>
                <attribute name="Main-Class" value="${manifest.main.socket.class}"/>
                <attribute name="Permissions" value="${manifest.permissions}"/>
                <attribute name="Codebase" value="${manifest.codebase}"/>
                <attribute name="Caller-Allowable-Codebase" value="${manifest.caller.allowable.codebase}"/>
                <attribute name="Application-Library-Allowable-Codebase" value="${manifest.application.allowable.codebase}"/>
            </manifest>
        </jar>
    </target>


    <target name="sign-jar" depends="build-jar,sign-jar-self,sign-jar-tsa">
        <path>
            <fileset dir="${dist.dir}/${lib.dir}" includes="${lib.dir}/**/*.jar" />
        </path>
    </target>

    <!-- tsaurl attribute cannot be empty, so separate methods are needed to sign both jars -->
    <target name="sign-jar-self" unless="signing.tsaurl">
        <echo>Self-signing Applet jar</echo>
        <signjar jar="${dist.applet.jar}"
                 signedjar="${dist.applet.jar}"
                 alias="${signing.alias}"
                 storepass="${signing.storepass}"
                 keystore="${signing.keystore}"
                 keypass="${signing.keypass}"
                />
        <signjar alias="${signing.alias}"
                 storepass="${signing.storepass}"
                 keystore="${signing.keystore}"
                 keypass="${signing.keypass}">
            <path>
                <fileset dir="${dist.applet.dir}/${lib.dir}" includes="**/*.jar" />
            </path>
        </signjar>

        <echo>Self-signing Socket jar</echo>
        <signjar jar="${dist.socket.jar}"
                 signedjar="${dist.socket.jar}"
                 alias="${signing.alias}"
                 storepass="${signing.storepass}"
                 keystore="${signing.keystore}"
                 keypass="${signing.keypass}"
                />
    </target>

    <target name="sign-jar-tsa" if="signing.tsaurl">
        <echo>Signing Applet jar with timestamp</echo>
        <signjar jar="${dist.applet.jar}"
                 signedjar="${dist.applet.jar}"
                 alias="${signing.alias}"
                 storepass="${signing.storepass}"
                 keystore="${signing.keystore}"
                 keypass="${signing.keypass}"
                 tsaurl="${signing.tsaurl}"
                />
        <signjar alias="${signing.alias}"
                 storepass="${signing.storepass}"
                 keystore="${signing.keystore}"
                 keypass="${signing.keypass}"
                 tsaurl="${signing.tsaurl}">
            <path>
                <fileset dir="${dist.applet.dir}/${lib.dir}" includes="**/*.jar" />
            </path>
        </signjar>

        <echo>Signing Socket jar with timestamp</echo>
        <signjar jar="${dist.socket.jar}"
                 signedjar="${dist.socket.jar}"
                 alias="${signing.alias}"
                 storepass="${signing.storepass}"
                 keystore="${signing.keystore}"
                 keypass="${signing.keypass}"
                 tsaurl="${signing.tsaurl}"
                />
    </target>

    <!-- Get version information from JAR -->
    <target name="get-version" depends="build-jar">
        <property file="ant/project.properties"/>
        <java jar="${dist.applet.jar}" fork="true" outputproperty="build.version">
            <arg value="--version"/>
        </java>
        <!-- Fallback to a bogus version number if the above command failed -->
        <property name="build.version" value="0.0.0" />
        <echo>Version ${build.version}</echo>
        <!-- Calculate installation size -->
        <length property="build.socket.bytes" mode="all">
            <fileset dir="${dist.socket.dir}" includes="**/*"/>
        </length>
        <script language="javascript">
            <![CDATA[
            project.setNewProperty("build.socket.size", Math.round(project.getProperty("build.socket.bytes") / 1024));
            ]]>
        </script>
        <echo>Size: ${build.socket.size} KB</echo>
    </target>

    <target name="include-assets" depends="init,get-version" >
        <echo>Copying resource files to output</echo>

        <copy todir="${dist.applet.dir}/${demo.dir}/${asset.dir}">
            <fileset dir="${asset.dir}">
                <exclude name="**/*.properties"/>
                <exclude name="**/branding/"/>
            </fileset>
        </copy>
        <copy todir="${dist.socket.dir}/${demo.dir}/${asset.dir}">
            <fileset dir="${asset.dir}">
                <exclude name="**/*.properties"/>
                <exclude name="**/branding/"/>
            </fileset>
        </copy>

        <copy todir="${dist.applet.dir}/${demo.dir}/${js.dir}">
            <fileset dir="${js.dir}"/>
        </copy>
        <copy todir="${dist.socket.dir}/${demo.dir}/${js.dir}">
            <fileset dir="${js.dir}"/>
        </copy>

        <copy todir="${dist.applet.dir}/${demo.dir}">
            <fileset file="sample.html"/>
        </copy>
        <copy todir="${dist.socket.dir}/${demo.dir}">
            <fileset file="sample.html"/>
        </copy>

        <copy file="${jnlp.inf.dir}/APPLICATION.JNLP" tofile="${dist.applet.jnlp}" />

        <!-- Distribute applet with README, CHANGELOG, LICENSE -->
        <copy todir="${dist.applet.dir}">
            <fileset file="${basedir}/*.txt"/>
        </copy>

        <!-- Distribute socket with LICENSE only -->
        <copy todir="${dist.socket.dir}">
            <fileset file="${basedir}/LICENSE.txt"/>
        </copy>

    </target>

    <!--
    ################################################################
    #               Prepackage Steps - All Platforms               #
    ################################################################
    -->
    <target name="prepackage">
        <echo>Processing self-signing variables</echo>
        <property file="ant/self-sign.properties"/>

        <echo>Creating Firefox certificate config files</echo>
        <copy file="${firefoxconfig.in}" tofile="${firefoxconfig.out}" >
            <filterchain><expandproperties/></filterchain>
        </copy>

        <copy file="${firefoxprefs.in}" tofile="${firefoxprefs.out}" >
            <filterchain><expandproperties/></filterchain>
        </copy>

        <copy file="${basedir}/CHANGELOG.txt" tofile="${build.socket.dir}/CHANGELOG.txt" />
    </target>

    <!--
    ################################################################
    #                    Windows Installer                         #
    ################################################################
    -->
    <target name="nsis" depends="build-exe,sign-exe-self,sign-exe-tsa"/>

    <target name="build-exe" depends="distribute,prepackage,nsisbin-1,nsisbin-2,nsisbin-3">
        <echo>Creating installer using ${nsisbin}</echo>

        <property file="ant/windows/windows.properties"/>
        <copy file="${branding.dir}/${windows.icon}" tofile="${dist.socket.dir}/${windows.icon}" />

        <copy file="${windows.packager.in}" tofile="${windows.packager.out}" overwrite="true">
            <filterchain><expandproperties/></filterchain>
        </copy>

        <copy file="${windows.launcher.in}" tofile="${windows.launcher.out}" overwrite="true">
            <filterchain><expandproperties/></filterchain>
        </copy>

        <copy file="${windows.keygen.in}" tofile="${windows.keygen.out}" >
            <filterchain><expandproperties/></filterchain>
        </copy>

        <exec executable="${nsisbin}" failonerror="true" >
            <arg value="${windows.launcher.out}"/>
        </exec>
        <delete file="${windows.launcher.out}"/>

        <exec executable="${nsisbin}" failonerror="true" >
            <arg value="${windows.packager.out}"/>
        </exec>

        <!-- Cleanup lingering icon (for other OS installers) -->
        <delete file="${dist.socket.dir}/${windows.icon}" />
    </target>

    <!-- Linux makensis -->
    <target name="nsisbin-1" depends="init" unless="env.windir">
        <property name="nsisbin" value="makensis"/>
    </target>

    <!-- Win32 makensis -->
    <target name="nsisbin-2" depends="init" unless="env.ProgramFiles(x86)">
        <property name="nsisexe" value="${env.ProgramFiles}/NSIS/makensis.exe"/>
    </target>

    <!-- Win64 makensis -->
    <target name="nsisbin-3" depends="init" if="env.ProgramFiles(x86)">
        <property name="nsisbin" value="${env.ProgramFiles(x86)}/NSIS/makensis.exe"/>
    </target>

    <!-- As seen with signing jars, tsaurl attribute cannot be empty, so separate methods are needed to sign both exes -->
    <target name="sign-exe-self" depends="build-exe" unless="signing.tsaurl">
        <taskdef name="signexe" classname="net.jsign.PESignerTask" classpath="${basedir}/ant/lib/jsign-1.2.jar"/>

        <echo>Signing Windows Executable: No tsaurl was provided so this exe was not timestamped. Users will not be able to validate this exe after the signer certificate's expiration date or after any future revocation date.</echo>
        <signexe file="${out.dir}/${build.socket.name}-${build.version}.exe"
            name="${socket.name}"
            url="${vendor.website}"
            keystore="${signing.keystore}"
            alias="${signing.alias}"
            storepass="${signing.storepass}"
            keypass="${signing.keypass}"
            />
    </target>

    <target name="sign-exe-tsa" depends="build-exe" if="signing.tsaurl">
        <taskdef name="signexe" classname="net.jsign.PESignerTask" classpath="${basedir}/ant/lib/jsign-1.2.jar"/>

        <echo>Signing Windows Executable:</echo>
        <signexe file="${out.dir}/${build.socket.name}-${build.version}.exe"
            name="${socket.name}"
            url="${vendor.website}"
            keystore="${signing.keystore}"
            alias="${signing.alias}"
            storepass="${signing.storepass}"
            keypass="${signing.keypass}"          
            tsaurl="${signing.tsaurl}"
            />
    </target>

    <!--
    ################################################################
    #                     Apple Installer                          #
    ################################################################
    -->
    <target name="pkgbuild" depends="distribute,prepackage">
        <echo>Creating installer using pkgbuild</echo>

        <property file="ant/apple/apple.properties"/>

        <!--
        ###################################
        #   Build MacOS Bundle Structure  #
        ###################################
        -->

        <!-- Contents/Resources/apple-icon.icns -->
        <mkdir dir="${dist.socket.dir}/${apple.resources}"/>
        <copy file="${branding.dir}/${apple.icon}" tofile="${dist.socket.dir}/${apple.resources}/${apple.icon}" />

        <!-- Contents/MacOS/QZ\ Tray -->
        <mkdir dir="${dist.socket.dir}/${apple.macos}"/>
        <copy file="${apple.launcher.in}" tofile="${apple.launcher.out}">
            <filterchain><expandproperties/></filterchain>
        </copy>
        <chmod perm="+x" file="${apple.launcher.out}"/>

        <!-- Info.plist -->
        <copy file="${apple.plist.in}" tofile="${apple.plist.out}">
            <filterchain><expandproperties/></filterchain>
        </copy>

        <copy file="${apple.packager.in}" tofile="${apple.packager.out}">
            <filterchain><expandproperties/></filterchain>
        </copy>
        <chmod perm="u+x" file="${apple.packager.out}"/>

        <mkdir dir="${apple.scripts}"/>
        <copy file="${apple.keygen.in}" tofile="${apple.keygen.out}">
            <filterchain><expandproperties/></filterchain>
        </copy>
        <chmod perm="u+x" file="${apple.keygen.out}"/>

        <copy file="${apple.preinstall.in}" tofile="${apple.preinstall.out}">
            <filterchain><expandproperties/></filterchain>
        </copy>
        <chmod perm="u+x" file="${apple.preinstall.out}"/>

        <copy file="${apple.postinstall.in}" tofile="${apple.postinstall.out}">
            <filterchain><expandproperties/></filterchain>
        </copy>
        <chmod perm="u+x" file="${apple.postinstall.out}"/>

        <copy file="${apple.uninstall.in}" tofile="${apple.uninstall.out}">
            <filterchain><expandproperties/></filterchain>
        </copy>
        <chmod perm="u+x" file="${apple.uninstall.out}"/>

        <copy file="${firefoxcert.in}" tofile="${firefoxcert.out}">
            <filterchain><expandproperties/></filterchain>
        </copy>
        <chmod perm="u+x" file="${firefoxcert.out}"/>

        <copy file="${locator.in}" tofile="${locator.out}" >
            <filterchain><expandproperties/></filterchain>
        </copy>
        <chmod perm="u+x" file="${locator.out}"/>

        <exec executable="${apple.packager.out}" failonerror="true" />

        <!-- Cleanup lingering resources for other installer targets -->
        <delete dir="${dist.socket.dir}/${apple.resources}" />
        <delete dir="${dist.socket.dir}/${apple.macos}" />
        <delete file="${dist.socket.dir}/${apple.plist.out}" />
    </target>

    <!--
    ################################################################
    #                     Linux Installer                          #
    ################################################################
    -->
    <target name="makeself" depends="distribute,prepackage">
        <echo>Creating installer using makeself</echo>

        <property file="ant/linux/linux.properties"/>
        <copy file="${branding.dir}/${linux.icon}" tofile="${dist.socket.dir}/${linux.icon}" />

        <copy file="${linux.installer.in}" tofile="${linux.installer.out}" >
            <filterchain><expandproperties/></filterchain>
        </copy>

        <copy file="${linux.keygen.in}" tofile="${linux.keygen.out}" >
            <filterchain><expandproperties/></filterchain>
        </copy>

        <copy file="${linux.packager.in}" tofile="${linux.packager.out}" >
            <filterchain><expandproperties/></filterchain>
        </copy>

        <chmod perm="u+x" type="file">
            <fileset dir="${build.dir}">
                 <include name="**/*.sh"/>
            </fileset>
        </chmod>

        <copy file="${firefoxcert.in}" tofile="${firefoxcert.out}">
            <filterchain><expandproperties/></filterchain>
        </copy>
        <chmod perm="u+x" file="${firefoxcert.out}"/>

	<copy file="${locator.in}" tofile="${locator.out}" >
            <filterchain><expandproperties/></filterchain>
        </copy>
        <chmod perm="u+x" file="${locator.out}"/>

        <exec executable="${linux.packager.out}" failonerror="true" />

        <!-- Cleanup lingering icon (for other OS installers) -->
        <delete file="${dist.socket.dir}/${linux.icon}" />
    </target>
</project>
