#! /usr/bin/env python
"""
move.

Move a file between buckets.
"""

if __name__ == '__main__':
    import sys
    import argparse as ap
    from boto.s3.connection import S3Connection

    from aws_keys import AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY

    parser = ap.ArgumentParser(
        prog='move',
        conflict_handler='resolve',
        description='Move a file between buckets.')
    group1 = parser.add_argument_group('Options', '')
    group1.add_argument('--from_file', metavar="STR", type=str, required=True,
                        help='File (and complete path) to move from.')
    group1.add_argument('--to_file', metavar="STR", type=str, required=True,
                        help='File (and complete path) to move to.')
    group1.add_argument('--from_bucket', metavar="STR", type=str,
                        required=True, help='Bucket to move from.')
    group1.add_argument('--to_bucket', metavar="STR", type=str, required=True,
                        help='Bucket to move to.')
    group1.add_argument('-h', '--help', action='help',
                        help='Show this help message and exit')

    if len(sys.argv) == 1:
        parser.print_usage()
        sys.exit(1)

    args = parser.parse_args()

    conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    bucket = conn.get_bucket(args.from_bucket)

    # Get key file
    key = bucket.get_key(args.from_file)
    if key:
        print "Moving {0} to bucket {1}".format(
            args.from_file,
            args.to_bucket
        )
        new_key = key.copy(args.to_bucket, args.to_file, preserve_acl=True)
        if key.etag[1:-1] == new_key.etag[1:-1]:
            print "Deleting {0} from bucket {1}".format(
                args.from_file,
                args.from_bucket
            )
            key.delete()
    else:
        print "{0} not found in bucket {1}".format(
            args.from_file,
            args.from_bucket
        )
