import paramiko
import sys
import json

def load_config(config_file):
    try:
        with open(config_file) as infile:
            try:
                json_dict = json.load(infile)
                #logging.info(json_dict)
                return json_dict["remotes"], json_dict["jobs"], json_dict["settings"]
            except json.JSONDecodeError as e:
                logging.warning('exception caught: ', exc_info = True)
                sys.exit(1)
    except FileNotFoundError:
        logging.warning('exception caught: ', exc_info = True)
        sys.exit(1)

def create_remote(config, settings):
    type_r = config["remote_type"]
    if type_r == "sftp":
        import remote_sftp
        remote = remote_sftp.sftp_remote(config, settings)
    elif type_r == "local_dir":
        import remote_local_dir
        remote = remote_local_dir.local_dir_remote(config)
    return remote

def distribute_files(path_list, dest):
    for pathfile in path_list:
        if not str(pathfile).endswith('.bsp'):
            continue
        if not dest.put(pathfile, dest.path):
            log_msg = ''.join(["failed putting file: ", str(pathfile)])
            print(log_msg)
            sys.exit(1)

def main():
    config_file = 'config_maps.json'

    ip = sys.argv[1]
    username = sys.argv[2]
    password = sys.argv[3]

    remotes, jobs, settings = load_config(config_file)

    remotes["sftp_ovh_ze_maps_dest"]["hostname"] = ip
    remotes["sftp_ovh_ze_maps_dest"]["username"] = username

    settings["sftp"][ip] =  {}
    settings["sftp"][ip][username] = {"password": password}
    for index, job in enumerate(jobs):
        src = create_remote(remotes[job["src"]], settings)
        dest = create_remote(remotes[job["dest"]], settings)

        source_files = src.list_dir()

        distribute_files(source_files, dest)
        for pathfile in source_files:
            src.delete_local_map(pathfile)

if __name__ == '__main__':
    main()


