<!-- (jEdit options) :folding=explicit:collapseFolds=1: -->
<project name="king" default="dist" basedir=".">
<description>KiNG kinemage viewer</description>

<!-- global properties that we always want defined {{{ -->
<property name="src"        location="src"/>
<property name="resource"   location="resource"/>
<property name="build"      location="build"/>
<property name="dist"       location="dist"/>
<property name="installer"  location="installer"/>
<property name="javadev"    location=".."/>
<!-- Version control: creates timestamp, updates ${buildnum}, loads ${version} -->
<target name="init">
    <tstamp>
        <format property="DSTAMP_SIX" pattern="yyMMdd"/>
    </tstamp>
    <propertyfile file="${resource}/${ant.project.name}/buildnum.props">
        <entry key="buildnum" value="${DSTAMP}.${TSTAMP}"/>
    </propertyfile>
    <property file="${resource}/${ant.project.name}/buildnum.props"/>
    <property file="${resource}/${ant.project.name}/version.props"/>
    <!-- Conditional, platform-specific compilation -->
    <!--<condition property="isMacOSX">
        <and>
            <os family="mac"/>
            <os family="unix"/>
        </and>
    </condition>-->
</target>
<!-- }}} -->

<!-- build, compile, compile-mac, compile-jogl {{{ -->
<!-- Bundles up all needed class and resource files into a JAR -->
<target name="build" description="Build class files" depends="init,compile,compile-mac,compile-jogl">
    <ant antfile="../molikin/build.xml" inheritAll="false" target="compile">
        <!-- inherited properties go here -->
    </ant>
    <echo message="Version ${version}"/>
    <echo message="Build # ${buildnum}"/>
    <concat destfile="${resource}/king/king_prefs" append="false">
      <filelist dir="${resource}/king/" files="source_king_prefs"/>
      <filelist dir="../molikin/resource/king" files="king_prefs"/>
    </concat>
    <concat destfile="${resource}/META-INF/services/king.Plugin" append="false">
      <filelist dir="${resource}/META-INF/services/" files="source.king.Plugin"/>
      <filelist dir="../molikin/resource/META-INF/services/" files="king.Plugin"/>
    </concat>
    <!-- As of 1.5.2, Ant screws up the INDEX by not considering Class-Path -->
    <jar jarfile="plugins/${ant.project.name}.jar" manifest="Manifest.mf" index="false">
        <fileset dir="${build}"/>
        <fileset dir="${resource}"/>
        <fileset dir="../driftwood/build/">
           <!--<exclude name="driftwood/moldb*/"/>-->
        </fileset>
        <fileset dir="../driftwood/resource/">
           <!--<exclude name="driftwood/moldb*/"/>-->
        </fileset>
        <fileset dir="../molikin/build/"/>
        <fileset dir="../molikin/resource/">
          <exclude name="king/"/>
          <exclude name="META-INF/"/>
        </fileset>
    </jar>
    <copy file="resource/king/king_prefs" tofile="${basedir}/doc/dot_king_prefs.txt" overwrite="true"/>
</target>

<!-- Compiles all .java files for this package -->
<target name="compile" depends="init">
    <ant antfile="../driftwood/build.xml" inheritAll="false" target="compile">
        <!-- inherited properties go here -->
    </ant>
    <copy file="../lib/pdfbox/pdfbox-2.0.19.jar" tofile="${basedir}/pdfbox.jar" failonerror="false"/>
    <copy file="../lib/pdfbox/fontbox-2.0.19.jar" tofile="${basedir}/fontbox.jar" failonerror="false"/>
    <copy file="../lib/pdfbox/pdfbox-graphics2d/target/graphics2d-0.26-SNAPSHOT.jar" tofile="${basedir}/pdfbox-graphics2d.jar" failonerror="false"/>
    <copy file="../lib/pdfbox/commons-logging-1.2/commons-logging-1.2.jar" tofile="${basedir}/commons-logging-1.2.jar" failonerror="false"/>
    <mkdir dir="${build}"/>
    <javac srcdir="${src}" destdir="${build}" debug="on" debuglevel="lines,source" target="1.8" source="1.8">
         <include name="**/*.java"/>
         <exclude name="**/old/"/>
         <exclude name="**/Mac*.java"/>
         <exclude name="**/Jogl*.java"/>
         <exclude name="**/dive/"/>
         <classpath>
            <pathelement location="../driftwood/build"/>
            <pathelement location="pdfbox.jar"/>             <!-- Required for PDF export -->
            <pathelement location="fontbox.jar"/>
            <pathelement location="pdfbox-graphics2d.jar"/>
            <pathelement location="commons-logging-1.2.jar"/>
         </classpath>
    </javac>
</target>

<!-- Compiles .java files that only work on the Mac -->
<target name="compile-mac" depends="init,compile" if="isMacOSX">
    <javac srcdir="${src}" destdir="${build}" debug="on" debuglevel="lines,source" target="1.8" source="1.8">
         <include name="**/Mac*.java"/>
         <classpath>
            <pathelement location="${build}"/>
            <pathelement location="../driftwood/build"/>
         </classpath>
    </javac>
</target>

<!-- Compiles .java files that only work with JOGL installed -->
<target name="compile-jogl" depends="init,compile">
    <javac srcdir="${src}" destdir="${build}" debug="on" debuglevel="lines,source" failonerror="false" target="1.8" source="1.8">
         <include name="**/Jogl*.java"/>
         <include name="**/dive/"/>
         <classpath>
            <pathelement location="${build}"/>
            <pathelement location="../driftwood/build"/>
            <pathelement location="../lib/jogl2/jogl-all.jar"/>
            <pathelement location="../lib/jogl2/gluegen-rt.jar"/>
         </classpath>
    </javac>
</target>
<!-- }}} -->

<!-- dist, dist-src, dist-exe {{{ -->
<!-- Makes .zip files from the ${dist} directory -->
<target name="dist" description="Generate the distributions" depends="init,clean,build,jpackage-base,dist-src">
    <zip destfile="${dist}/${ant.project.name}.${version}.${DSTAMP_SIX}.src.zip">
        <zipfileset dir="${dist}" includes="${ant.project.name}/"/>
        <zipfileset dir="${dist}" includes="javadocs/"/>
        <!-- old version, incorporates version names which breaks ant when users try to compile them VBC 111230
        <zipfileset dir="../driftwood/dist/" includes="driftwood-*-src/"/>
        <zipfileset dir="../chiropraxis/dist/" includes="chiropraxis-*-src/"/>
        <zipfileset dir="../extratools/dist/" includes="extratools-*-src/"/>
        <zipfileset dir="../molikin/dist/" includes="molikin-*-src/"/> 
        -->
        <zipfileset dir="../driftwood/dist/" includes="driftwood/"/>
        <zipfileset dir="../chiropraxis/dist/" includes="chiropraxis/"/>
        <zipfileset dir="../extratools/dist/" includes="extratools/"/>
        <zipfileset dir="../molikin/dist/" includes="molikin/"/> 
        <zipfileset dir="../rdcvis/dist/" includes="rdcvis/"/>
        <zipfileset dir="../lib/" prefix="lib/">
            <include name="pdfbox/*.jar"/>
            <include name="pdfbox/pdfbox-graphics2d/target/graphics2d-0.26-SNAPSHOT.jar"/>
            <include name="pdfbox/commons-logging-1.2/commons-logging-1.2.jar"/>
            <include name="jogl2/*.jar"/>
            <include name="win64/*.jar"/>
            <include name="macosx/*.jar"/>
            <include name="linux64/*.jar"/>
        </zipfileset>
    </zip>
    <copy todir="plugins/" preservelastmodified="true" flatten="true">
        <fileset dir="${javadev}/lib/jogl2/">
            <include name="*/*.jar"/>
        </fileset>
        <fileset dir="${javadev}/lib/jogl2/">
            <include name="*.jar"/>
        </fileset>
    </copy>
    <copy todir="${dist}/${ant.project.name}-${version}" preservelastmodified="true">
        <fileset dir="plugins/">
            <include name="*.jar"/>
        </fileset>
    </copy>
    <copy todir="${dist}/${ant.project.name}-${version}" preservelastmodified="true">
        <fileset dir="${javadev}/lib/rlab/linux/">
            <include name="probe"/>
            <include name="suitename"/>
        </fileset>
    </copy>
    <zip basedir="${dist}"
         destfile="${dist}/${ant.project.name}.${version}.${DSTAMP_SIX}.zip"
         includes="${ant.project.name}-${version}/">
    </zip>
</target>

<!-- Makes the source code distribution -->
<target name="dist-src" depends="init">
    <ant antfile="../driftwood/build.xml" inheritAll="false" target="dist-src">
        <!-- inherited properties go here -->
    </ant>
    <ant antfile="../chiropraxis/build.xml" inheritAll="false" target="dist-src">
        <!-- inherited properties go here -->
    </ant>
    <ant antfile="../extratools/build.xml" inheritAll="false" target="dist-src">
        <!-- inherited properties go here -->
    </ant>
    <ant antfile="../molikin/build.xml" inheritAll="false" target="dist-src">
        <!-- inherited properties go here -->
    </ant>
    <!--
    <mkdir dir="${dist}/${ant.project.name}-${version}-src"/>
    <copy todir="${dist}/${ant.project.name}-${version}-src" preservelastmodified="true">
    -->
    <mkdir dir="${dist}/${ant.project.name}"/>
    <copy todir="${dist}/${ant.project.name}" preservelastmodified="true">
        <fileset dir="${basedir}">
            <exclude name="build/"/>
            <exclude name="dist/"/>
            <exclude name="**/old/"/>
        </fileset>
    </copy>
    <!-- docs for other top-level projects are in their dist/ folders already -->
    <mkdir dir="${dist}/javadocs"/>
    <javadoc packagenames="driftwood.*,Jama.*,king.*"
        sourcepath="src:../driftwood/src"
        classpath="../lib:../lib/pdfbox/pdfbox-2.0.19.jar:../lib/pdfbox/pdfbox-graphics2d/target/graphics2d-0.26-SNAPSHOT.jar:../lib/jogl2/jogl-all.jar"
        destdir="${dist}/javadocs" access="protected" source="1.8"
        breakiterator="true" noqualifier="all"
        version="true" use="true" author="true"
        windowtitle="IWD's JavaDocs"
        link="http://java.sun.com/j2se/1.8.0/docs/api/">
    </javadoc>
</target>

<!-- Makes the end-user distribution -->
<target name="dist-exe" depends="init,build">
    <copy file="${javadev}/chiropraxis/chiropraxis.jar" todir="plugins/" preservelastmodified="true"/>
    <copy file="${javadev}/extratools/extratools.jar" todir="plugins/" preservelastmodified="true"/>
    <copy file="${javadev}/rdcvis/rdcvis.jar" todir="plugins/" preservelastmodified="true"/>
    
    <mkdir dir="${dist}/${ant.project.name}-${version}"/>
    <copy todir="${dist}/${ant.project.name}-${version}" preservelastmodified="true">
        <fileset dir="${basedir}">
            <include name="*.jar"/>
            <include name="README*"/>
            <include name="LICENSE*"/>
            <include name="plugins/"/>
            <include name="doc/"/>
            <exclude name="doc/work/"/>
        </fileset>
    </copy>
    <!-- extra doc files from other top-level projects; some need to be renamed -->
    <copy file="../chiropraxis/doc/tools-manual.pdf" tofile="${dist}/${ant.project.name}-${version}/doc/chiropraxis-manual.pdf"/>
    <copy file="../extratools/doc/tools-manual.pdf" tofile="${dist}/${ant.project.name}-${version}/doc/extratools-manual.pdf"/>
</target>
<!-- }}} -->

<target name="jpackage-base">
    <delete dir="${dist}"/>
    <copy file="${javadev}/chiropraxis/chiropraxis.jar" todir="plugins/" preservelastmodified="true"/>
    <copy file="${javadev}/extratools/extratools.jar" todir="plugins/" preservelastmodified="true"/>
    <copy file="${javadev}/rdcvis/rdcvis.jar" todir="plugins/" preservelastmodified="true"/>
    <copy todir="plugins/" preservelastmodified="true">
        <fileset dir="${basedir}">
            <include name="*.jar"/>
        </fileset>
    </copy>

    <mkdir dir="${dist}/${ant.project.name}-${version}"/>
    <copy todir="${dist}/${ant.project.name}-${version}" preservelastmodified="true">
        <fileset dir="${basedir}">
            <include name="README*"/>
            <include name="LICENSE*"/>
            <include name="doc/"/>
            <exclude name="doc/work/"/>
        </fileset>
    </copy>
    <!-- extra doc files from other top-level projects; some need to be renamed -->
    <copy file="../chiropraxis/doc/tools-manual.pdf" tofile="${dist}/${ant.project.name}-${version}/doc/chiropraxis-manual.pdf"/>
    <copy file="../extratools/doc/tools-manual.pdf" tofile="${dist}/${ant.project.name}-${version}/doc/extratools-manual.pdf"/>
</target>

<target name="jpackage-win" depends="init,build,jpackage-base">
    <copy todir="plugins/" preservelastmodified="true">
        <fileset dir="${javadev}/lib/jogl2/win64/">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${javadev}/lib/jogl2/">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${javadev}/lib/rlab/win/">
            <include name="probe.exe"/>
            <include name="suitename.exe"/>
            <include name="msvcr71.dll"/>
        </fileset>
    </copy>
    <exec executable="jpackage.exe">
        <arg line="--input .\plugins\"/>
        <arg line="--name KiNG"/>
        <arg value="--verbose"/>
        <arg line="--app-version ${version}"/>
        <arg value="--description"/>
        <arg value="Kinemage next generation viewer"/>
        <arg line="--icon ${resource}\king\images\KiNG.ico"/>
        <arg line="--main-jar king.jar"/>
        <arg value="--win-dir-chooser"/>
        <arg value="--win-menu"/> 
        <arg value="--vendor"/>
        <arg value="Richardson Lab"/>
        <arg line="--license-file .\LICENSE.txt"/>
        <arg line="--dest ${dist}/${ant.project.name}-${version}/"/>
    </exec>
    <zip destfile="${dist}/king.${version}.${DSTAMP_SIX}.win64.zip" basedir="${dist}/${ant.project.name}-${version}/"/>
</target>

<target name="jpackage-mac" depends="init,build,jpackage-base">
    <copy todir="plugins/" preservelastmodified="true">
        <fileset dir="${javadev}/lib/jogl2/macosx/">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${javadev}/lib/jogl2/">
            <include name="*.jar"/>
        </fileset>
    </copy>
    <exec executable="cp" failonerror="true">
        <arg line="${javadev}/lib/rlab/mac/probe plugins/"/>
    </exec>
    <exec executable="cp" failonerror="true">
        <arg line="${javadev}/lib/rlab/mac/suitename plugins/"/>
    </exec>
    <exec executable="jpackage">
        <arg line="--input plugins/"/>
        <arg line="--name KiNG"/>
        <arg value="--verbose"/>
        <arg line="--app-version ${version}"/>
        <arg value="--description"/>
        <arg value="Kinemage next generation viewer"/>
        <arg line="--icon ${resource}/king/images/KiNG.icns"/>
        <arg line="--main-jar king.jar"/>
        <arg value="--vendor"/>
        <arg value="Richardson Lab"/>
        <arg line="--license-file ./LICENSE.txt"/>
        <arg line="--dest ${dist}/${ant.project.name}-${version}/"/>
    </exec>
    <copy file="${dist}/${ant.project.name}-${version}/KiNG-${version}.dmg" tofile="${dist}/${ant.project.name}-${version}/king.${version}.${DSTAMP_SIX}.macosx.dmg" preservelastmodified="true"/>
</target>

<!-- bug on mojave: jogl doesn't work and crashes the app:
NSWindow drag regions should only be invalidated on the Main Thread!
https://groups.google.com/g/jaamsim-users/c/JGqmj9CRXcU/discussion-->
<target name="jpackage-mac-nojogl" depends="init,build,jpackage-base">
    <exec executable="cp" failonerror="true">
        <arg line="${javadev}/lib/rlab/mac/probe plugins/"/>
    </exec>
    <exec executable="cp" failonerror="true">
        <arg line="${javadev}/lib/rlab/mac/suitename plugins/"/>
    </exec>
    <exec executable="jpackage">
        <arg line="--input plugins/"/>
        <arg line="--name KiNG"/>
        <arg value="--verbose"/>
        <arg line="--app-version ${version}"/>
        <arg value="--description"/>
        <arg value="Kinemage next generation viewer"/>
        <arg line="--icon ${resource}/king/images/KiNG.icns"/>
        <arg line="--main-jar king.jar"/>
        <arg value="--vendor"/>
        <arg value="Richardson Lab"/>
        <arg line="--license-file ./LICENSE.txt"/>
        <arg line="--dest ${dist}/${ant.project.name}-${version}/"/>
    </exec>
    <copy file="${dist}/${ant.project.name}-${version}/KiNG-${version}.dmg" tofile="${dist}/${ant.project.name}-${version}/king.${version}.${DSTAMP_SIX}.macosx.dmg" preservelastmodified="true"/>
</target>

<target name="jpackage-nix" depends="init,build,jpackage-base">
    <copy todir="plugins/" preservelastmodified="true">
        <fileset dir="${javadev}/lib/jogl2/linux64/">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${javadev}/lib/jogl2/">
            <include name="*.jar"/>
        </fileset>
    </copy>
    <exec executable="jpackage">
        <arg line="--input plugins/"/>
        <arg line="--name KiNG"/>
        <arg value="--verbose"/>
        <arg line="--app-version ${version}"/>
        <arg value="--description"/>
        <arg value="Kinemage next generation viewer"/>
        <arg line="--icon ${resource}/king/images/KiNG.icns"/>
        <arg line="--main-jar king.jar"/>
        <arg value="--vendor"/>
        <arg value="Richardson Lab"/>
        <arg line="--license-file ./LICENSE.txt"/>
        <arg value="--linux-shortcut"/>
        <arg line="--dest ${dist}/${ant.project.name}-${version}/"/>
    </exec>
</target>

<taskdef name="bundleapp"
         classname="com.oracle.appbundler.AppBundlerTask"
         classpath="../lib/appbundler/appbundler-1.0ea.jar" />

<property environment="env" />         
         
<!--<target name="mac-app" depends="dist">-->
<target name="mac-app">
    <bundleapp outputdirectory="${dist}"
        name="KiNG"
        displayname="KiNG"
        identifier="edu.duke.biochem.kinemage.king"
        executableName="king"
        shortversion="${version}"
        version="${version}.${DSTAMP_SIX}"
        icon="work/KiNG.icns"
        mainclassname="king.KingMain">
        <runtime dir="${env.JAVA_HOME}" />
        <classpath file="king.jar">
            <include name="plugins/**" />
        </classpath>
        <bundledocument extensions="pdb,pdb1,ent,pqr"
          icon="work/mageosX.icns"
          name="PDB Molecular structure"
          role="editor"
          handlerRank="alternate">
        </bundledocument>
        <bundledocument contentTypes="edu.duke.biochem.kinemage.kinemage"
          name="Kinemage file"
          role="editor"
          handlerRank="owner">
        </bundledocument>
        <typedeclaration
          identifier="edu.duke.biochem.kinemage.kinemage"
          description="Kinemage file"
          icon="work/mageosX.icns"
          conformsTo="public.text"
          extensions="kin,kip"
          mimeTypes="text/plain" />
          
        <option value="-Xdock:name=KiNG"/>
      </bundleapp>
    <!-- do some stupid manual moving of plugin files since bundleapp doesn't seem to be able to copy over the plugin directory -->
    <mkdir dir="${dist}/KiNG.app/Contents/Java/plugins"/>
    <move todir="${dist}/KiNG.app/Contents/Java/plugins">
        <fileset dir="${dist}/KiNG.app/Contents/Java">
            <include name="**/*.jar"/>
            <exclude name="**/king.jar"/>
        </fileset>
    </move>
    <!-- copy probe and suitename executables to app -->
    <!--<copy todir="${dist}/KiNG.app/Contents/Java">
        <fileset dir="../lib/rlab/mac"/>
    </copy>-->
    <exec executable="cp">
        <arg line="${javadev}/lib/rlab/mac/probe ${javadev}/lib/rlab/mac/suitename ${dist}/KiNG.app/Contents/Java"/>
    </exec>
    <exec executable="cp">
        <arg line="${javadev}/lib/jogl2/jogl-all.jar ${javadev}/lib/jogl2/macosx/jogl-all-natives-macosx-universal.jar ${javadev}/lib/jogl2/gluegen-rt.jar ${javadev}/lib/jogl2/macosx/gluegen-rt-natives-macosx-universal.jar ${dist}/KiNG.app/Contents/Java"/>
    </exec>
    <!-- add lines to Info.plist to enable high-resolution
    <replace file="${dist}/KiNG.app/Contents/Info.plist">
        <replacetoken><![CDATA[<string>king.KingMain</string>]]></replacetoken>
        <replacevalue><![CDATA[<string>king.KingMain</string>
<key>NSHighResolutionCapable</key>
<true/>]]></replacevalue>
    </replace>-->
</target>

<!-- clean, backup {{{ -->
<!-- Removes products of compilation -->
<target name="clean" description="Clean up build/ and dist/">
    <delete dir="${dist}"/>
    <delete dir="${build}"/>
    <delete dir="${basedir}/installer/king/king_Build_Output"/>
    <delete file="${ant.project.name}.jar"/>
    <delete file="itext.jar"/>
    <delete file="pdfbox.jar"/>
    <delete file="fontbox.jar"/>
    <delete file="pdfbox-graphics2d.jar"/>
    <delete file="commons-logging-1.2.jar"/>
    <delete includeemptydirs="true">
        <fileset dir="plugins/">
            <exclude name="beanking.jar"/>
        </fileset>
    </delete>

</target>

<!-- Also cleans driftwood -->
<target name="clean-all" depends="clean" description="Clean up king and driftwood">
    <ant antfile="../driftwood/build.xml" inheritAll="false" target="clean">
        <!-- inherited properties go here -->
    </ant>
    <ant antfile="../molikin/build.xml" inheritAll="false" target="clean">
        <!-- inherited properties go here -->
    </ant>
</target>
<!-- }}} -->

<!-- deploy-local {{{ -->
<!-- Deploys the executable on the local system - applies to IWD's machine only  -->
<target name="deploy-local"  description="Deploy to ~/bin" depends="build">
    <copy file="${ant.project.name}.jar" todir="/Applications/KiNG.app/Contents/Resources/Java/" overwrite="true" failonerror="false"/>
    <copy todir="/Applications/KiNG.app/Contents/Resources/Java/plugins/" overwrite="true" failonerror="false">
        <fileset dir="plugins/"/>
    </copy>
</target>

<!-- Deploys the executable on the local MolProbity - applies to IWD's machine only  -->
<target name="deploy-local-mp"  description="Deploy to local MolProbity 3" depends="build">
    <copy file="${ant.project.name}.jar" todir="${user.home}/Sites/molprobity3/public_html/" overwrite="true" failonerror="false"/>
</target>
<!-- }}} -->
</project>
