<?xml version="1.0"?>
<project name="sms-tools" default="run" basedir=".">
   <description>simple sms-tools ant build file </description>
   
	<import file="../../../morpho/build.xml" />
	
	<!-- override the morpho property -->
   	<property name="morpho.args" value=".semtools"/>
	<property name="release" value="1.10.0semtools4"/>
	
   <!-- set global properties for this build -->
   <property name="src" location="src"/>
   <property name="build" location="build"/>
   <property name="dist"  location="dist"/>
   <property name="config"  location="config"/>
   <property name="infile"  value="examples/test-annotation1.xml"/>
	
	<!-- for getting datamanager -->
	   <property name="emldir"  value="${morpho.basedir}/../eml"/>
	   <property name="svn.emlUrl"  value="https://code.ecoinformatics.org/code/eml/trunk/"/>
	
	<!-- for building metacat -->
	<property name="metacat.dir"  value="${morpho.basedir}/../metacat"/>
	
   <target name="init" depends="morpho.init">
      <!-- Create the time stamp -->
      <tstamp/>
      <!-- Create the build directory structure used by compile -->
      <mkdir dir="${build}" />
      <!-- Create the config sub-directory -->
      <mkdir dir="${build}/config"/>
      <copy todir="${build}/config">
         <fileset dir="${config}">
            <include name="**"/>
         </fileset>
      </copy>
   	  <condition property="eml.required">
			<or>
				<not>
					<available file="${emldir}" type="dir" />
				</not>
			</or>
		</condition>
   		<condition property="datamanager.required">
   				<or>
   					<not>
   						<available file="$./lib/datamanager.jar" />
   					</not>
   				</or>
   			</condition>
   </target>

   <target name="setclasspath" depends="datamanager">
      <path id="project.class.path">
         <pathelement location="./"/>
         <pathelement location="./configs"/>
         <pathelement location="./lib"/>
         <pathelement location="./build"/>
         <fileset dir="lib">
            <include name="**/*.jar"/>
         </fileset>
      	<!-- morpho jars -->
      	<path path="${cpath}" />
      	<!-- metacat jars -->
      	<fileset dir="${metacat.dir}/build">
            <include name="*.jar"/>
      	</fileset>
      </path>
      <property name="classpath" refid="project.class.path"/>
   </target>

	<target name="prepare-lib" depends="morpho.prepare-lib">
		<copy todir="${build.lib}" filtering="no" file="./lib/config.xml" overwrite="true" />
		<copy todir="${build.lib}" filtering="no" file="./src/log4j.properties" overwrite="true" />
		<copy todir="${build.lib}" filtering="no" overwrite="true" >
			<fileset dir="./lib">
			    <include name="**/*"/>
			    <exclude name="*.jar"/>
			</fileset>
		</copy>

	</target>
	
   <target name="compile" depends="morpho.compile,init,setclasspath"
        description="compile the source " >
   		<echo message="Compiling semtools with classpath: ${classpath}"></echo>
      <!-- Compile the java code from ${src} into ${build} -->
      <javac srcdir="${src}" 
           destdir="${build}"
           classpath="${classpath}"
           debug="on"
           source="1.5"
           deprecation="true"/>
   		<!-- copy the image resources we are using -->
		<copy todir="${build}" filtering="no">
			<fileset dir="${src}">
				<include name="**/*.gif" />
				<include name="**/*.png" />
				<include name="**/*.jpg" />
			</fileset>
		</copy>
   		<!-- include configuration files, also -->
   		<copy todir="${build}" filtering="no">
			<fileset dir="${src}">
				<include name="**/*.xml" />
			</fileset>
		</copy>
   </target>

   <target name="test" depends="compile">
      <java classname="org.ecoinformatics.sms.Test" classpath="${classpath}" fork="yes">
         <arg value="http://linus.nceas.ucsb.edu/sms/metacat/ont.3.1"/>
         <arg value="http://linus.nceas.ucsb.edu/sms/metacat/ont.7.1"/>
         <jvmarg value="-DSMS=${pwd}"/>
         <jvmarg value="-Xmx1024m"/>
         <jvmarg value="-Xss5m"/>
      </java>
   </target>

   <target name="test2" depends="compile">
      <java classname="org.ecoinformatics.sms.Test2" classpath="${classpath}" fork="yes">
         <jvmarg value="-DSMS=${pwd}"/>
         <jvmarg value="-Xmx1024m"/>
         <jvmarg value="-Xss5m"/>
      </java>
   </target>

   <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
      <mkdir dir="${dist}/lib"/>
    <!-- Put everything in ${build} into the SMSt-${DSTAMP}.jar file -->
      <jar jarfile="${dist}/lib/sms-tools-${DSTAMP}.jar" basedir="${build}"/>
   </target>

   <target name="run" depends="compile">
      <echo>Running with classpath: ${classpath}:${build}</echo>
      <java classname="org.ecoinformatics.sms.annotation.Annotation"
          classpath="${classpath}:${build}"
          fork="yes">
         <arg value="${infile}"/>
         <jvmarg value="-DSMS=${pwd}"/>
         <jvmarg value="-Xmx512m"/>
         <jvmarg value="-Xss5m"/>
      </java>
   </target>

   <target name="jar" depends="morpho.jar,compile">
      <jar jarfile="sms.jar" >
         <fileset dir="${build}"/>
        <fileset dir="${src}"/>
      </jar>
   </target>

	<!-- we need runtime jars in the morpho build somehow -->
	<target name="install" depends="morpho.install">
	      <copy todir="${build.lib}" >
	      	<fileset dir="./lib" includes="*.jar" excludes="saxon9he.jar"/>	      	
	      </copy>
			<copy todir="${build.lib}" file="${basedir}/sms.jar" overwrite="true">
	      </copy>
	      <echo message="Install complete. Added SMS jars to lib. Use the morpho script to run it." />
	   </target>
	
	   <target name="semtools" depends="install">
	     <java classname="edu.ucsb.nceas.morpho.Morpho"
	           fork="yes" dir="${morpho.basedir}" >
	     	<classpath>
	     		<path refid="classpath.runtime" />
	     		<pathelement path="${morpho}:${help}"/>
	     	</classpath>
	     	<arg line=".semtools"/>
	       <jvmarg value="-Xmx512m"/>
	       <jvmarg value="-Xss1m"/>
	       <jvmarg value="-Djava.protocol.handler.pkgs=HTTPClient"/>
	     </java>
	   </target>
	
	<target name="install-metacat">
		<echo message="Calling Metacat warPrepare"></echo>
		<ant dir="${metacat.dir}" target="warPrepare" inheritall="false"/>
		
		<echo message="Compiling and Building semtools jar"></echo>
		<antcall>
			<target name="jar"></target>
		</antcall>
		
		<echo message="Copying sms.jar to Metacat"></echo>
		<copy file="sms.jar" todir="${metacat.dir}/build/war/lib" overwrite="true"></copy>
		<echo message="Copying supporting semtools jars to Metacat"></echo>
		<copy todir="${metacat.dir}/build/war/lib">
			<fileset dir="./lib" includes="*.jar"/>	      	
		</copy>
		<!-- TODO: metacat.properties to include the plugin -->
		<copy todir="${metacat.dir}/build/war/WEB-INF/classes">
			<fileset dir="${emldir}/src" includes="*.properties" excludes="log4j.properties"/>	      	
		</copy>
		<!-- move semtools skin to metacat -->
		<copy todir="${metacat.dir}/build/war/knb/style/skins">
			<fileset dir="./lib/style/skins" includes="**/*"/>	      	
		</copy>
		
		<echo message="Continuing Metacat build"></echo>
		<ant dir="${metacat.dir}" target="install" inheritall="false"/>
		
	</target>
	
   <target name="javadoc" depends="run">
      <javadoc sourcepath="src"
             destdir="doc/javadoc"
	     classpath="${classpath}:${build}"
	     verbose="false"
	     maxmemory="256m"
	     source="1.5"
	     failonerror="true">
         <package name="org.*"/>
      </javadoc>
   </target>

   <target name="clean"
        description="clean up" depends="morpho.clean">
    <!-- Delete the ${build} and ${dist} directory trees -->
      <delete dir="${build}"/>
      <delete dir="${dist}"/>
   </target>
	
	   <target name="clean-all" description="clean up all sorts of stuff" depends="clean">
	      <delete file="./lib/datamanager.jar"/>
	   </target>
	
	<target name="datamanager" depends="geteml" if="datamanager.required">
		<ant dir="${emldir}" target="clean" inheritAll="false" />
		<ant dir="${emldir}" target="jar-datamanager-lib" inheritAll="false" />
		<copy file="${emldir}/build/datamanager.jar" todir="./lib" overwrite="true"/>
	</target>

	<target name="geteml" depends="init" if="eml.required"
		description="Checks eml out of svn and copies it to your eml dir if it does not already exist">
		<echo message="Exporting eml files from svn repository:" />
		<svn>
			<export srcUrl="${svn.emlUrl}"
				destPath="${emldir}" />
		</svn>

	</target>

</project>

