#!/usr/bin/env python

import argparse
import smarterling
import sys


def main():
    """ The entry point of the app.
    """

    parser = argparse.ArgumentParser(description='Smarterling', usage='%(prog)s [options]')
    parser.add_argument('-c', '--config-file', help='Configuration file', default='smarterling.config')
    parser.add_argument('-u', '--upload', help='Upload files', default=False, action='store_true')
    parser.add_argument('-d', '--download', help='Download files', default=False, action='store_true')
    args = parser.parse_args(sys.argv[1:])

    config = smarterling.parse_config(args.config_file)

    fapi = smarterling.create_file_api(config)

    if args.upload or (not args.upload and not args.download):
        for f, fconf in config.files.items():
            smarterling.upload_file(fapi, f, fconf)

    if args.download or (not args.upload and not args.download):
        for f, fconf in config.files.items():
            smarterling.download_files(fapi, f, fconf)

if __name__ == "__main__":
    try:
        main()
    except smarterling.SmarterlingError as e:
        print("\n\nFATAL ERROR: %s" % e.message)
