#!/usr/bin/env python
# Programmer: Chris Bunch (chris@appscale.com)
""" A Python script that provides a command-line interface to cloud storage
offerings. """


# General-purpose Python library imports
import argparse
import os
import sys


# Magik library imports
lib = os.path.dirname(__file__) + os.sep + ".."
sys.path.append(lib)
from magik.storage_factory import StorageFactory


if __name__ == "__main__":
  parser = argparse.ArgumentParser(description='Upload or download files ' +
    'from supported cloud storage systems.')

  # Flags not specific to any particular storage service.
  parser.add_argument('directive', help='the action to take',
    choices=['upload_files', 'download_files'])
  parser.add_argument('--source', '-s')
  parser.add_argument('--destination', '-d')
  parser.add_argument('--name', '-n',
    help='the name of the storage service to interact with',
    choices=StorageFactory.SUPPORTED_STORAGE_PLATFORMS)

  # Flags enabling users to specify their S3 credentials.
  parser.add_argument('--AWS_ACCESS_KEY')
  parser.add_argument('--AWS_SECRET_KEY')

  # Flags enabling users to specify their GCS credentials.
  parser.add_argument('--GCS_ACCESS_KEY')
  parser.add_argument('--GCS_SECRET_KEY')

  # Flags enabling users to specify their Walrus credentials.
  parser.add_argument('--S3_URL', help='the location where Walrus is ' +
    'running (e.g., http://1.2.3.4:8773/services/Walrus)')

  # Flags enabling users to specify their Azure credentials.
  parser.add_argument('--AZURE_ACCOUNT_NAME')
  parser.add_argument('--AZURE_ACCOUNT_KEY')

  # Parse the arguments and invoke the right command.
  args = vars(parser.parse_args(sys.argv[1:]))
  storage = StorageFactory.get_storage(args)
  source_to_dest_list = [{
    'source' : args['source'],
    'destination' : args['destination']
  }]
  print getattr(storage, args['directive'])(source_to_dest_list)
