package hudson.plugins.sap.utils;

import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;
import org.codehaus.plexus.util.cli.WriterStreamConsumer;

import java.io.File;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class NeoCommandLine {
    private String host;

    private String account;

    private String user;

    private String password;

    private String appName;

    private String sourceLocation;

    private String neosdk;

    public void setHost(String host) {
        this.host = host;
    }

    public String getHost() {
        return host;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getAccount() {
        return account;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getUser() {
        return user;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getAppName() {
        return appName;
    }

    public void setSourceLocation(String sourceLocation) {
        this.sourceLocation = sourceLocation;
    }

    public String getSourceLocation() {
        return sourceLocation;
    }

    public void setNeosdk(String neosdk) {
        this.neosdk = neosdk;
    }

    public String getNeosdk() {
        return neosdk;
    }

    public void deployApplication(PrintStream logger) {
        Commandline commandline = new Commandline();

        if (System.getProperty("os.name").startsWith("Windows"))
            commandline.setExecutable(neosdk + File.separator + "tools" + File.separator + "neo.bat");
        else
            commandline.setExecutable(neosdk + File.separator + "tools" + File.separator + "neo.sh");

        commandline.setWorkingDirectory(neosdk + File.separator + "tools");
        commandline.addArguments(new String[]{"deploy"});
        commandline.addArguments(new String[]{"--host", host});
        commandline.addArguments(new String[]{"--account", account});
        commandline.addArguments(new String[]{"--user", user});
        commandline.addArguments(new String[]{"--password", password});
        commandline.addArguments(new String[]{"--application", appName});
        commandline.addArguments(new String[]{"--source", sourceLocation});

        logger.println("Executing command : ");

        StringBuffer commandForm = new StringBuffer();
        for (String command: commandline.getCommandline()) {
            commandForm = commandForm.append(command).append(" ");
        }
        logger.println(commandForm.toString());

        try {
            WriterStreamConsumer systemOut = new WriterStreamConsumer(new OutputStreamWriter(System.out, "UTF-8"));
            CommandLineUtils.executeCommandLine(commandline, systemOut, systemOut);
            logger.println("command successfully executed");
        } catch (UnsupportedEncodingException e) {
            logger.println(e.getMessage());
        }
          catch (CommandLineException e) {
            logger.println(e.getMessage());
        }
    }
}
@@ -2706,16 +2706,55 @@
 uffer();
+%0A%0A        boolean passwordFlag = false;
 %0A       
@@ -2823,61 +2823,324 @@
     
-commandForm = commandForm.append(command).append(%22 %22)
+if (passwordFlag) %7B%0A                commandForm = commandForm.append(%22****%22).append(%22 %22);%0A                passwordFlag = false;%0A            %7D%0A            else%0A                commandForm = commandForm.append(command).append(%22 %22);%0A%0A            if (command.startsWith(%22--password%22))%0A                passwordFlag = true
 ;%0A  

fix for showing password