#! /usr/bin/env python3
"""
Convenience script for calling the indy command line interface (CLI). For now,
the CLI is designed for experimenting with the Indy Identity platform, and not
for creating a live consensus pool. For that, it's as simple as defining a node
registry, creating a looper, creating a node, and running it.

$ indy

or supply a command to be executed first

$ indy "new nodes all"

"""

import logging
import os
import sys

# NOTE: Loading of plugin should happen as early as possible
# So put all other required imports after loadPlugins function call below
from plenum.common.plugin_helper import loadPlugins
from indy_common.config_util import getConfig

logging.root.handlers = []
logger = logging.getLogger()
logger.propagate = False
logger.disabled = True

config = getConfig()
baseDir = os.path.expanduser(config.CLI_BASE_DIR)
network_dir = os.path.expanduser(config.CLI_NETWORK_DIR)
if not os.path.exists(baseDir):
    os.makedirs(baseDir)
if not os.path.exists(network_dir):
    os.makedirs(network_dir)
loadPlugins(baseDir)

# NOTE: Put all regular imports below (not related to loadplugin)
from indy_client.cli.cli import IndyCli
from stp_core.loop.looper import Looper


def run_cli():
    print("This client is deprecated! "
          "Please, use the new libindy-based CLI: "
          "https://github.com/hyperledger/indy-sdk/tree/master/cli")

    commands = sys.argv[1:]

    withNode = True if '--with-node' in commands else False

    with Looper(debug=config.LOOPER_DEBUG) as looper:
        logFilePath = os.path.expanduser(os.path.join(config.CLI_BASE_DIR, config.logFilePath))
        cli = IndyCli(looper=looper,
                      basedirpath=baseDir,
                      ledger_base_dir=network_dir,
                      logFileName=logFilePath,
                      withNode=withNode
                      )

        looper.run(cli.shell(*commands))


default_config = """
[node_reg]
Alpha = 127.0.0.1 8001
Beta = 127.0.0.1 8003
Gamma = 127.0.0.1 8005
Delta = 127.0.0.1 8007

[client_node_reg]
AlphaC = 127.0.0.1 8002
BetaC = 127.0.0.1 8004
GammaC = 127.0.0.1 8006
DeltaC = 127.0.0.1 8008

[storage_locations]
basePath = ~
"""


if __name__ == '__main__':
    run_cli()
