package jetbrains.buildServer.autotools.agent;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.VisibleForTesting;
import com.intellij.execution.configurations.GeneralCommandLine;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jetbrains.buildServer.ExecResult;
import jetbrains.buildServer.SimpleCommandLineProcessRunner;
import jetbrains.buildServer.agent.*;
import jetbrains.buildServer.util.EventDispatcher;
import org.jetbrains.annotations.NotNull;
import static jetbrains.buildServer.autotools.common.AutotoolsBuildConstants.*;

/**
 * Created on 26.07.2017.
 * Author     : Nadezhda Demina
 */
public class AutotoolsLifeCycleListener extends AgentLifeCycleAdapter {

  /**
   * Does autotools test report
   */
  private AutotoolsTestsReporter testReporter;
  AutotoolsLifeCycleListener(EventDispatcher<AgentLifeCycleListener> dispatcher) {
    dispatcher.addListener(this);
  }


  }

  @Override
  public void beforeRunnerStart(@NotNull final BuildRunnerContext runner) {
    myRuntestToolProvider = (RuntestToolProvider) myProvidersRegistry.findToolProvider(TOOL_RUNTEST);
                                              runner.getBuild().getCheckoutDirectory().getAbsolutePath() + "/",
                                              Boolean.parseBoolean(runner.getRunnerParameters().get(UI_NEED_DEJAGNU_VALID_XML)));
    testReporter.findDejagnu(runner.getBuild().getCheckoutDirectory());
  }



  @Override
  public void runnerFinished(@NotNull final BuildRunnerContext runner, @NotNull final BuildFinishedStatus status) {
    super.runnerFinished(runner, status);
    testReporter.searchTestsFiles(runner.getBuild().getCheckoutDirectory());
    testReporter.doTestsReport();
  }
}

@@ -214,16 +214,69 @@
 ndLine;%0A
+import com.intellij.util.text.VersionComparatorUtil;%0A
 import j
@@ -851,16 +851,61 @@
 t%0A   */%0A
+  RuntestToolProvider myRuntestToolProvider;%0A
   privat
@@ -929,17 +929,19 @@
 eporter 
-t
+myT
 estRepor
@@ -945,16 +945,69 @@
 porter;%0A
+  private ToolProvidersRegistry myProvidersRegistry;%0A
   Autoto
@@ -1081,51 +1081,139 @@
 cher
-) %7B%0A    dispatcher.addListener(this);%0A  %7D%0A%0A
+, ToolProvidersRegistry toolProvidersRegistry) %7B%0A    dispatcher.addListener(this);%0A    myProvidersRegistry = toolProvidersRegistry;
 %0A  %7D
@@ -1404,16 +1404,232 @@
 NTEST);%0A
+    if (myRuntestToolProvider != null)%7B%0A      myRuntestToolProvider.setDejagnuParameters(runner);%0A    %7D%0A    myTestReporter = new AutotoolsTestsReporter(System.currentTimeMillis(), runner.getBuild().getBuildLogger(),%0A
         
@@ -1858,29 +1858,32 @@
 LID_XML)));%0A
+%0A
     
-t
+myT
 estReporter.
@@ -2114,25 +2114,27 @@
 tatus);%0A    
-t
+myT
 estReporter.
@@ -2197,17 +2197,19 @@
 ));%0A    
-t
+myT
 estRepor
@@ -2229,14 +2229,172 @@
 port();%0A
+    if (myRuntestToolProvider != null && VersionComparatorUtil.compare(myRuntestToolProvider.getVersion(), (%221.4.4%22)) %3C= 0)%7B%0A      // public artifacts;%0A    %7D%0A
   %7D%0A%7D%0A

add myProvidersRegistry and call myRuntestToolProvider.setDejagnuParameters