#!/usr/bin/env python

from __future__ import print_function

import argparse
import subprocess

__author__ = "Begon Jean-Michel <jm.begon@gmail.com>"
__copyright__ = "3-clause BSD License"


__LH__ = "localhost"

# TODO do something more portable
__CT_FOLDER__ = "~/clustertools_data/"


if __name__ == '__main__':
    description = "Clustertools task synchro script"
    source_doc = """The source.
Use scp-like syntax: 'luke@skywalker:exp_about_the_force' to specify that you
are targeting experiment 'exp_about_the_force' on machine 'skywalker' where your
id is 'luke'. Use a dot '.' for localhost."""
    dest_doc = """The destination.
Use the syntax: 'vador@deathstar' to specify that you the destination is
machine 'deathstar' where your id is 'vador'. Use a dot '.' for localhost."""

    parser = argparse.ArgumentParser(description=description)
    parser.add_argument("source", help=source_doc)
    parser.add_argument("destination", help=dest_doc)
    parser.add_argument("--quiet", "-q", default=False, action="store_true",
                        help="Runs in quiet mode.")
    parser.add_argument("--test", "-t", default=False, action="store_true",
                        help="print the command instead of running it.")

    # TODO allow for some flexibility
    # parser.add_argument("--results", action="store_false", default=True,
    #                     help="Disable result synchronization.")
    # parser.add_argument("--notifications", action="store_false", default=True,
    #                     help="Disable notification synchronization.")
    # parser.add_argument("--logs", action="store_true", default=False,
    #                     help="Disable log synchronization.")

    args = parser.parse_args()

    source_info = args.source.split(":")
    dest_info = args.destination.split(":")

    experiment = None
    if len(source_info) == 1:
        source = "."
        experiment = source_info[0]
    else:
        source, experiment = source_info

    if len(dest_info) == 1 or (len(dest_info) == 2 and len(dest_info[1]) == 0):
        destination = dest_info[0]
    elif experiment is not None:
        raise ValueError("Only the source can specify an experiment.")

    print("Transferring {} from {} to {}".format(experiment, source,
                                                 destination))

    if source == ".":
        source = __CT_FOLDER__
    else:
        source += ":"+__CT_FOLDER__
    if destination == ".":
        destination = __CT_FOLDER__
    else:
        destination += ":" + __CT_FOLDER__

    # TODO: do something more portable
    source += "exp_" + experiment + "/"
    destination += "exp_" + experiment

    if args.quiet:
        r_args = "-az"
    else:
        r_args = "-azP"

    cmd = "rsync {r_args} {source} {dest}".format(r_args=r_args, source=source,
                                                  dest=destination)

    if args.test:
        print(cmd)
    else:
        subprocess.call(cmd, shell=True)


