package jetbrains.buildServer.autotools.agent;

import com.intellij.execution.configurations.GeneralCommandLine;
import jetbrains.buildServer.ExecResult;
import jetbrains.buildServer.SimpleCommandLineProcessRunner;
import jetbrains.buildServer.agent.*;
import jetbrains.buildServer.log.Loggers;
import jetbrains.buildServer.util.EventDispatcher;
import org.jetbrains.annotations.NotNull;

/**
 * Created on 10.08.2017.
 * Author     : Nadezhda Demina
 */
public class AutotoolsToolProvider extends AgentLifeCycleAdapter implements ToolProvider {
  /**
   * Tool Name
   */

  protected final String myToolName;
  protected final String myVersionArg;

  public AutotoolsToolProvider(@NotNull ToolProvidersRegistry toolProvidersRegistry,
                               @NotNull EventDispatcher<AgentLifeCycleListener> eventDispatcher,@NotNull String toolName, @NotNull String versionArg){
    toolProvidersRegistry.registerToolProvider(this);
    eventDispatcher.addListener(this);
    myVersionArg = versionArg;
    myToolName = toolName;
  }


  @Override
  public void beforeAgentConfigurationLoaded(@NotNull BuildAgent agent) {
    if (isExistedTool()){
      Loggers.AGENT.info("AutotoolsToolProvider for tool " + myToolName + " found");
      agent.getConfiguration().addConfigurationParameter(myToolName, myToolName);
      return;
    }

    Loggers.AGENT.info("AutotoolsToolProvider for tool " + myToolName + "did not find");
  }


  /**
   * Returns True if Existed Tool myConfigName
   * @return
   */
  public boolean isExistedTool(){
    final GeneralCommandLine commandLine = new GeneralCommandLine();
    commandLine.setExePath(myToolName);
    commandLine.addParameter(myVersionArg);
    final ExecResult execResult = SimpleCommandLineProcessRunner.runCommand(commandLine, (byte[])null);
    return execResult.getExitCode() == 0;
  }


  @NotNull
  @Override
  public boolean supports(@NotNull final String s) {
    return myToolName.equals(s);
  }

  @NotNull
  @Override
  public String getPath(@NotNull final String s) throws ToolCannotBeFoundException {
    return myToolName;
  }

  @NotNull
  @Override
  public String getPath(@NotNull final String s, @NotNull final AgentRunningBuild agentRunningBuild, @NotNull final BuildRunnerContext buildRunnerContext) throws ToolCannotBeFoundException {
    return myToolName;
  }
}

@@ -56,60 +56,180 @@
 com.
-intellij.execution.configurations.GeneralCommandLine
+google.common.annotations.VisibleForTesting;%0Aimport com.intellij.execution.configurations.GeneralCommandLine;%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern
 ;%0Aim
@@ -687,16 +687,116 @@
 %0A   */%0A%0A
+  protected final static String regVersionNumer = %22%5C%5Cd+(?:%5C%5C.%5C%5Cd+)+%22;%0A  protected String myVersion;%0A
   protec
@@ -864,16 +864,17 @@
 onArg;%0A%0A
+%0A
   public
@@ -2024,50 +2024,500 @@
     
-return execResult.getExitCode() == 0;%0A  %7D%0A
+if (execResult.getExitCode() == 0)%7B%0A      myVersion = findVersion(execResult.getOutLines().toString());%0A    %7D%0A    return execResult.getExitCode() == 0;%0A  %7D%0A%0A  @NotNull%0A  @VisibleForTesting%0A  String findVersion(@NotNull final String text) %7B%0A    final Pattern p = Pattern.compile(regVersionNumer);%0A    final Matcher matcher = p.matcher(text);%0A    return matcher.find() ? text.substring(matcher.start(), matcher.end()) : %22%22;%0A  %7D%0A  @NotNull%0A  public String getVersion()%7B%0A    return myVersion;%0A  %7D
 %0A%0A  

add findVersion and getVersion in AutotoolsToolProvider