package ssh;

import com.jscape.inet.ssh.*;
import java.io.*;
import java.util.ArrayList;
public class SshConnect extends SshAdapter {

    private String shellPrompt = "$";
    // state of SSH connection
    public boolean connect = false;
    public SshSession session = null;
    public String hostname = "hallg.inf.unideb.hu";
    public String username = "";
    public String password = "";

    public SshConnect(String username, String password) throws SshException {
        this.username = username;
        this.password = password;
        session = new SshSession(this.hostname, this.username, this.password);
        session.setShellPrompt(shellPrompt);
        session.addSshListener(this);
        session.connect();
    }

    /**
     * Captures SshConnectedEvent
     */
    public void connected(SshConnectedEvent ev) {
        System.out.println("Connected: " + ev.getHost());
        connect = true;
    }

    /**
     * Captures SshDataReceivedEvent
     */
    public void dataReceived(SshDataReceivedEvent ev) {
        // send data received to console
        //System.out.print(ev.getData());
    }

    /**
     * Captures SshDisconnectedEvent
     */
    public void disconnected(SshDisconnectedEvent ev) {       
        connect = false;
    }

    public String Bead(String feladatszam) throws SshException {
        if (this.goToDir(feladatszam)) {
            String cmd = "prog1-bead " + feladatszam;
            String answer = encoding(session.send(cmd));
            System.out.println(answer);
            return answer;
        }
        return "Nincs ilyen könyvtár!";
    }

    public String Teszt(String feladatszam) throws SshException {
        if (this.goToDir(feladatszam)) {
            String cmd = "prog1-teszt " + feladatszam;
            String answer = encoding(session.send(cmd));
            System.out.println(answer);
            return answer;
        }
        return "Nincs ilyen könyvtár!";
    }

    public boolean goToDir(String feladatszam) throws SshException {
        String cmd = "";
        String[] result = feladatszam.split("/");
        for (int x = 1; x < result.length; x++) {
            cmd = "cd " + result[x];
            if (!this.isDir(result[x])) {
                return false;
            }
            String answer = encoding(session.send(cmd));
            System.out.println(answer);
            }
        return true;
    }

    public boolean isDir(String dir) throws SshException {
        String cmd = "ls | grep " + dir + " | wc -l";
        String answer = encoding(session.send(cmd));
        System.out.println(answer);
        if (Integer.parseInt(answer.substring(0, 1)) != 0) {
            return true;
        }
        return false;
    }

    public void createDir(String dir) throws SshException {
        String[] result = dir.split("/");
        if (!this.isDir(result[1])) {
            session.send("mkdir " + result[1]);
            session.send("cd " + result[1]);
            session.send("mkdir " + result[2]);
        } else {
            session.send("cd " + result[1]);
            if (!this.isDir(result[2])) {
                session.send("mkdir " + result[2]);
            } else {
                session.send("rm -r " + result[2]);
                session.send("mkdir " + result[2]);
            }
        }
        session.send("cd /home/"+username);
    }

    public String Results() throws SshException {
        String cmd = "prog1-eredmeny";
        String answer = encoding(session.send(cmd));
        System.out.println(answer);
        return answer;
    }

    public String encoding(String answer) {
        InputStreamReader reader = null;
        String s = "";
        try {
            InputStream bi = new ByteArrayInputStream(answer.getBytes());
            reader = new InputStreamReader(bi, "UTF-8");
            Reader in = new BufferedReader(reader);
            ArrayList<Byte> buffer = new ArrayList<Byte>();
            int ch;
            while ((ch = in.read()) > -1) {
                buffer.add((byte) ch);
            }
            byte[] b = new byte[buffer.size()];
            for (int i = 0; i < buffer.size(); i++) {
                b[i] = buffer.get(i);
            }
            s = new String(b, "ISO-8859-2");
            in.close();
        } catch (Exception ex) {
        } finally {
            return s;
        }
    }

    public void exit(){
        try {
            session.sendNoWait("exit");
        } catch (IOException ex) {

        }
       
    }

} 
