package com.intropro.prairie.unit.cmd;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by presidentio on 11/26/15.
 */
public class CommandClient {

    private static final Logger LOGGER = LogManager.getLogger(CommandsServer.class);

    @Parameter(names = "-h")
    private String host;

    @Parameter(names = "-p")
    private int port;

    @Parameter(names = "-c")
    private String command;

    @Parameter
    private List<String> args = new ArrayList<>();

    private int exitStatus;

    public static void main(String[] args) throws IOException, InterruptedException {
        CommandClient commandClient = new CommandClient();
        new JCommander(commandClient, args);
        System.exit(commandClient.exec());
    }

    public int exec() throws IOException, InterruptedException {
        System.err.println("Executing command " + command + " on " + host + ":" + port);
        Socket socket = new Socket(host, port);
        socket.getOutputStream().write((command + "\n").getBytes());
        socket.getOutputStream().write((StringUtils.join(args, ' ') + "\n").getBytes());
        StreamRedirect inStreamRedirect = new StreamRedirect(System.in, socket.getOutputStream());
        inStreamRedirect.start();
        StreamRedirect outStreamRedirect = new StreamRedirect(socket.getInputStream(), System.out) {
            @Override
            protected void handleLine(String line) throws IOException {
                if (line.startsWith(CommandProcessor.FIRST_LINE_MARKER)) {
                    super.write(line.replace(CommandProcessor.FIRST_LINE_MARKER, ""));
                } else {
                    if (isEof()) {
                        exitStatus = Integer.valueOf(line);
                    } else {
                        super.write("\n" + line);
                    }
                }
            }
        };
        outStreamRedirect.start();
        inStreamRedirect.join();
        socket.shutdownOutput();
        outStreamRedirect.join();
        System.err.println("Finished command " + command + " on " + host + ":" + port);
        return exitStatus;
    }

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

    public CommandClient setPort(int port) {
        this.port = port;
        return this;
    }

    public CommandClient setCommand(String command) {
        this.command = command;
        return this;
    }

    public CommandClient setArgs(List<String> args) {
        this.args = args;
        return this;
    }
}

@@ -319,35 +319,9 @@
 rray
-List;%0Aimport java.util.List
+s
 ;%0A%0A/
@@ -651,23 +651,8 @@
 d;%0A%0A
-    @Parameter%0A
     
@@ -663,45 +663,21 @@
 ate 
-List%3C
 String
-%3E
+%5B%5D
  args
- = new ArrayList%3C%3E()
 ;%0A%0A 
@@ -856,16 +856,40 @@
 %0A       
+ JCommander jCommander =
  new JCo
@@ -913,14 +913,139 @@
 ient
-, args
+);%0A        jCommander.parse(Arrays.copyOfRange(args, 0, 6));%0A        commandClient.setArgs(Arrays.copyOfRange(args, 6, args.length)
 );%0A 
@@ -1236,32 +1236,73 @@
 ost + %22:%22 + port
+ + %22 with args: %22 + Arrays.toString(args)
 );%0A        Socke
@@ -2857,20 +2857,16 @@
 rgs(
-List%3C
 String
-%3E
+%5B%5D
  arg

fix cmd unit transport