<?xml version="1.0"?>
<!--
  Copyright 2001-2004 The Apache Software Foundation

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!--

  Build the scripts 

  If you need to bootstrap this, see gen.sh or gen.bat.

  Important targets:
    scripts - generate the scripts
    all     - generate both the scripts and javadocs

-->

<project name="gump" default="scripts" basedir=".">

  <property file="local.properties"/>
  <!-- Default properties. Define them before to override them.-->
  <property name="workspace" value="rubypad.xml"/>
  <property name="webstyle" value="stylesheet/jakarta.xsl"/>

  <target name="offline">
    <property name="onlineflag" value="-offline"/>
  </target>


  <target name="platform_props">
    <condition property="target" value="bash">
        <os family="unix"/>
    </condition>
    <condition property="target" value="win2k">
      <and>
        <os family="windows"/>
        <not>
          <os family="win9x"/>
        </not>
      </and>
    </condition>

    <condition property="extension" value="sh">
        <os family="unix"/>
    </condition>
    <condition property="extension" value="bat">
      <and>
        <os family="windows"/>
        <not>
          <os family="win9x"/>
        </not>
      </and>
    </condition>

    <condition property="shell" value="/bin/sh">
        <os family="unix"/>
    </condition>
    <condition property="shell" value="cmd.exe">
      <and>
        <os family="windows"/>
        <not>
          <os family="win9x"/>
        </not>
      </and>
    </condition>
  </target>

  <!-- Build the code used to generate the merged xml scripts -->
  <target name="compile" depends="platform_props">
    <mkdir dir="classes"/>
    <mkdir dir="work"/>
    <mkdir dir="cache"/>
    <javac srcdir="java" destdir="classes"/>
    <jar destfile="jenny.jar" basedir="classes"/>

    <uptodate property="uptodate" targetfile="work/merge.xml">
      <srcfiles dir="." includes=".timestamp"/>
      <srcfiles dir="." includes="jenny.jar"/>
      <srcfiles dir="." includes="${workspace}"/>
      <srcfiles dir="profile" includes="*.xml"/>
      <srcfiles dir="project" includes="*.xml"/>
      <srcfiles dir="repository" includes="*.xml"/>
    </uptodate>

    <uptodate property="javadocs.uptodate" targetfile="javadocs/index.html">
      <srcfiles dir="java" includes="*.java"/>
    </uptodate>
  </target>

  <!-- Merge the xml sources, resolving references -->
  <target name="gen" depends="compile" unless="uptodate">
    <property name="onlineflag" value="-online"/>
    <java classname="Jenny" fork="yes" failonerror="true" classpath="jenny.jar">
      <jvmarg value="-Dhttp.proxyHost=${http.proxyHost}"/>
      <jvmarg value="-Dhttp.proxyPort=${http.proxyPort}"/>
      <arg value="${workspace}"/>
      <arg value="${onlineflag}"/>
    </java>
  </target>

  <!-- Alias of 'gen' target, used to verify correctness and completeness
       of a project definition -->
  <target name="verify" depends="gen"
    description="Verify the project definitions"/>

  <!-- Extract the info necessary to produce the update script -->
  <target name="update" depends="gen">
    <style in="work/merge.xml" out="work/update.def"
           style="stylesheet/update.xsl"/>
  </target>

  <!-- Extract the info necessary to produce the build script -->
  <target name="build" depends="gen">
    <style in="work/merge.xml" out="work/build.def"
           style="stylesheet/build.xsl"/>
  </target>

  <!-- Produce the xref script -->
  <target name="xref" depends="gen">
    <style in="work/merge.xml" out="work/xref.def"
           style="stylesheet/xref.xsl"/>
    <style basedir="work" destdir="work" includes="xref.def" extension=".site"
           style="${webstyle}"/>
    <style basedir="work" destdir="work" includes="xref.site"
           extension=".${extension}" style="stylesheet/${target}.xsl"/>
  </target>

  <!-- Extract the info necessary to produce the publish script -->
  <target name="publish" depends="gen">
    <copy file="work/merge.xml" tofile="work/puball.site"/>
    <style in="work/merge.xml" out="work/publish.def"
           style="stylesheet/publish.xsl"/>
    <style in="work/merge.xml" out="work/map.pl"
           style="stylesheet/sedmap.xsl"/>
  </target>

  <!-- Extract the info necessary to produce the deliver script -->
  <target name="deliver" depends="gen">
    <style in="work/merge.xml" out="work/deliver-local.sh"
           style="stylesheet/deliver-local.xsl"/>
  </target>

  <!-- Extract the info necessary to produce the naglist -->
  <target name="naglist" depends="gen">
    <style in="work/merge.xml" out="work/naglist"
           style="stylesheet/nag.xsl"/>
  </target>

  <!-- generate the move script -->
  <target name="move" depends="gen">
    <style in="work/merge.xml" out="work/move.xml"
           style="stylesheet/move.xsl"/>
    <style in="work/move.xml" out="work/move.${extension}"
           style="stylesheet/${target}.xsl"/>
  </target>

  <!-- Add in the website information -->
  <target name="website" depends="build,update,publish,deliver,naglist,move">
    <style basedir="work" destdir="work" includes="*.def" extension=".site"
           excludes="xref.*" style="${webstyle}"/>
  </target>

  <!-- Generate the scripts -->
  <target name="scripts" depends="website"
    description="Generate the update and build scripts">

    <style basedir="work" destdir="work" includes="*.site" excludes="xref.*"
           extension=".${extension}" style="stylesheet/${target}.xsl"/>
  </target>

  <!-- generate and publish scripts to work area -->
  <target name="exec-scripts" depends="scripts">
    <exec dir="work" executable="${shell}">
      <arg value="puball.${extension}"/>
      <arg value="../${workspace}"/>
    </exec>
    <exec executable="${shell}">
      <arg value="work/move.${extension}"/>
    </exec>
   </target>

   <!-- Generate javadocs -->
  <target name="javadocs" depends="compile" unless="javadocs.uptodate">
    <mkdir dir="javadocs"/>
    <javadoc sourcepath="java" destdir="javadocs" classpath="jenny.jar">
      <source file="java/Jenny.java"/>
      <source file="java/Module.java"/>
      <source file="java/Project.java"/>
      <source file="java/Repository.java"/>
      <source file="java/Server.java"/>
      <source file="java/Workspace.java"/>
    </javadoc>
  </target>

  <!-- Generate publish instructions for the javadocs -->
  <target name="pubdocs" depends="gen">
    <style in="work/merge.xml" out="work/pubdoc.pl"
           style="stylesheet/pubdocs.xsl"/>
  </target>

  <!-- Generate publish instructions for the junit reports -->
  <target name="pubreport" depends="gen">
    <style in="work/merge.xml" out="work/pubreport.pl"
           style="stylesheet/pubreport.xsl"/>
  </target>

  <!-- Produce the gump web site -->
  <target name="site" description="Generate the site">
    <ant dir="site"/>
  </target>

  <!-- Check to see if all of the local packages are installed -->
  <target name="check" depends="compile"
    description="Check to see if all the local packages are installed">

    <property name="onlineflag" value="-online"/>
    <java classname="LocalCheck" fork="yes" classpath="jenny.jar">
      <arg value="${workspace}"/>
    </java>
  </target>

  <!-- Do everything -->
  <target name="all" depends="scripts,xref,javadocs,pubdocs,check"
    description="Generate scripts, docs, sites, and check local packages"/>

  <!-- Remove all outputs and intermediate results -->
  <target name="clean"
    description="Remove build and work byproducts">

    <delete file=".timestamp"/>
    <delete file="jenny.jar"/>
    <delete dir="classes"/>
    <delete dir="work"/>
    <ant dir="site" target="clean"/>
  </target>
  
  
  <target name="nobuild-projects" description="Create nobuild version of all projects">
  		<style  basedir="project"
  			   destdir="project/nobuild"
  			   
  			   extension=".xml" 
  			   style="stylesheet/nobuild-projects.xsl">
  			
  				<include  name="*.xml" />
  			
  		</style>
		
		 <replace dir="project/nobuild" token="-@@DATE@@" value="">
		 	<include name="*.xml"/>
		 </replace>
		 
  </target>
  
  <target name="undate-pkgs" >
  	<move  todir="${pkg.dir}">
		<fileset dir="${pkg.dir}">
			<include name="*/*.jar" />
		</fileset>
		<mapper type="regexp" from="^(.*)-[0-9]*\.jar$$" to="\1.jar"/>
	</move>
  </target>
</project>
