<?xml version = "1.0" encoding = "utf-8"?>
<project name = "Watson plugin project" default = "install" basedir = ".">    
   <!-- 
        To run this build file set the environment variable
        PROTEGE_HOME to point to a protege distribution and type ant
        install or jar.
     -->

   <property environment="env"/>
   <property name = "protege.home"      location="${env.PROTEGE_HOME}"/>


   <!-- 
        these can be overridden in the local.properties for different
        distributions 
     -->
   <property file="local.properties"/>
   <property name = "protege.common"    location="${protege.home}/bundles"/>
   <property name = "protege.plugins"   location="${protege.home}/plugins"/>
   <property name = "protege.osgi"      location="${protege.home}/org.eclipse.osgi.jar"/>


   <!-- ============================================================= -->
   <!-- 
        This section may need to be customized by the developer for a 
        particular project 
     -->

   <!-- 
        This setting defines the name of the plugin.
        This may be the only setting that the developer needs to
        change.
     -->
   <property name = "plugin"          value = "org.imdea.watson"/>

   
   <!-- 
        Here is the copy.resources target.  It may need modification
        to copy the right resources into the classes directory.  By
        default it already copies non-java files found in the source
        directory, the libraries needed by the project and the
        viewconfig and the plugin.xml.  This will be sufficient in
        many cases. 
     -->
   <target name="copy.resources" depends="build.manifest">
     <copy todir="${classes}">
       <fileset dir="${src}">
         <include name="**/*"/>
         <exclude name="**/*.java"/>
         <exclude name="**/MANIFEST.MF"/>
         <exclude name="**/manifest.mf"/>
       </fileset>
       <fileset dir="${src}/../lib/all/">
         <include name="**/*"/>
         <include name="*"/>
       </fileset>
     </copy>
     <copy todir="${classes}">
       <fileset dir="." includes="*.xml">
           <exclude name="build.xml"/>
       </fileset>
     </copy>
   	 <!-- the manifest doesn't belong here but this is good for IDE's -->
     <mkdir dir="${classes}/META-INF"/>  
     <copy todir="${classes}/META-INF"   
           file = "${manifest}"/>
   </target>


   <!-- 
        The following target only needs to be modified if the
        developer needs to obtain some jar files that are contained in
        the Protege bundles.  The contents of these jar files are
        found when Protege 4 runs but may be needed in order to
        compile the plugin. 
     -->
   <target name = "buildlibs" depends="init, checkProtegeLibsAndReport">
       <unjar dest="${build}"
              src="${common.lib}">
           <patternset>
               <include name = "**/log4j.jar"/>
           </patternset>
       </unjar>
   </target>



   <!-- 
        The following target only needs to be modified if the user
        needs to change the classpath.  It is preconfigured to use
        the common protege 4 jars, the lib directory and the
        libraries that have been uncovered in buildlibs. 
     -->
   <target name = "checkProtegeLibs">
     <echo message="Using Protege Home = ${protege.home} to find protege jars"/>
     <condition property="libs.found">
       <and>
	 <available file="${equinox.common}" type = "file"/>
	 <available file="${equinox.registry}" type = "file"/>
	 <available file="${owl.editor.jar}" type = "file"/>
         <available file="${owl.lib}" type="file"/>
       </and>
     </condition>
     <path id = "project.classpath">    
       <pathelement location = "${protege.lib}"/>
       <pathelement location="${equinox.common}"/>
       <pathelement location="${equinox.registry}"/>
       <pathelement location="${owl.editor.jar}"/>
       <pathelement location="${owl.lib}"/>
       <pathelement location="lib/all.jar"/>
       <fileset dir="${genlib}"/>
     </path>
   </target>


   <!-- 
        It is less likely that the developer will want to make changes
        below this line 
        -->
   <!-- ============================================================= -->

   
   <property name="protege.home"      location="${env.PROTEGE_HOME}"/>
   <property name = "src"             location = "./src"/>  
   <property name = "build"           location = "./build"/>
   <property name = "classes"         location = "${build}/classes"/>
   <property name = "lib"             location = "./lib"/>
   <property name = "genlib"          location = "${build}/lib"/>
   <property name = "manifest"        location = "${build}/manifest.mf"/>

   <!-- 
        This is the section that changes for different distributions.
        It would only need small changes for knopflerfish or felix.
     -->

   <property name="osgi"
             location="${protege.home}/org.eclipse.osgi.jar"/>
   <property name="equinox.common" 
             location="${protege.common}/org.eclipse.equinox.common.jar"/>
   <property name="equinox.registry"
             location="${protege.common}/org.eclipse.equinox.registry.jar"/>
   <property name="protege.lib" 
             location="${protege.common}/org.protege.editor.core.application.jar"/>
   <property name="common.lib"
             location="${protege.common}/org.protege.common.jar"/>

   <property name="owl.lib"
             location="${protege.plugins}/org.semanticweb.owl.owlapi.jar"/>

   <property name="owl.editor.jar"
	     location="${protege.plugins}/org.protege.editor.owl.jar"/>

   <!-- end of equinox specific settings -->


   <target name = "usage">
     <echo message = "To run this script set the PROTEGE_HOME environment"/>
     <echo message = "variable and use one of the following targets"/>
     <echo message = "jar - builds the jar (bundle) file for this project"/>
     <echo message = "install - installs  the bundle into the Protege distribution"/>
     <echo message = "copy.resources - copyies resources into the classes directory"/>
     <echo message = "    this can  be useful for ide developers - see the wiki"/>
   </target>

   <target name = "init">
     <tstamp>
       <format property="build.time" pattern="MM/dd/yyyy hh:mm aa"/>
     </tstamp>
     <mkdir dir = "${build}"/>
     <mkdir dir = "${classes}"/>
     <mkdir dir = "${classes}/lib"/>
     <mkdir dir = "${genlib}"/>
   </target>  
   	
   <target name="checkProtegeHome">
       <condition property="protege.home.set">
           <available file="${protege.home}/org.protege.editor.core.application.jar" type="file"/>
       </condition>
   </target>

    <target name="checkProtegeLibsAndReport" depends="checkProtegeLibs"
            unless="libs.found">
      <echo message="Missing protege libraries.  You need to set "/>
      <echo message="the PROTEGE_HOME environment variable to a"/>
      <echo message="protege installation directory where the"/>
      <echo message="appropriate plugins have been installed."/>
      <echo message="Alternatively set the jar libs in local.properties (protege.lib=...)"/>
      <echo message="Use the -v option to ant to see what jars are missing."/>
      <fail message = "missing protege libraries"/>
    </target>
      

   <target name = "compile" depends = "buildlibs, checkProtegeLibsAndReport">
     <javac srcdir = "${src}"
            destdir = "${classes}" 
            debug="on"
            includeAntRuntime="false">
       <classpath refid = "project.classpath"/>  
     </javac>
   </target>

   <target name="build.manifest">
     <copy tofile="${manifest}" 
           file="META-INF/MANIFEST.MF" overwrite="true"/>
     <manifest file="${manifest}" 
               mode = "update">
        <attribute name="Built-By" value = "${user.name}"/>
        <attribute name="Build-Date" value = "${build.time}"/>
     </manifest>
   </target>

   <target name = "jar" depends = "compile, copy.resources">
     <jar jarfile = "${build}/${plugin}.jar"
          basedir = "${classes}" 
          manifest = "${build}/manifest.mf"/>
   </target>

   <target name = "install" depends = "jar">
     <!-- flush cache -->
     <delete dir = "${protege.home}/configuration/org.eclipse.core.runtime"/>
     <delete dir = "${protege.home}/configuration/org.eclipse.osgi"/>
     <copy file="${build}/${plugin}.jar" 
           todir = "${protege.plugins}"
           overwrite = "true"/>
   </target>
	
   <target name = "clean">
     <delete dir = "${build}"/>
   </target>

</project>
