<!-- (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="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="${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>
        <!-- iText library is in a separate JAR -->
    </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/iText/itext-1.02b.jar" tofile="${basedir}/itext.jar" failonerror="false"/>
    <mkdir dir="${build}"/>
    <javac srcdir="${src}" destdir="${build}" debug="on" debuglevel="lines,source" target="1.7" source="1.7">
         <include name="**/*.java"/>
         <exclude name="**/old/"/>
         <exclude name="**/Mac*.java"/>
         <exclude name="**/Jogl*.java"/>
         <exclude name="**/dive/"/>
         <classpath>
            <pathelement location="../driftwood/build"/>
            <pathelement location="itext.jar"/>             <!-- Required for PDF export -->
         </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.7" source="1.7">
         <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,dist-src,dist-exe,mac-app">
    <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="../lib/" prefix="lib/">
            <include name="iText/*.jar"/>
            <include name="jogl2/*.jar"/>
        </zipfileset>
    </zip>
    <zip basedir="${dist}"
         destfile="${dist}/${ant.project.name}.${version}.${DSTAMP_SIX}.zip"
         includes="${ant.project.name}-${version}/"/>
</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/iText/itext-1.02b.jar:../lib/jogl/jogl.jar"
        destdir="${dist}/javadocs" access="protected" source="1.7"
        breakiterator="true" noqualifier="all"
        version="true" use="true" author="true"
        windowtitle="IWD's JavaDocs"
        link="http://java.sun.com/j2se/1.7.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>
<!-- }}} -->

<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"/>
</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 file="itext.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>
