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 final 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();
        command = constructCommand();
    }

    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;
    }

    private String[] constructCommand() {
        return new String[]{"koji", "moshimoshi"};
    }

    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;
    }
}

@@ -252,30 +252,24 @@
     private 
-final 
 String%5B%5D com
@@ -274,16 +274,16 @@
 ommand;%0A
+
 %0A    pri
@@ -634,46 +634,8 @@
 ();%0A
-        command = constructCommand();%0A
     
@@ -1277,42 +1277,327 @@
     
-private String%5B%5D constructCommand(
+void moshiMoshiCommand() %7B%0A        command =  new String%5B%5D%7B%22koji%22, %22moshimoshi%22%7D;%0A    %7D%0A%0A    void mavenBuildCommand(String options, String target, String sources) %7B%0A        command =  new String%5B%5D%7B%22koji%22, %22maven-build%22, %22-Dmaven.test.skip=true%22, options, target, sources%7D;%0A    %7D%0A%0A    void watchTaskCommand(String taskId
 ) %7B%0A
@@ -1604,22 +1604,26 @@
         
-return
+command = 
  new Str
@@ -1633,35 +1633,43 @@
 %5B%5D%7B%22koji%22, %22
-moshimoshi%22
+watch-task%22, taskId
 %7D;%0A    %7D%0A%0A  

Add more commands to KojiLauncher