#!/usr/bin/env python
import argparse
from dynomite import *
from dynomite.s3 import *


def get_args():
    parser = argparse.ArgumentParser()

    parser.add_argument('--source_access_key',
                        help='Access key for source account')
    parser.add_argument('--source_secret_key',
                        help='Secret key for source account')
    parser.add_argument('--destination_access_key',
                        help='Access key for destination account')
    parser.add_argument('--destination_secret_key',
                        help='Secret key for destination account')
    parser.add_argument('--source_bucket',
                        help='Name of the bucket to be backed up')
    parser.add_argument('--destination_bucket',
                        help='Name of the bucket where source bucket '
                             'will be backed up')
    parser.add_argument('--source_path',
                        default=None,
                        help='In case of restore, the path where the  '
                             'backup resides')
    parser.add_argument('--destination_path',
                        default=None,
                        help='Name of the bucket where source bucket '
                             'will be backed up')
    parser.add_argument('--date',
                        help='The date')
    parser.add_argument('--region',
                        help='The AWS region')
    parser.add_argument('--debug',
                        dest='debug',
                        action='store_const',
                        const='debug',
                        help='Runs in debug mode')

    return parser.parse_args()


def main(args):
    s3_conn_dest = s3_connect_to_region(
        args.region,
        args.destination_access_key,
        args.destination_secret_key
    )
    s3_conn_src = s3_connect_to_region(
        args.region,
        args.source_access_key,
        args.source_secret_key
    )
    iam_conn = boto.iam.connection.IAMConnection(
        aws_access_key_id=args.destination_access_key,
        aws_secret_access_key=args.destination_secret_key
    )

    set_permissions_for_user(args.source_bucket, iam_conn,
                             s3_conn_src, args.debug)
    cleanup_destination(s3_conn_dest, args.destination_bucket,
                        args.destination_path)
    copy_backup_from_source(s3_conn_dest, args.source_bucket,
                            args.destination_bucket, args.debug,
                            source_path=args.source_path,
                            destination_path=args.destination_path)


if __name__ == '__main__':
    args = get_args()

    main(args)
