#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Useful commands that get reused during the normal course of development
import argparse
import json
import sys

from globaleaks.settings import Settings
from globaleaks.utils import templating


def generate_templates_descriptor(args):
    # Helper script that outputs JSON describing the variables each template can use
    out_dict = dict()
    for template_type, kw_class in templating.supported_template_types.items():
        postfix = ['_mail_template', '_mail_title']
        if template_type in ['null', 'user_credentials']:
            continue
        elif template_type == 'export_message':
            postfix = ['_whistleblower', '_recipient']
        elif template_type == 'export_template':
            postfix = ['']

        for p in postfix:
            out_dict[template_type + p] = kw_class.keyword_list

    print(json.dumps(out_dict, indent=2, separators=(',', ':'), sort_keys=True))


Settings.eval_paths()

parser = argparse.ArgumentParser(prog="gl-admin",
                 description="GlobaLeaks backend administator interface")

subp = parser.add_subparsers(title="commands")

kw_p = subp.add_parser("generate_templates_descriptor", help="Generate mail templates descriptors")
kw_p.set_defaults(func=generate_templates_descriptor)

if __name__ == '__main__':
    args = parser.parse_args()
    try:
        func = args.func
    except AttributeError:
        parser.print_help()
        sys.exit(1)
    func(args)
