<?xml version="1.0"?>

<project default="compile" basedir=".">
	<property name="project.dir" value="${basedir}"/> 
    <property name="property.file" value="mobyService.properties"/>        
    <description>
        This is an Ant build file for creating and registering a Moby Service.  
        The general usage pattern is:

        1. Write your service and test data 
           (see http://biomoby.open-bio.org/CVS_CONTENT/moby-live/Java/docs/deployingServices.html for details)
        2. Modify mobyService.properties according to the location of your code and servlet container (e.g. Apache Tomcat)
        3. ant test
        4. ant war
        5. Deploy the WAR file to your servlet container
        6. ant registerService
__________________________________________________________
    </description>

    <property name="description" value="[ MobyService ]"/>
	
    <property name="build.dir" value="${project.dir}${file.separator}build"/>        
    <property name="build.classes.dir" value="${build.dir}${file.separator}classes"/>        	
		
	
	    <condition property="windows">
 	        <os family="windows" /> 	
	    </condition>
	    <condition property="unix">
	        <os family="unix" />
	    </condition>
 	    <condition property="solaris">
 	        <os name="SunOS"/>
 	    </condition>
 	    <condition property="linux">
 	        <os name="Linux"/>
 	    </condition>
	    <condition property="mac">
 	        <os name="Mac OS X"/>
 	    </condition>
	
	 
	    <condition property="build.property.file" 
 	               value="windowsOS.properties">
 	        <isset property="windows"/>
 	    </condition>
		<condition property="build.property.file" 
 	               value="sunOS.properties">
 	        <isset property="solaris"/>
 	    </condition>
	
		<condition property="file.separator.to.use" 
 	               value="${file.separator}${file.separator}">
 	        <isset property="windows"/>
 	    </condition>
		<condition property="file.separator.to.use" 
 	               value="${file.separator}">
 	        <isset property="solaris"/>
 	    </condition>
	
	
	
	<delete file="${build.dir}${file.separator}${build.property.file}" failonerror="false"/>
	<copy file="${property.file}" tofile="${build.dir}${file.separator}${build.property.file}"  failonerror="false"/>
	
	<replace file="${build.dir}${file.separator}${build.property.file}" token="//" value="@@@" summary="yes"/>
	<replace file="${build.dir}${file.separator}${build.property.file}" token="\" value="${file.separator.to.use}" summary="yes"/>
    <replace file="${build.dir}${file.separator}${build.property.file}" token="@@@" value="//" summary="yes"/>
	
   <!-- <property file="${build.dir}${file.separator}${property.file}"/>-->
	<property file="${build.dir}${file.separator}${build.property.file}"/>

	
    <property name="moby.servlet.war.file" value="${lib.dir}${file.separator}MobyServlet.war"/>        
    <!--<property name="remote.dir" value="http://biomoby.org/jmoby-jars/jars-archive"/>   --> 
    <property name="remote.dir" value="http://www.visualgenomics.ca/gordonp"/>    

    <path id="classpath">
        <pathelement location="${build.classes.dir}"/>                

        <!--pathelement path="${java.class.path}"/-->     

        <fileset dir="${lib.dir}">
            <include name="**${file.separator}*.war"/>
        </fileset>           
        <fileset dir="${lib.dir}">
            <include name="**${file.separator}*.jar"/>
        </fileset>           
    </path>
   
	
	<pathconvert property="my.servlet.war.file.name" >
	<path path="${my.servlet.war.file}"/>
           <chainedmapper>
	<flattenmapper/>
	    <packagemapper from="*.war" to="*"/>
	</chainedmapper>
	</pathconvert>


     <pathconvert property="moby.servlet.war.file.name" >
        <path path="${moby.servlet.war.file}"/>
        <chainedmapper>
	    <flattenmapper/>
        </chainedmapper>
     </pathconvert>

	
     <pathconvert property="tmp.file.name" >
        <path path="${main.class.file}"/>
        <chainedmapper>
           <flattenmapper/>
        </chainedmapper>
     </pathconvert>

     <pathconvert property="main.class.package" >
        <path path="${project.dir}${file.separator}${src.dir}${file.separator}${main.class.file}"/>
        <packagemapper from="${project.dir}${file.separator}${src.dir}${file.separator}*${file.separator}${tmp.file.name}" to="*"/>
     </pathconvert>

     <pathconvert property="main.class.dir" >
        <path path="${project.dir}${file.separator}${src.dir}${file.separator}${main.class.file}"/>
        <globmapper from="${project.dir}${file.separator}${src.dir}${file.separator}*${file.separator}${tmp.file.name}" to="*"/>
     </pathconvert>


     <pathconvert property="main.class" >
        <path path="${tmp.file.name}"/>
        <chainedmapper>
           <flattenmapper/>
           <packagemapper from="*.java" to="*"/>
        </chainedmapper>
     </pathconvert>

	
    <target name="init" description="(Internal) Initialize the build directory.">
        <echo message="${description} -- init build directory. "/>       
    	
        <mkdir dir="${build.classes.dir}"/>

  	   <copy file="${src.dir}${file.separator}log4j.properties" todir="${build.classes.dir}"  failonerror="false"/>

        <get src="${remote.dir}/${moby.servlet.war.file.name}" dest="${lib.dir}${file.separator}${moby.servlet.war.file.name}"  usetimestamp="true"/>

    </target>
    
    <target name="compile" depends="init" description="(User) Compiles your new Moby service code.">
        <echo message="${description} -- compile --> ${main.class.dir}${file.separator}${main.class} "/>                      
        <javac srcdir="${src.dir}" destdir="${build.classes.dir}" 
            includes="${main.class.dir}${file.separator}${main.class}.java"            
        	debug="on" debuglevel="lines, vars, and source"        	
            deprecation="${deprecation}"
			classpathref="classpath">
        </javac>    
    </target>               
	
    <target name="test" depends="compile" description="(User) Tests the processRequest() method of your new Moby service code (before deployment).">           
        <echo message="${description} -- execute --> ${main.class.package}.${main.class}"/>
        <java classname="${main.class.package}.${main.class}"         	
            dir="${build.classes.dir}" fork="true"
            classpathref="classpath" failonerror="true" >   
        	<sysproperty key="DEBUG" value="true"/>
        	<arg value="${main.class.package}.${main.class}"/>
        	<arg value="${input.data.file}"/>
        </java> 
    </target>        

    <target name="war" depends="compile, updateWebXML" description="(User) Wraps up your new code in a deployable WAR archive.">
        <echo message="${description} -- making WAR file --> ${my.servlet.war.file}"/>

	<delete file="${build.dir}${file.separator}${my.servlet.war.file}"/>

        <mkdir dir="${build.dir}${file.separator}WEB-INF"/>

        <copy todir="${build.dir}${file.separator}WEB-INF${file.separator}lib">
            <fileset dir="${lib.dir}">
               <exclude name="${moby.servlet.war.file.name}"/>
            </fileset>
        </copy>

        <copy todir="${build.dir}${file.separator}WEB-INF${file.separator}classes">
            <fileset dir="${build.classes.dir}"/>
        </copy>
 	
        <copy file="${moby.servlet.war.file}" tofile="${build.dir}${file.separator}${my.servlet.war.file}"/>

	<jar basedir="${build.dir}" destfile="${build.dir}${file.separator}${my.servlet.war.file}" 
		update="true" manifest="${build.dir}${file.separator}META-INF${file.separator}MANIFEST.MF" includes="WEB-INF${file.separator}**">
	</jar>			

	<jar basedir="${build.classes.dir}" destfile="${build.dir}${file.separator}${my.servlet.war.file}" 
		update="true" manifest="${build.dir}${file.separator}META-INF${file.separator}MANIFEST.MF" includes="**"/>

        <echo message="Your WAR archive (${build.dir}${file.separator}${my.servlet.war.file}) is now ready for deployment (e.g. onto an Apache Tomcat server)."/>
    </target>		

    <target name="testService" description="(User) Test the service (requires that the WAR file be deployed already)." >
        <echo message="${description} -- testing service  (did you remember to deploy the WAR?) --> ${servlet.host.url}/${my.servlet.war.file.name}"/>
<java jar="${build.dir}${file.separator}${my.servlet.war.file}"
                fork="true"
		classpathref="classpath" >
          <arg value="${servlet.host.url}/${my.servlet.war.file.name}"/>
          <arg value="${input.data.file}"/>
	</java>
    </target> 


    <target name="registerService" description="(User) Register the service at MOBY Central (requires that the WAR file be deployed already)." >
        <echo message="${description} -- registering Service --> ${servlet.host.url}/${my.servlet.war.file.name}  --X ${my.servlet.war.file.name}"/>
<java jar="${build.dir}${file.separator}${my.servlet.war.file}"
                fork="true"
		classpathref="classpath" >
          <arg value="${servlet.host.url}/${my.servlet.war.file.name}/"/>
          <arg value="${input.data.file}"/>
          <arg value="register"/>
	</java>
    </target> 

    <target name="registerServicePermanent" description="(User) Register the service at MOBY Central, with RDF signature (cannot be unregistered except by undeployment of servlet for several days)." >
        <echo message="${description} -- registering Service --> ${servlet.host.url}/${my.servlet.war.file.name}  --X ${my.servlet.war.file.name}"/>
<java jar="${build.dir}${file.separator}${my.servlet.war.file}"
                fork="true"
		classpathref="classpath" >
          <arg value="${servlet.host.url}/${my.servlet.war.file.name}/"/>
          <arg value="${input.data.file}"/>
          <arg value="register_permanent"/>
	</java>
    </target> 

    <target name="unregisterService" description="(User) Unregister the service at MOBY Central.">
        <echo message="${description} -- unregistering Service --> ${servlet.host.url}/${my.servlet.war.file.name}"/>
<java jar="${build.dir}${file.separator}${my.servlet.war.file}"
                fork="true"
		classpathref="classpath" >
          <arg value="${servlet.host.url}/${my.servlet.war.file.name}/"/>
          <arg value="unregister"/>
	</java>
    </target> 

    <target name="updateWebXML" description="(Internal) updates the WAR's web.xml file to reflect the new main servlet class" >
        <echo message="${description} -- updating ${build.dir}${file.separator}WEB-INF${file.separator}web.xml."/>
        <unjar src="${lib.dir}${file.separator}${moby.servlet.war.file.name}" dest="${build.dir}">
             <patternset>
                 <include name="**${file.separator}web.xml"/>
                 <include name="META-INF${file.separator}MANIFEST.MF"/>
             </patternset>
        </unjar>

         <replace file="${build.dir}${file.separator}WEB-INF${file.separator}web.xml" token="org.biomoby.service.MobyServlet" value="${main.class.package}.${main.class}"/>
         <replace file="${build.dir}${file.separator}WEB-INF${file.separator}web.xml" token="MobyServlet" value="${my.servlet.war.file.name}"/>
        
     </target> 

    <target name="clean" description="(User) Clean the build directory.">
        <echo message="${description} -- clean "/>     
        <delete dir="${build.dir}"/>
    </target>        

    <target name="env" description="(User) Show the current environment variables for the build.">
        <echo message=" -- servlet.name             --> ${my.servlet.war.file.name}"/>                                  
        <echo message=" -- project.dir              --> ${project.dir}"/>                                  
        <echo message=" -- src.dir                  --> ${src.dir}"/>
        <echo message=" -- lib.dir                  --> ${lib.dir}"/>
        <echo message=" -- build.dir                --> ${build.dir}"/>
        <echo message=" -- build.classes.dir        --> ${build.classes.dir}"/>
	    <echo message=" -- user.home                --> ${user.home}"/>		  
		<echo message=" -- os.name                  --> ${os.name}"/>
		<echo message=" -- build.property.file      --> ${build.property.file}"/>               
        <echo message=" -- main.class               --> ${main.class}"/>                                              
        <echo message=" -- main.class.package       --> ${main.class.package}"/>                                    
        <echo message=" -- main.class.dir           --> ${main.class.dir}"/>
        <echo message=" -- servlet.host.url         --> ${servlet.host.url}"/>
    </target>   	
</project>

