"""
 *
 * This file is part of rasdaman community.
 *
 * Rasdaman community is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rasdaman community 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  General Public License for more details.
 *
 * You should have received a copy of the GNU  General Public License
 * along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2003 - 2016 Peter Baumann / rasdaman GmbH.
 *
 * For more information please see <http://www.rasdaman.org>
 * or contact Peter Baumann via <baumann@rasdaman.com>.
 *
"""
from services import executor


class PackageUploader:
    def __init__(self, package_local_file_path, server_info, package_server_file_path):
        """
        The class offers functionality to upload the packaged software to a server.
        :param str package_local_file_path:
        :param ServerInfo server_info:
        :param str package_server_file_path:
        :return:
        """
        self.package_local_file_path = package_local_file_path
        self.server_info = server_info
        self.package_server_file_path = package_server_file_path

    def create_scp_command(self):
        """
        Creates the scp command.
        :return: ScpCommand scp_command: the object representing the scp command
        """
        scp_command = ScpCommand(self.package_local_file_path, self.server_info.user_name, self.server_info.host_name,
                                 self.package_server_file_path)
        return scp_command

    def execute_scp_command(self, scp_command):
        """
        Executes the scp command.
        :param ScpCommand scp_command: the scp command to be executed
        :return:
        """
        executor.executeSudo(scp_command.as_list())

    def upload(self):
        """
        Uploads the package file to the server.
        :return:
        """
        self.execute_scp_command(self.create_scp_command())


class ServerInfo:
    def __init__(self, host_name, user_name):
        """
        Stores connection information for a server.
        :param str host_name: the address of the server.
        :param str user_name: a user on the server.
        :return:
        """
        self.host_name = host_name
        self.user_name = user_name


class ScpCommand:
    def __init__(self, source, user_name, host_name, destination):
        """
        Represents an scp command.
        :param source: the path to the file to be copied.
        :param user_name: the user name for the server.
        :param host_name: the host name of the server.
        :param destination: the path on the server where the file is to be copied.
        :return:
        """
        self.source = source
        self.host_name = host_name
        self.user_name = user_name
        self.destination = destination

    def __str__(self):
        scp_template = "scp  -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no {0} {1}@{2}:{3}"
        scp_command = scp_template.format(self.source, self.user_name, self.host_name, self.destination)
        return scp_command

    def as_list(self):
        scp_second_arg = "{0}@{1}:{2}"
        result = ["scp", self.source, scp_second_arg.format(self.user_name, self.host_name, self.destination)]
        return result
