package jetbrains.buildServer.autotools.agent;

import com.google.common.annotations.VisibleForTesting;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jetbrains.buildServer.agent.AgentLifeCycleListener;
import jetbrains.buildServer.agent.ToolProvidersRegistry;
import jetbrains.buildServer.util.EventDispatcher;
import org.jetbrains.annotations.NotNull;

/**
 * Created on 14.08.2017.
 * Author     : Nadezhda Demina
 */
public class RuntestToolProvider extends AutotoolsToolProvider {


  @VisibleForTesting
  RuntestToolProvider(@NotNull String toolName, @NotNull String versionArg){
    super(toolName, versionArg);
  }
  public RuntestToolProvider(@NotNull final ToolProvidersRegistry toolProvidersRegistry, @NotNull final EventDispatcher<AgentLifeCycleListener> eventDispatcher, @NotNull final String toolName, @NotNull final String versionArg) {
    super(toolProvidersRegistry, eventDispatcher, toolName, versionArg);
  }

  @NotNull
  @VisibleForTesting
  @Override
  String findVersion(@NotNull final String text) {
    final Pattern needVersion = Pattern.compile("(DejaGnu|Framework).+version.+" + regVersionNumer);
    Matcher matcher = needVersion.matcher(text);
    if (!matcher.find()){
      return "";
    }
    final String versionLine = text.substring(matcher.start(), matcher.end());
    final Pattern versionNumberOnly = Pattern.compile(regVersionNumer);
    matcher = versionNumberOnly.matcher(versionLine);
    return matcher.find() ? versionLine.substring(matcher.start(), matcher.end()) : "";
  }
}

@@ -97,16 +97,106 @@
 esting;%0A
+import com.intellij.openapi.util.Pair;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A
 import j
@@ -306,24 +306,79 @@
 leListener;%0A
+import jetbrains.buildServer.agent.BuildRunnerContext;%0A
 import jetbr
@@ -485,41 +485,179 @@
 ort 
-org.jetbrains.annotations.NotNull
+jetbrains.buildServer.util.VersionComparatorUtil;%0Aimport org.jetbrains.annotations.NotNull;%0A%0Aimport static jetbrains.buildServer.autotools.common.AutotoolsBuildConstants.*
 ;%0A%0A/
@@ -1820,11 +1820,923 @@
  %22%22;%0A  %7D
+%0A%0A  /**%0A   * Returns need runner parameters dependence current version runtest%0A   * @param version current version runtest%0A   * @return list of need parameters%0A   */%0A  @NotNull%0A  @VisibleForTesting%0A  static List%3CPair%3CString, String%3E%3E getDejagnuParameters(@NotNull final String version)%7B%0A    List%3CPair%3CString, String%3E%3E params = new ArrayList%3CPair%3CString, String%3E%3E();%0A    params.add(new Pair%3CString, String%3E(HAS_RUNTEST_VAR, %221%22));%0A    if (VersionComparatorUtil.compare(version, %221.4.4%22) %3C= 0)%7B%0A      params.add(new Pair%3CString, String%3E(MY_RUNTESTFLAGS, %22RUNTESTFLAGS=--all%22));%0A      return params;%0A    %7D%0A    if (VersionComparatorUtil.compare(version, %221.5.3%22) %3C 0)%7B%0A        params.add(new Pair%3CString, String%3E(MY_RUNTESTFLAGS, %22RUNTESTFLAGS=--all --xml%22));%0A      return params;%0A    %7D%0A    params.add(new Pair%3CString, String%3E(MY_RUNTESTFLAGS, %22RUNTESTFLAGS=--all --xml=%5C%22testresults.xml%5C%22%22));%0A    return params;%0A  %7D
 %0A%7D%0A

add method getDejagnuParameters for different versions of runtest