package org.jenkinsci.plugins.koji;

import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;

import java.io.File;
import java.io.IOException;

public class KojiLauncher {

    private final String workspacePath;
    private String[] command;

    private final AbstractBuild<?,?> build;
    private final BuildListener listener;
    private Launcher launcher;

    public KojiLauncher(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) {
        this.build = build;
        this.listener = listener;
        this.launcher = launcher;

        workspacePath = initWorkspacePath();
    }

    private String initWorkspacePath() {
        String workspace = "";
        try {
            workspace = build.getWorkspace().absolutize().getRemote();
        } catch (IOException e) {
            listener.getLogger().println("[Koji integration] Error executing Koji command.");
            listener.getLogger().println(e.getMessage());
            return null;
        } catch (InterruptedException e) {
            listener.getLogger().println("[Koji integration] Error executing Koji command.");
            listener.getLogger().println(e.getMessage());
            return null;
        }

        return workspace;
    }

    void moshiMoshiCommand() {
        command =  new String[]{"koji", "moshimoshi"};
    }

    void mavenBuildCommand(String options, String target, String sources) {
        command =  new String[]{"koji", "maven-build", "-Dmaven.test.skip=true", options, target, sources};
    }

    void watchTaskCommand(String taskId) {
        command =  new String[]{"koji", "watch-task", taskId};
    }

    void downloadCommand(String kojiBuild) {
        command =  new String[]{"koji", "download-build", "--type=maven", kojiBuild};
    }

    public boolean callKoji() {
        boolean successfull = true;

        successfull = (workspacePath != null);
        if (!successfull) return successfull;

//        listener.getLogger().println("[Koji integration] Workspace path: " + workspacePath);

        try {
            int exitCode = launcher.launch().cmds(command).envs(build.getEnvironment(listener)).pwd(build.getWorkspace()).stdout(listener).join();
            successfull = (exitCode == 0);
        } catch (IOException e) {
            listener.getLogger().println("[Koji integration] Error executing Koji command.");
            listener.getLogger().println(e.getMessage());
            return false;
        } catch (InterruptedException e) {
            listener.getLogger().println("[Koji integration] Error executing Koji command.");
            listener.getLogger().println(e.getMessage());
            return false;
        }

        return successfull;
    }
}

@@ -1438,33 +1438,45 @@
 rces) %7B%0A        
-c
+String%5B%5D tmpC
 ommand =  new St
@@ -1531,25 +1531,16 @@
 =true%22, 
-options, 
 target, 
@@ -1548,16 +1548,182 @@
 ources%7D;
+%0A        if (options.equals(%22%22)) %7B%0A            command = tmpCommand;%0A        %7D else %7B%0A            command = concatenate(tmpCommand, new String%5B%5D %7Boptions%7D);%0A        %7D
 %0A    %7D%0A%0A
@@ -2913,11 +2913,317 @@
 l;%0A    %7D
+%0A%0A    public static String%5B%5D concatenate(String%5B%5D A, String%5B%5D B) %7B%0A        int aLength = A.length;%0A        int bLength = B.length;%0A        String%5B%5D C = new String%5BaLength + bLength%5D;%0A        System.arraycopy(A, 0, C, 0, aLength);%0A        System.arraycopy(B, 0, C, aLength, bLength);%0A        return C;%0A    %7D
 %0A%7D%0A

Fix number of arguments issue for non-scratch builds