/**
 * Formic installer framework.
 * Copyright (C) 2012  Eric Van Dewoestine
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.formic.ant;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import java.util.HashMap;

import org.apache.commons.io.IOUtils;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import org.apache.velocity.VelocityContext;

import org.apache.velocity.app.Velocity;

import org.formic.wizard.step.shared.TemplateStepAction;

/**
 * Ant task to evaluate a velocity template.
 *
 * @author Eric Van Dewoestine
 */
public class TemplateTask
  extends Task
{
  private File template;
  private File out;
  private boolean move;

  public void execute()
    throws BuildException
  {
    if (template == null || out == null){
      throw new BuildException("Both template and out attributes must be supplied");
    }

    if (template.equals(out)){
      out = new File(out.getAbsolutePath() + ".out");
      move = true;
    }

    HashMap values = new HashMap();

    values.put(TemplateStepAction.INSTALLER_KEY, TemplateStepAction.INSTALLER);
    values.put(TemplateStepAction.OS_KEY, TemplateStepAction.OS);
    values.putAll(getProject().getProperties());

    FileReader reader = null;
    FileWriter writer = null;
    try{
      reader = new FileReader(this.template);
      writer = new FileWriter(this.out);
      VelocityContext context = new VelocityContext(values);
      Velocity.evaluate(context, writer, TemplateTask.class.getName(), reader);
    }catch(Exception e){
      throw new RuntimeException(e);
    }finally{
      IOUtils.closeQuietly(reader);
      IOUtils.closeQuietly(writer);
    }

    if (move){
      out.renameTo(template);
    }
  }

  public void setTemplate(File template)
  {
    this.template = template;
  }

  public void setOut(File out)
  {
    this.out = out;
  }
}

@@ -1060,16 +1060,60 @@
 .Task;%0A%0A
+import org.apache.tools.ant.taskdefs.Move;%0A%0A
 import o
@@ -2050,16 +2050,63 @@
    try%7B%0A
+      log(%22Evaluating template: %22 + template);%0A
       re
@@ -2505,29 +2505,184 @@
     
-out.renameTo(template
+Move move = new Move();%0A      move.setProject(getProject());%0A      move.setTaskName(getTaskName());%0A      move.setFile(out);%0A      move.setTofile(template);%0A      move.execute(
 );%0A 

update template task to use ant move task internally