<?xml version="1.0" encoding="ISO-8859-1"?>
<project default="none" name="forrestbot-templates">

  <!--
    This file carries the Ant-target templates which are called from the
    generated work.build.xml file in which most of the properties are set that
    are picked up by these templates.

    Maintenance of this file should be in sync with the allowed (supportedd)
    settings in the /forrestbot.conf.xml file.

    These templates reflect the workstages defined in the stages.conf.xml file.
    The various responsibilities for these workstages are:
    [prepare] make a clean forrest-defined cocoon context environment
    [get-src] slide in the project specific document sources in the cocoon env
    [generate] let the cocoon crawler spit out the site
    [deploy] make the result of the generate step public in configured way
    [cleanup] no real need at the moment?
    each workstage can come in different flavours depicted with their @style
    in the forrestbot.conf.xml

    currently there is:                    activated by forrestbot.conf.xml:
    template.prepare                       <prepare>
    template.get-src.cvs                   <get-src type="cvs">
    template.get-src.local-copy            <get-src type="local-copy" >
    template.generate                      <generate>
    template.deploy.local-copy             <deploy type="local-copy" >
    template.deploy.scp                    <deploy type="scp" >
    template.deploy.mail                   <deploy type="mail" >

    each template target uses its own set of parameters (properties in fact)
    that should be set before calling them

    Todo: maybe we will need a stage for getting soure of custom generators
          compile them to class files and slide them into the context as well
          Also other extensions could come along

    The Meta-template: (how to add one yourself)
    could be silly to you, but there is this bit of a way of working to keep
    this file a bit readable... so please fit in:
    - build an echo target for each template target you make:

    <target name="echo.workstage-name.optional-type" if="template.echo">
          ... here you put echo ant-tasks that somewhat tell what you will
          ... do with all the params you expect
    </target>

    <target name="template.workstage-name.optional-type"
            depends="shared.set-props, echo.workstage-name.optional-type">
          ... here you put whatever ant-tasks that actually do it
    </target>

    - in the future there will be a DTD for forrestbot.conf.xml that should be
    updated probably after you added stuff here.
  -->

  <!-- Some defaults that typically get set before the template targets
       are called. Here there are some defaults for when they were not set -->
  <property name="xlayout.build.bot.dir"            value="./build/bot" />
  <property name="xlayout.build.bot.projects.dir"   value="./build/bot/projects" />
  <property name="xlayout.build.bot.cvsmodules.dir" value="./build/bot/cvsmodules" />
  <property name="xlayout.build.bot.work.dir"       value="./build/bot/work" />
  <property name="xlayout.build.bot.docs.dir"       value="./build/bot/docs" />
  <property name="project.name"                     value="xml-forrest" />
  <!-- Todo: always cleanup/check for more implicit dependencies on being
             called from the main ant script -->


  <!--
    <property name="template.echo" value="true" />

    uncomment this one or use -Dtemplate.echo=true at the commandline
    if you want to know the configuration of each template
  -->


  <target name="shared.set-props" >
    <!-- where we build the cocoon context per project: -->
    <!--
    <property name="project.ctxt-dir"
      value="${xlayout.build.bot.projects.dir}/${project.name}/context" />
    -->
    <!-- where we will temporarily store and cache the various cvsmodules: -->
    <property name="project.cvsmodule.dir"
            value="${xlayout.build.bot.cvsmodules.dir}/${project.name}" />
    <!-- where the stuff forrest is offering comes from -->
    <property name="forrest.context.dir" value="${xlayout.source.resources.dir}" />

      <!-- where cocoon can have his toy-work-environment while generating: -->
      <!-- mind: other stages can use this as a temp dir as well     -->
      <!--
      <property name="project.work-dir"
        value="${project.ctxt-dir}/../work" />
      -->
      <!-- where cocoon will spit out the generated stuff: -->
      <!--
      <property name="project.docs-dir" value="${project.ctxt-dir}/../docs" />
      -->

    <!-- These are OVERRIDES, hence 'project.*' -->
    <property name="project.build-dir" location="${bot.build.dir}"     />
    <property name="project.temp-dir"  location="${project.build-dir}/tmp"     />
    <property name="project.ctxt-dir"  location="${project.build-dir}/${project.name}/context" />
    <property name="project.work-dir"  location="${project.build-dir}/${project.name}/work" />
    <property name="project.site-dir"  location="${project.build-dir}/${project.name}/site" />
 
  </target>


  <!-- ________________ PREPARE TEMPLATES __________________ -->
  <target name="echo.prepare" if="template.echo">

    <echo message="prepare workstage for project ${project.name}"/>
    <echo message="skin = ${prepare.skin}"/>
  </target>

  <target name="template.prepare" depends="shared.set-props, echo.prepare">
    <echo>
      ############################
      # Target: prepare
      # Calling ${forrest.home}/forrest.build.xml#bare-context
      # In dir ${basedir}
      # Skin="${prepare.skin}"
      ############################
    </echo>
    <ant dir="${basedir}" antfile="${forrest.home}/forrest.build.xml" target="bare-context">
      <property name="project.skin" value="${prepare.skin}"/>
    </ant>
  </target>


  <!-- ________________ GET-SRC TEMPLATES __________________ -->
  <!-- [[get-src type cvs ]] -->
  <target name="echo.get-src.cvs" if="template.echo">
    <echo message="get-src workstage of type CVS for project ${project.name}"/>
    <echo message="cvs login: usr= ${get-src.cvs.user} / pwd= ${get-src.cvs.passwd}"/>
    <echo message="cvs update from host: ${get-src.cvs.host} with root: ${get-src.cvs.root}"/>
    <echo message="cvs update  for module: ${get-src.cvs.module} to dir:
      ${get-src.cvs.content-dir}"/>
  </target>

  <target name="template.get-src.cvs" depends="shared.set-props, echo.get-src.cvs">
    <cvspass cvsroot=":pserver:${get-src.cvs.user}@${get-src.cvs.host}:${get-src.cvs.root}"
            password="${get-src.cvs.passwd}"
            />
    <mkdir dir="${project.cvsmodule.dir}" />
    <!-- get from cvs only the directory that is holding the xdoc content -->
    <cvs cvsRoot=":pserver:${get-src.cvs.user}@${get-src.cvs.host}:${get-src.cvs.root}"
         package="${get-src.cvs.module}/${get-src.cvs.content-dir}"
         dest="${project.cvsmodule.dir}"
         quiet="true"
         />
    <!-- get from cvs only the top section files: they should contain the *.*.ml
         project descriptors (-L option makes non recursive cvs update) -->
    <cvs cvsRoot=":pserver:${get-src.cvs.user}@${get-src.cvs.host}:${get-src.cvs.root}"
         package="${get-src.cvs.module}"
         dest="${project.cvsmodule.dir}"
         quiet="true"
         command="checkout -l"
         />

    <echo>
      ############################
      # Target: get-src.cvs
      # Calling ${forrest.home}/forrest.build.xml#project-context-bot
      # In dir="${project.cvsmodule.dir}/${get-src.cvs.module}/${get-src.cvs.project-dir}"
      # Content dir="${project.cvsmodule.dir}/${get-src.cvs.module}/${get-src.cvs.content-dir}"
      # Skin="${prepare.skin}"
      ############################
    </echo>
    <!--
    dir="${project.cvsmodule.dir}/${get-src.cvs.module}/${get-src.cvs.dir}"-->
    <ant
      dir="${project.cvsmodule.dir}/${get-src.cvs.module}/${get-src.cvs.project-dir}"
      antfile="${forrest.home}/forrest.build.xml" target="project-context-bot">
      <property name="project.skin" value="${prepare.skin}"/>
      <property name="forrest.echo" value="true"/>
    </ant>

    <!--
    <copy todir="${project.ctxt-dir}">
      <fileset dir="${project.cvsmodule.dir}/${get-src.cvs.module}/${get-src.cvs.dir}" />
    </copy>
    <copy todir="${project.ctxt-dir}" >
      <fileset dir="${project.cvsmodule.dir}/${get-src.cvs.module}">
        <include name="*.*ml"/>
      </fileset>
    </copy>
    -->
  </target>

  <!-- [[get-src type local-copy ]] -->
  <target name="echo.get-src.local-copy" if="template.echo">
    <echo message="get-src workstage of type LOCAL-COPY for project ${project.name}"/>
    <echo message="content-dir: ${get-src.local-copy.content-dir} provides xdocs"/>
    <echo message="project-dir: ${get-src.local-copy.project-dir} provides [project-description].*ml files"/>
  </target>

  <target name="template.get-src.local-copy" depends="shared.set-props, echo.get-src.local-copy">
    <echo>
      ############################
      # Target: get-src.local-copy
      # Calling ${forrest.home}/forrest.build.xml#project-context-bot
      # In dir ${get-src.local-copy.project-dir}
      ############################
    </echo>
    <ant dir="${get-src.local-copy.project-dir}"
      antfile="${forrest.home}/forrest.build.xml" target="project-context-bot">
    </ant>
  </target>

  <!-- ________________ GENERATE TEMPLATES __________________ -->

  <target name="echo.generate" if="template.echo">
    <property name="classpath-dump" refid="forrest.cp"/>
    <echo message="generate workstage for project ${project.name}"/>
    <echo message="using cocoon in dir ${bot.build.dir}/${project.name}/src"/>
    <echo message="at debug level = ${generate.debuglevel}"/>
    <echo message="looking for java classes in ${classpath-dump}"/>
  </target>

  <target name="template.generate" depends="shared.set-props, echo.generate">
    <echo>
      ############################
      # Target: generate
      # Calling ${forrest.home}/forrest.build.xml#-site
      # In dir ${project.ctxt-dir}
      ############################
    </echo>
    <!--# In dir ${get-src.local-copy.project-dir}-->
    <!--<ant dir="${project.ctxt-dir}"-->
      <ant dir="${project.ctxt-dir}"
        antfile="${forrest.home}/forrest.build.xml" target="-site">
        <property name="project.content.present" value="HACK"/>
        <property name="project.skin" value="${prepare.skin}"/>
      </ant>
  </target>

  <!-- ________________ DEPLOY TEMPLATES __________________ -->
  <!-- [[deploy type scp ]] -->
  <target name="echo.deploy.scp" if="template.echo">
    <echo message="deploy workstage of type scp for project ${project.name}"/>
    <echo message="will deploy to ${deploy.scp.host}" />
    <echo message="as user ${deploy.scp.user}" />
    <echo message="into dir ${deploy.scp.root}/${deploy.scp.dir}" />
  </target>

  <target name="template.deploy.scp" depends="shared.set-props, echo.deploy.scp">
    <!-- clean out the temp space -->
    <delete dir="${project.work-dir}"/>
    <mkdir dir="${project.work-dir}"/>
    <property name="tempname" value="${project.name}-deploy" />
    <property name="tempfile"
              value="${project.work-dir}/${tempname}" />
    <property name="remotedir" value="${deploy.scp.root}/${deploy.scp.dir}" />
    <tar tarfile="${tempfile}.tar" basedir="${project.site-dir}"/>
    <gzip zipfile="${tempfile}.tar.gz" src="${tempfile}.tar"/>
    <delete file="${tempfile}.tar"/>
    <exec dir="." executable="scp">
      <!--<arg value="-oProtocol=1"/>-->
      <arg value="${tempfile}.tar.gz"/>
      <arg value="${deploy.scp.user}@${deploy.scp.host}:${remotedir}"/>
    </exec>
    <exec dir="." executable="ssh">
      <arg line="-n ${deploy.scp.host} -l ${deploy.scp.user} 'cd ${remotedir};gunzip ${tempname}.tar.gz;tar xUvf ${tempname}.tar;chmod -R g+u *;rm ${tempname}.tar'"/>
    </exec>
    <delete file="${tempfile}.tar.gz"/>
  </target>

  <!-- [[deploy type local-copy ]] -->
  <target name="echo.deploy.local-copy" if="template.echo">
      <echo message="deploy workstage of type scp for project ${project.name}"/>
      <echo message="destination dir = ${deploy.local-copy.destination}" />
  </target>

  <target name="template.deploy.local-copy" depends="shared.set-props, echo.deploy.local-copy">
    <copy todir="${deploy.local-copy.destination}">
        <fileset dir="${project.site-dir}" />
    </copy>
  </target>


  <!-- [[deploy type ftp ]] -->
  <target name="echo.deploy.ftp" if="template.echo">
    <echo>
Deploy workstage of type ftp for project ${project.name}
Will use ftp to host: ${deploy.ftp.host}
Impersonating user: ${deploy.ftp.user} with passwd: ${deploy.ftp.passwd}
to copy the stuff to relative user dir: ${deploy.ftp.destination}
    </echo>
  </target>

  <target name="template.deploy.ftp" depends="shared.set-props, echo.deploy.ftp">
    <ftp server="${deploy.ftp.host}"
         userid="${deploy.ftp.user}"
         password="${deploy.ftp.passwd}"
         remotedir="${deploy.ftp.destination}"
         action="put"
         depends="yes"
         verbose="no"
         ignoreNoncriticalErrors="true"
    >
      <fileset dir="${project.site-dir}" />
    </ftp>
  </target>

  <!-- ________________ CLEANUP TEMPLATES __________________ -->
  <target name="echo.cleanup" if="template.echo">
      <echo message="cleanup workstage for project ${project.name}"/>
  </target>
  <target name="template.cleanup" depends="shared.set-props, echo.cleanup">
    <!-- todo: FIXME: not implemented yet -->
  </target>



  <!-- ________________ MAIL TEMPLATE ______________________ -->
  <target name="echo.mail" if="template.echo">
    <echo>
Will send mail (${mail.send}) to ${mail.to} about the completion-status ${mail.completion-status}
of the work the robot has done on project ${project.name}.
The mail will hold the attachment(s) ${mail.attachments}
[using smtp host: ${mail.host}]
	</echo>
  </target>

  <target name="template.mail" if="mail.send" depends="echo.mail">
    <property name="mail.host" value="localhost" />
    <echo>Mailing ${mail.to}...</echo>
    <mail from="forrest-bot@xml.apache.org"
          mailhost="${mail.host}"
          tolist="${mail.to}"
          subject="[DO NOT REPLY] ForrestBot site builder for '${project.name}'"
          files="${mail.attachments}"
          failonerror="false">
      <message>
Another hard day at the BOT factory, and quite happy to have worked for you.
Our completion status for your project ${project.name} was ${mail.completion-status}
Please find the details in the log attached.

The Forrest-Bot team. (http://xml.apache.org/forrest)
      </message>
    </mail>
  </target>

</project>
