#include "ssh_subsystem.h"

SshSubsystem::SshSubsystem(char *hostname, char *username, char*password)
{
	int port=22;

	session = ssh_new(); if (session==NULL) { cout << "Error starting the SSH Subsystem" << endl; return; }
	ssh_options_set(session, SSH_OPTIONS_HOST, hostname);
	ssh_options_set(session, SSH_OPTIONS_PORT, &port);
	rc = ssh_connect(session); if (rc != SSH_OK) { cout << "Line 10 " << ssh_get_error(session) << endl; return; }
	rc = ssh_userauth_password(session, username, password); if (rc != SSH_OK) { cout << "Line 11 " << ssh_get_error(session) << endl; return; }
}

int SshSubsystem::getFile(char *remotefile, char *localfile)
{
	ofstream file;
	scp = ssh_scp_new(session, SSH_SCP_READ, remotefile);
	rc = ssh_scp_init(scp); if (rc != SSH_OK) { cout << "Line 17 " << ssh_get_error(session) << endl; return -1; }
	rc = ssh_scp_pull_request(scp);
	int filesize = ssh_scp_request_get_size(scp);
	const char *filename = ssh_scp_request_get_filename(scp);
	buffer = new char[filesize];
	ssh_scp_accept_request(scp);
	ssh_scp_read(scp,buffer,filesize);
	file.open(localfile, ios::binary);
	file.write(buffer, filesize);
	file.close();
	delete [] buffer;
	ssh_scp_close(scp);
	return 0;
}

int SshSubsystem::sendFile(char *localfile, char *remotefile)
{
	return 0;
}