#!/usr/bin/env python
# -*- coding: utf-8; fill-column: 100 -*-
#
# Copyright (C) 2016 Alex Franke

__doc__ = """Creates a new space according to the CFEL-CMI template.

The CMI template includes a custom homepage, the space categories **cfel-cmi** and
**cmi-elog-calendar**.

You have to provide a servername and a user with rights to create a new space. Additional settings
like the spacekey and name of the new space are required.
"""

import argparse

from confluence import spaces


def main():
    """Command-line user interface for creating a new space according to CMI template."""
    # define command options
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--server', dest='server', default="https://confluence.desy.de/",
                        help='Server address [default: https://confluence.desy.de/]')
    parser.add_argument('--user', dest='user', default="jkuepper",
                        help='Please enter your Username. [default: jkuepper]')
    parser.add_argument('--key', dest='spacekey', required=True,
                        help='Please enter the spaceKey of the space you want to create.')
    parser.add_argument('--name', dest='spacename', required=True,
                        help='Please enter the spaceName of the space you want to create.')
    parser.add_argument('--read', dest='read', default='cmi-users',
                        help='enter a list of user and/or groups with READ access, seperated by a comma. [default:cmi-users]')
    parser.add_argument('--write', dest='write', default='',
                        help='enter a list of users and/or groups with WRITE access, seperated by a comma [default: none]')
    parser.add_argument('--admin', dest='admin', default='jkuepper',
                        help='enter a list of users and/or groups with ADMIN access, seperated by a comma. [default: jkuepper]')
    # parse arguments
    args = parser.parse_args()
    if args.server[-1:] != "/":
        args.server += "/"

    # create lists of readers, writers, and admins (users and groups)
    reads = list()
    writes = list()
    admins = list()
    for entity in args.read.split(','):
        if entity != '':
            reads.append(entity)
    for entity in args.write.split(','):
        if entity != '':
            writes.append(entity)
    for entity in args.admin.split(','):
        if entity != '':
            admins.append(entity)

    # always, unconditionally, add jkuepper (Jochen Küpper) to readers and as admin
    if 'jkuepper' not in reads:
        reads.append('jkuepper')
    if 'jkuepper' not in admins:
        admins.append('jkuepper')

    # inform user about Space setup
    print('----------------------------------------------------------------------')
    print('IMPORTANT: Please make sure you have the permission to create a space!')
    print("Please confirm the following settings:")
    print('----------------------------------------------------------------------')
    print("   Server address                 :  " + args.server)
    print("   Username                       :  " + args.user)
    print("   new Spacekey                   :  " + args.spacekey)
    print("   new Spacename                  :  " + args.spacename)
    print("   Users/Groups READ  permission  :  " + str(reads))
    print("   Users/Groups WRITE permission  :  " + str(writes))
    print("   Users/Groups ADMIN permission  :  " + str(admins))
    print('----------------------------------------------------------------------')
    # create Space
    spaces.createCMISpace(args.server, args.user, args.spacekey, args.spacename, reads, writes, admins)


# let us run as top-level script -- call main()
if __name__ == "__main__":
    main()
