<?xml version="1.0"?>
<project name="gs3-soap-ext" default="usage" basedir=".">

  <property environment="env"/>

  <property name="axis.zip.version" value="axis-bin-1_4.zip"/>
  <property name="axis.dir.version" value="axis-1_4"/>

 <property name="src.home" value="${basedir}/src/java"/>
 <property name="build.home" value="${basedir}/build"/>

  <property name="web.writablehome" location="${basedir}/../../web"/>
  <property name="web.lib" value="${web.writablehome}/WEB-INF/lib"/>
  <!-- this may be set in build.properties, e.g. if you move the web dir to 
       tomcats webapps directory -->
  <property name="web.home" value="${basedir}/web"/>

  <target name="usage" description="Print a help message">
    <echo message="  Execute 'ant -projecthelp' for a list of targets."/>
    <echo message="  Execute 'ant -help' for Ant help."/>
    <echo>To install the Soap extension for Greenstone3, run 'ant add-extension'.
        To remove the files and folders installed by add-extension, run 'ant del-extension'.
    </echo>
  </target> 
    <!-- ======================= Axis Targets ============================ -->  
  
  <target name="prepare-axis">
    <echo>Checking for existence of: ${packages.home}/axis/.flagfile</echo>
    <if>
      <bool>
        <not><available file="${packages.home}/axis/.flagfile"/></not>
      </bool>
      <unzip src="${packages.home}/${axis.zip.version}"
        dest="${packages.home}"/>
      <move todir="${packages.home}/axis">
        <fileset dir="${packages.home}/${axis.dir.version}"/>
      </move>
      <!-- install axis into greenstone web app -->
      <copy todir="${web.lib}">
        <fileset dir="${packages.home}/axis/webapps/axis/WEB-INF/lib">
          <include name="*.jar"/>
        </fileset>
      </copy>
      <copy todir="${web.home}">
        <fileset dir="${packages.home}/axis/webapps/axis/">
          <include name="*.jsp"/>
          <include name="*.jws"/>
        </fileset>
      </copy>
      <copy tofile="${web.home}/axis.html" file="${packages.home}/axis/webapps/axis/index.html"/>
      <copy todir="${web.classes}">
        <fileset dir="${packages.home}/axis/webapps/axis/WEB-INF/classes">
          <include name="*.properties"/>
        </fileset>
      </copy>
      <echo file="${packages.home}/axis/.flagfile">
        the timestamp of this file is the time that axis was extracted from the zip files.
        it is used to figure out whether the files need to be refreshed or not in `ant prepare-axis`
      </echo>

      <else>
        <echo>Axis has been prepared, will not prepare</echo>
        <echo>Delete ${packages.home}/axis/.flagfile to force refresh</echo>
      </else>

    </if>
  </target>
  
  <target name="soap-deploy-site" depends="get-sitename,get-siteuri,get-webservices,create-deployment-files,deploy-site"
	  description="Deploy a SOAP web service for a local Greenstone site. Tomcat must be running for this to work."/>
  
  <target name="deploy-site">
    <java classname="org.apache.axis.client.AdminClient">
      <classpath refid="compile.classpath"/>
      <arg value="-l"/>
      <arg value="${default.server.protocol}://${tomcat.server}:${default.tomcat.port}${app.path}/servlet/AxisServlet"/>
      <arg file="${basedir}/resources/soap/deploy.wsdd"/>
    </java>
    <delete file="${basedir}/resources/soap/deploy.wsdd"/> <!--clean up, no longer used-->
  </target>
  
  <target name="soap-undeploy-site" depends="get-undeploy-service-name"
	  description="Undeploy a SOAP web service for a local Greenstone site. Tomcat must be running for this to work.">
    <filter token="servicesname" value="${axis.undeploy.servicename}"/>
    <copy file="${basedir}/resources/soap/undeploy-site.wsdd.template"
	  tofile="${basedir}/resources/soap/undeploy.wsdd"
	  filtering="true"
	  overwrite="true"/>
    <java classname="org.apache.axis.client.AdminClient">
      <classpath refid="compile.classpath"/>
      <arg value="-l"/>
      <arg value="${default.server.protocol}://${tomcat.server}:${default.tomcat.port}${app.path}/servlet/AxisServlet"/>
      <arg file="${basedir}/resources/soap/undeploy.wsdd"/>
    </java>
    <delete file="${basedir}/resources/soap/undeploy.wsdd"/> <!--clean up, no longer used-->
  </target>  
  
  <!-- this target used to deploy the default web service SOAPServer (base.webservice.name) on the localsite server
       with the default servicename of localsite-->
  <target name="deploy-localsite" 
	  description="Deploy the SOAP server for localsite. Will start and stop Tomcat.">
    <antcall target="force-start-tomcat"/>
    <echo>Deploying ${base.webservice.name} web services for localsite under service name: localsite</echo>
    <antcall target="create-deployment-files">
      <param name="axis.sitename" value="localsite"/>
      <param name="axis.servicesname" value="${base.webservice.name}"/>
      <param name="axis.siteuri" value="localsite"/>
    </antcall>
    <antcall target="deploy-site">
      <param name="axis.sitename" value="localsite"/>
      <param name="axis.servicesname" value="${base.webservice.name}"/>
      <param name="axis.siteuri" value="localsite"/>
    </antcall>
    <echo>The Greenstone server has been started up. If you do not want it running, please type: ant stop.</echo>
  </target>
    
  <target name="get-sitename" unless="axis.sitename">
    <input addproperty="axis.sitename" defaultvalue="localsite">What site do you want to deploy services for?
Press Enter for default:localsite</input>
  </target>

  <target name="get-undeploy-service-name" unless="axis.undeploy.servicename">
    <input addproperty="axis.undeploy.servicename" defaultvalue="localsite">Please enter the full name of the service you wish to undeploy.
To find out which web services you have got deployed, point your browser to ${default.server.protocol}://${tomcat.server}:${default.tomcat.port}/greenstone3/services 
Or press Enter for undeploying the default:localsite /&gt;</input>
     <echo>Name of service to undeploy: ${axis.undeploy.servicename}</echo>
  </target>

  <target name="get-webservices" unless="axis.servicesname">
    <input addproperty="axis.servicesname" defaultvalue="${base.webservice.name}">Which set of web services do you want to deploy?
Choose from: ${web.services.list}
Or press Enter for default:${base.webservice.name} /&gt;</input>
    <echo>${axis.servicesname}</echo>
  </target>

  <target name="get-siteuri" depends="get-sitename,get-webservices" unless="axis.siteuri">
    <input addproperty="axis.siteuri" defaultvalue="${axis.servicesname}${axis.sitename}">What name do you want the service to have? (Press Enter for default:${axis.servicesname}${axis.sitename})</input>
    <echo>Site: ${axis.sitename}, services: ${axis.servicesname}, servicesname: ${axis.siteuri}</echo>
  </target>

  <target name="set-soapmethod" description="Determines whether the service in the wsdd should have the style attribute set to message or the provider attribute set to java:RPC"  if="axis.servicesname">
   <condition property="soap.method" value="provider='java:MSG' style='message' use='literal'">
      <equals arg1="${axis.servicesname}" arg2="${base.webservice.name}"/>
    </condition>
   
   <!--everything else defaults to java:RPC at present-->
   <condition property="soap.method" value="provider='java:RPC'">
     <not>
      <equals arg1="${axis.servicesname}" arg2="${base.webservice.name}"/>
    </not>
    </condition>
  </target>
    
  <target name="create-deployment-files" depends="set-soapmethod" if="axis.sitename">
    <filter token="sitename" value="${axis.sitename}"/>
    <filter token="siteuri" value="${axis.siteuri}"/>
    <filter token="servicesname" value="${axis.servicesname}"/>
    <filter token="soapmethod" value="${soap.method}"/>
    <copy file="${basedir}/resources/soap/site.wsdd.template"
      tofile="${basedir}/resources/soap/deploy.wsdd"
      filtering="true"
      overwrite="true"/>
    <!-- create the java files and compile them -->
    <copy file="${basedir}/resources/java/${axis.servicesname}.java.in"
      tofile="${src.gsdl3.home}/${axis.servicesname}${axis.sitename}.java"
      filtering="true"
      overwrite="true"/>
    <mkdir dir="${build.home}"/>
    <javac srcdir="${java.src.home}"
      destdir="${build.home}" 
      includeantruntime="${compile.includeantruntime}"
      debug="${compile.debug}"
      deprecation="${compile.deprecation}"
      optimize="${compile.optimize}"
      encoding="${compile.encoding}">
      <compilerarg line="${compile.javac.flags}"/>
      <classpath refid="compile.classpath"/>
      <include name="org/greenstone/gsdl3/${axis.servicesname}${axis.sitename}.java" />
    </javac>
    <mkdir dir="${web.classes}/org/greenstone/gsdl3"/>
    <copy file="${build.home}/org/greenstone/gsdl3/${axis.servicesname}${axis.sitename}.class" 
      tofile="${web.classes}/org/greenstone/gsdl3/${axis.servicesname}${axis.sitename}.class"
      overwrite="true" />

    <!-- this was in compile-core from main gs3 build.xml -->
    <!-- copy the localsite server in case we need it -->
    <copy file="${build.home}/org/greenstone/gsdl3/${base.webservice.name}localsite.class" tofile="${web.classes}/org/greenstone/gsdl3/${base.webservice.name}localsite.class" />

  </target>

  <!-- just copied from solr. do we need this? -->
  <target name="compile" description="Compile up the soap java classes, jar them up, and install gs3-soap.jar into Greenstone's web/WEB-INF/lib dir">

  <mkdir dir="${build.home}"/>
    <javac srcdir="${src.home}" destdir="${build.home}" debug="true"
           includeantruntime="false">
      <compilerarg line="${compile.javac.flags}"/>
      <classpath>
        <path refid="compile.classpath"/>
      </classpath>
    </javac>
    <jar destfile="${build.home}/gs3-soap.jar">
      <fileset dir="${build.home}">
        <include name="org/greenstone/gsdl3/**"/>
      </fileset>
      <manifest>
        <attribute name="Built-By" value="${user.name}" />
      </manifest>
    </jar>
    <copy file="${build.home}/gs3-soap.jar" todir="${web.libdir}"/>
  </target>
</project>
