package gZFS;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Properties;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class libConnect {

	private String userLogin;
	private String userPassword;
	private String sshHost;
	private int sshPort;
	private Session session;
	private JSch jsch = new JSch();

	public String connect(String cmd) {
		String outCmd = null ;
		try {
			
			setSession(jsch.getSession(getUserLogin(), getSshHost(), getSshPort()));
			session.setPassword(this.getUserPassword());
			Properties config = new Properties();
			config.put("StrictHostKeyChecking", "no");
			session.setConfig(config);
			session.connect();
			if ( session.isConnected()){
				System.out.println("Connected");
			}else{
				System.out.println("not Connected");
			}
			ChannelExec channel = (ChannelExec) session.openChannel("exec");
			//((ChannelExec) channel).setCommand("ls");
			((ChannelExec) channel).setCommand(cmd);
			InputStream in = channel.getInputStream();
			OutputStream out = channel.getOutputStream();
			((ChannelExec) channel).setErrStream(System.err);

			channel.connect();
			byte[] tmp = new byte[1024];
			
			while (true) {
				
				int ind = 0;
				while (in.available() > 0) {
					int i = in.read(tmp, 0, 1024);
					if (i < 0)
						break;
					
					outCmd= new String(tmp, 0, i);
					//System.out.println(toto);
					System.out.println(outCmd);
					//System.out.print(new String(tmp, 0, i));
					
				}
				if (channel.isClosed()) {
					break;
				}
				try {
					Thread.sleep(1000);
				} catch (Exception ee) {
					System.out.println(ee);
				}
			}
			channel.disconnect();
					
		} catch (JSchException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		/*for (int cpt=0;cpt < outCmd.length;cpt++ ) {
			System.out.println(outCmd[cpt]);
		}*/
		System.out.println(outCmd);
		return outCmd;
	}

 
  
  public String zfsGetAllVolume(String volume){
	 return connect("zfs list");
	
//	  return this.executeCmd("/sbin/zfs get all "+volume);
  }
	public String getSshHost() {
		return sshHost;
	}

	public void setSshHost(String sshHost) {
		this.sshHost = sshHost;
	}

	public String getUserLogin() {
		return userLogin;
	}

	public void setUserLogin(String userLogin) {
		this.userLogin = userLogin;
	}


	public int getSshPort() {
		return sshPort;
	}


	public void setSshPort(int sshPort) {
		this.sshPort = sshPort;
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

}
