#!/usr/bin/python

import argparse
import json

def main(args):
    jsonfile = '/srv/portal/js/apps.json'

    data = {}
    with open(jsonfile, 'r') as f:
        data = json.load(f)
    if not args.app in data:
        data[args.app] = {}
    if args.url:
        data[args.app]['url'] = args.url
    if args.login:
        data[args.app]['login'] = args.login
    if args.password:
        data[args.app]['password'] = args.password
    if args.property:
        for key, value in args.property:
            data[args.app][key] = value
    with open(jsonfile, 'w') as f:
        json.dump(data, f)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Spotter Cluster portal application manager')
    parser.add_argument('app', help='Application ID.')
    parser.add_argument('url', nargs='?', help='URL to the application. Use "{host}" as a host placeholder.')
    parser.add_argument('login', nargs='?', help='Administrative login.')
    parser.add_argument('password', nargs='?', help='Administrative password.')
    parser.add_argument('-p', '--property', nargs=2, action='append', help='Add arbitrary key-value to the application properties')
    main(parser.parse_args())
