package org.perfcake.maven;

/*
 * Copyright 2001-2005 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.
 */

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.perfcake.ScenarioExecution;

/**
 * 
 * @author vjuranek
 * 
 */
@Mojo(name = "scenario-run", defaultPhase = LifecyclePhase.INTEGRATION_TEST)
public class ScenarioRunMojo extends AbstractMojo {

   private final String PERFCAKE_DIR = "perfcake";
   private final String DEFAULT_SCENARIOS_DIR = PERFCAKE_DIR + File.separator + "scenarios";
   private final String DEFAULT_MESSAGES_DIR = PERFCAKE_DIR + File.separator + "messages";
   private final String DEFAULT_PLUGINS_DIR = PERFCAKE_DIR + File.separator +  "plugins";

   @Parameter(required = true)
   private String scenario;
   @Parameter(alias = "scenarios-dir")
   private String scenariosDir;
   @Parameter(alias = "messages-dir")
   private String messagesDir;
   @Parameter(alias = "plugins-dir")
   private String pluginsDir;

   @Component
   private MavenProject project;

   public void execute() throws MojoExecutionException {
      initDefaults();
      
      ArrayList<String> args = new ArrayList<String>();
      args.add("-s");
      args.add(scenario);

      args.add("-sd");
      args.add(scenariosDir);

      args.add("-md");
      args.add(messagesDir);

      args.add("-pd");
      args.add(pluginsDir);

      System.out.println("Running scenario " + scenario); //TODO use log
      ScenarioExecution.main(args.toArray(new String[args.size()]));
      System.out.println("DONE scenario " + scenario); //TODO use log
   }

   private void initDefaults() {
      String resPath = getDefaultResourcePath();

      if (scenariosDir == null || scenariosDir.trim().isEmpty()) {
         File testDir = new File(resPath + File.separator + DEFAULT_SCENARIOS_DIR);
         scenariosDir = testDir.isDirectory() ? testDir.getAbsolutePath() : resPath;
      }

      if (messagesDir == null || messagesDir.trim().isEmpty()) {
         File testDir = new File(resPath + File.separator + DEFAULT_MESSAGES_DIR);
         messagesDir = testDir.isDirectory() ? testDir.getAbsolutePath() : resPath;
      }

      if (pluginsDir == null || pluginsDir.trim().isEmpty()) {
         File testDir = new File(resPath + File.separator+ DEFAULT_PLUGINS_DIR);
         pluginsDir = testDir.isDirectory() ? testDir.getAbsolutePath() : resPath;
      }
   }

   private String getDefaultResourcePath() {
      String defResPath = null;
      List<Resource> defResList = project.getBuild().getTestResources();
      if (!defResList.isEmpty() && defResList.get(0) != null) {
         defResPath = defResList.get(0).getDirectory();
      }
      return defResPath;
   }

}

@@ -2324,36 +2324,41 @@
 %0A%0A      
-System.out.println(%22
+getLog().info(%22PerfCake: 
 Running 
@@ -2376,39 +2376,24 @@
 + scenario);
- //TODO use log
 %0A      Scena
@@ -2460,32 +2460,41 @@
     
-System.out.println(%22DONE
+getLog().info(%22PerfCake: Finished
  sce
@@ -2517,23 +2517,8 @@
 io);
- //TODO use log
 %0A   
@@ -2831,32 +2831,109 @@
 th() : resPath;%0A
+         getLog().debug(%22Setting PerCake scenarios dir to %22 + scenariosDir);%0A
       %7D%0A%0A      i
@@ -3149,32 +3149,107 @@
 th() : resPath;%0A
+         getLog().debug(%22Setting PerCake messages dir to %22 + messagesDir);%0A
       %7D%0A%0A      i
@@ -3377,32 +3377,32 @@
 T_PLUGINS_DIR);%0A
-
          plugins
@@ -3464,24 +3464,97 @@
  : resPath;%0A
+         getLog().debug(%22Setting PerCake plugins dir to %22 + pluginsDir);%0A
       %7D%0A   %7D

Add logging