#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Generate valid and signed descriptors for mocked Tor relays or bridges.

.. note:: This application uses the CFFI_ version of PyNaCl_ to emulate the
    following curvecp_ commands (the ``curvecp*`` commands require
    libchloride_, which is therefore also required):

        $ curvecpmakekey ntor-key
        $ curvecpprintkey ntor-key > ntor-key.hex
        $ python -c 'import binascii, sys; \
            key_hex=open('./ntor-key.hex','rb').read();\
            key_b64=binascii.b2a_base64(binascii.unhexlify(key_hex));\
            sys.stdout.write(key_b64);'

    .. _CFFI: https://cffi.readthedocs.org
    .. _PyNaCl: https://github.com/pyca/pynacl
    .. _curvecp: http://curvecp.org/
    .. _libchloride: https://github.com/jedisct1/libchloride

.. authors:: Isis Lovecruft <isis@torproject.org> 0xA3ADB67A2CDB8B35
             Matthew Finkel <sysrqb@torproject.org>
.. licence:: see LICENSE file for licensing details
.. copyright:: (c) 2013-2015 The Tor Project, Inc.
               (c) 2013-2015 Isis Lovecruft
               (c) 2013-2015 Matthew Finkel
"""

from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

import logging
import sys

from leekspin import generator
from leekspin import util


if __name__ == "__main__":
    try:
        parser = util.getArgParser()
        options = parser.parse_args()

        if options.verbose:
            logging.getLogger().addHandler(logging.StreamHandler())
            logging.getLogger().setLevel(10)
        else:
            print = lambda x: True
            logging.disable(100)

        if options.version:
            print("%s" % parser.version)
            sys.exit(0)

        descType = 'bridge'
        if options.relay:
            descType = 'relay'
        elif options.hidden_service:
            descType = 'hidden_service'

        if options.descriptors and (options.descriptors > 0):
            generator.create(options.descriptors, descriptorType=descType,
                             withoutTAP=options.without_tap,
                             withoutNTOR=options.without_ntor)
        else:
            raise SystemExit(parser.format_help())

    except Exception as error:
        raise SystemExit(error)
