package jetbrains.buildServer.deployer.server.converter;

import jetbrains.buildServer.deployer.common.DeployerRunnerConstants;
import jetbrains.buildServer.deployer.common.FTPRunnerConstants;
import jetbrains.buildServer.deployer.common.SSHRunnerConstants;
import jetbrains.buildServer.log.Loggers;
import jetbrains.buildServer.serverSide.BuildServerAdapter;
import jetbrains.buildServer.serverSide.SBuildRunnerDescriptor;
import jetbrains.buildServer.serverSide.SBuildServer;
import jetbrains.buildServer.serverSide.SBuildType;
import jetbrains.buildServer.util.StringUtil;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Nikita.Skvortsov
 * date: 29.07.13.
 */
public class DeployerSettingsConverter extends BuildServerAdapter {

    private final SBuildServer myServer;

    public DeployerSettingsConverter(@NotNull SBuildServer server) {
        myServer = server;
        myServer.addListener(this);
    }



    @Override
    public void buildTypeRegistered(SBuildType buildType) {
        boolean persistBuildType = false;
        for (SBuildRunnerDescriptor descriptor : buildType.getBuildRunners()) {
            final String runnerType = descriptor.getType();
            final Map<String,String> newRunnerParams = new HashMap<String, String>(descriptor.getParameters());

            final String plainPassword = newRunnerParams.get(DeployerRunnerConstants.PARAM_PLAIN_PASSWORD);
            if (plainPassword != null) {
                persistBuildType = true;
                Loggers.SERVER.debug("Scrambling password for runner [" + runnerType + "-" + descriptor.getName() + "] in [" + buildType.getName() + "]");
                newRunnerParams.remove(DeployerRunnerConstants.PARAM_PLAIN_PASSWORD);
                newRunnerParams.put(DeployerRunnerConstants.PARAM_PASSWORD, plainPassword);
                buildType.updateBuildRunner(descriptor.getId(), descriptor.getName(), runnerType, newRunnerParams);
            }

            if (DeployerRunnerConstants.SSH_RUN_TYPE.equals(runnerType) ||
                    SSHRunnerConstants.SSH_EXEC_RUN_TYPE.equals(runnerType)) {
                final String sshAuthMethod = newRunnerParams.get(SSHRunnerConstants.PARAM_AUTH_METHOD);
                if (StringUtil.isEmpty(sshAuthMethod)) {
                    persistBuildType = true;
                    Loggers.SERVER.debug("Setting default (username password) ssh authentication method for runner [" + runnerType + "-" + descriptor.getName() + "] in [" + buildType.getName() + "]");
                    newRunnerParams.put(SSHRunnerConstants.PARAM_AUTH_METHOD, SSHRunnerConstants.AUTH_METHOD_USERNAME_PWD);
                    buildType.updateBuildRunner(descriptor.getId(), descriptor.getName(), runnerType, newRunnerParams);
                }
                if (SSHRunnerConstants.SSH_EXEC_RUN_TYPE.equals(runnerType)) {
                    final String oldUsername = newRunnerParams.get(SSHRunnerConstants.PARAM_USERNAME);
                    final String oldPassword = newRunnerParams.get(SSHRunnerConstants.PARAM_PASSWORD);
                    if (StringUtil.isNotEmpty(oldUsername)) {
                        persistBuildType = true;
                        newRunnerParams.remove(SSHRunnerConstants.PARAM_USERNAME);
                        newRunnerParams.put(DeployerRunnerConstants.PARAM_USERNAME, oldUsername);
                    }
                    if (StringUtil.isNotEmpty(oldPassword)) {
                        persistBuildType = true;
                        newRunnerParams.remove(SSHRunnerConstants.PARAM_PASSWORD);
                        newRunnerParams.put(DeployerRunnerConstants.PARAM_PASSWORD, oldPassword);
                    }
                }
            }

            if (DeployerRunnerConstants.FTP_RUN_TYPE.equals(runnerType)) {
                final String ftpAuthMethod = newRunnerParams.get(FTPRunnerConstants.PARAM_AUTH_METHOD);
                if (StringUtil.isEmpty(ftpAuthMethod)) {
                    persistBuildType = true;
                    Loggers.SERVER.debug("Setting ftp auth authentication method for runner [" + runnerType + "-" + descriptor.getName() + "] in [" + buildType.getName() + "]");
                    final String username = newRunnerParams.get(DeployerRunnerConstants.PARAM_USERNAME);
                    if (StringUtil.isEmpty(username)) {
                        newRunnerParams.put(FTPRunnerConstants.PARAM_AUTH_METHOD, FTPRunnerConstants.AUTH_METHOD_ANONYMOUS);
                    } else {
                        newRunnerParams.put(FTPRunnerConstants.PARAM_AUTH_METHOD, FTPRunnerConstants.AUTH_METHOD_USER_PWD);
                    }
                    buildType.updateBuildRunner(descriptor.getId(), descriptor.getName(), runnerType, newRunnerParams);
                }
            }
        }
        if (persistBuildType) {
            buildType.persist();
        }
    }

    @Override
    public void serverStartup() {
        myServer.removeListener(this);
        Loggers.SERVER.debug("Server started up, will not convert passwords in configurations from now on.");
    }
}

@@ -1867,124 +1867,8 @@
 d);%0A
-                buildType.updateBuildRunner(descriptor.getId(), descriptor.getName(), runnerType, newRunnerParams);%0A
     
@@ -2567,128 +2567,8 @@
 D);%0A
-                    buildType.updateBuildRunner(descriptor.getId(), descriptor.getName(), runnerType, newRunnerParams);%0A
     
@@ -2858,32 +2858,127 @@
 ARAM_PASSWORD);%0A
+                    final String oldHost = newRunnerParams.get(SSHRunnerConstants.PARAM_HOST);%0A
                 
@@ -3581,32 +3581,336 @@
               %7D%0A
+                    if (StringUtil.isNotEmpty(oldHost)) %7B%0A                        persistBuildType = true;%0A                        newRunnerParams.remove(SSHRunnerConstants.PARAM_HOST);%0A                        newRunnerParams.put(DeployerRunnerConstants.PARAM_TARGET_URL, oldHost);%0A                    %7D%0A
                 
@@ -4846,32 +4846,56 @@
       %7D%0A        
+        %7D%0A            %7D%0A
             buil
@@ -4994,40 +4994,8 @@
 s);%0A
-                %7D%0A            %7D%0A
     

fix conversion of old ssh exec settings