/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2016 Alejandro P. Revilla
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jpos.q2.ssh;

import org.apache.sshd.common.Factory;
import org.apache.sshd.common.SshConstants;
import org.apache.sshd.server.*;
import org.apache.sshd.server.session.ServerSession;
import org.jpos.q2.CLI;
import org.jpos.q2.Q2;
import org.jpos.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CliShellFactory implements Factory<Command>, CommandFactory {
    Q2 q2;
    String[] prefixes;

    public CliShellFactory(Q2 q2, String[] prefixes) {
        this.q2 = q2;
        this.prefixes = prefixes;
    }

    public Command create() {
        return new JPosCLIShell(null);
    }

    @Override
    public Command createCommand(String command) {
        return new JPosCLIShell(command);
    }

    public class JPosCLIShell implements Command, SessionAware {
        InputStream in;
        OutputStream out;
        OutputStream err;
        SshCLI cli = null;
        ServerSession serverSession;
        String args;

        public void setInputStream(InputStream in) {
            this.in = in;
        }

        public void setOutputStream(OutputStream out) {
            this.out = out;
        }

        public void setErrorStream(OutputStream err) {
            this.err = err;
        }

        public void setExitCallback(ExitCallback exitCallback) {
        }

        public JPosCLIShell(String args) {
            this.args = args;
        }

        public void setSession(ServerSession serverSession) {
            this.serverSession = serverSession;
        }

        public void start(Environment env) throws IOException {
            cli = new SshCLI(q2, args != null ? null : in, out, args, args == null);
            try {
                cli.setServerSession(serverSession);
                cli.start();
            } catch (Exception e) {
                throw new IOException("Could not start", e);
            }
        }

        public void destroy() {
            if (cli != null) {
                cli.stop();
            }
        }
    }

    public class SshCLI extends CLI {
        ServerSession serverSession = null;

        public SshCLI(Q2 q2, InputStream in, OutputStream out, String line, boolean keepRunning) throws IOException {
            super(q2, in, out, line, keepRunning);
        }

        protected boolean running() {
            return !ctx.isStopped();
        }

        protected void markStopped() {
            ctx.setStopped(true);
        }

        protected void markStarted() {
            ctx.setStopped(false);
        }

        protected String[] getCompletionPrefixes() {
            return prefixes;
        }

        protected String getPrompt() {
            return "q2> ";
        }

        protected void handleExit() {
            if (serverSession != null) {
                try {
                    serverSession.disconnect(SshConstants.SSH2_DISCONNECT_BY_APPLICATION, "");
                } catch (IOException e) {
                    Log.getLog(Q2.LOGGER_NAME, "sshd").error(e);
                }
            }
        }

        public void setServerSession(ServerSession serverSession) {
            this.serverSession = serverSession;
        }
    }
}

@@ -3421,85 +3421,8 @@
  %7D%0A%0A
-        protected String getPrompt() %7B%0A            return %22q2%3E %22;%0A        %7D%0A%0A
     

Use default 'q2>' prompt