import argparse
import sys

import utils.subprocess_utils as sub_utils

DEFAULT_DEPLOYMENT = \
    { "jira_backup.py"           : "ippops4"
    , "confluence_backup.py"     : "ippops4"
    , "svn_backup.py"            : "ippops4"
    , "mailman_backup.py"        : "ippops3"
    , "ippops_homedir_backup.py" : "ippops3"
    , "website_backup.py"        : "ippops3"
    }

ADDITIONAL_FILES = \
    [ './utils'
    , 'backup.py'
    ]

PREFIX = "/export/"
SUFFIX = ".0/backups/"


def deploy(user=None):

    for file in DEFAULT_DEPLOYMENT:
        default_machine = DEFAULT_DEPLOYMENT[file]

        chosen_machine = input(f"Enter machine to copy {file} to [default={default_machine}; ENTER to accept]:")
        chosen_machine.strip('\n').strip(' ')
        if chosen_machine == '':
            chosen_machine = default_machine

        copy_file_and_utils(file, chosen_machine, user)


def copy_file_and_utils(file, machine, user):
    # copy all the utils across too
    user_at = '' if user is None else f'{user}@'
    target = f"{user_at}{machine}.ifa.hawaii.edu:{PREFIX}{machine}{SUFFIX}"
    args = ['rsync', '-a', '--progress', file, ] + ADDITIONAL_FILES + [target]
    print(f"Performing: {' '.join(args)}")
    sub_utils.simple_unix_wrapper(args)


def main():
    args = parse_args(sys.argv[1:])
    args_dict = vars(args)
    deploy(**args_dict)


def parse_args(args):
    parser = argparse.ArgumentParser(
        description='Performs deploys backup files to desired locations')
    parser.add_argument('--user'
        , nargs='?'
        , help='specify the user to transfer files as'
        )
    return parser.parse_args()


if __name__ == "__main__":
    main()
