#!/usr/bin/env python

import sys
from optparse import OptionParser

if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option('-b', '--bucket', dest='bucket',
                      help='the s3 bucket to upload to')
    parser.add_option('-k', '--key', dest='key',
                      help='the name of the key to create in the bucket')
    parser.add_option('-K', '--aws_key', dest='aws_key',
                      help='aws access key')
    parser.add_option('-s', '--aws_secret', dest='aws_secret',
                      help='aws secret key')
    parser.add_option('-d', '--data', dest='data',
                      help='the data to upload to s3 -- if left blank will be read from STDIN')
    (options, args) = parser.parse_args()

    if len(sys.argv) < 2:
        parser.print_help()
        sys.exit(0)

    if not options.bucket:
        parser.error('bucket not provided')
    if not options.key:
        parser.error('key not provided')
    if not options.aws_key:
        parser.error('aws access key not provided')
    if not options.aws_secret:
        parser.error('aws secret key not provided')

    data = sys.stdin if not options.data else [options.data]

    def cb(part_no, uploaded, total):
        print part_no, uploaded, total

    upload(options.bucket, options.aws_key, options.aws_secret, data_collector(data), options.key,
           progress_cb=cb, replace=True)
