#!/usr/bin/env python

import os
import sys


ENABLE_PTVSD = os.getenv("ENABLE_PTVSD", "").lower()
ENABLE_PTVSD = ENABLE_PTVSD and ENABLE_PTVSD not in ("false", "0")

ENABLE_PYDEVD_PYCHARM = os.getenv("ENABLE_PYDEVD_PYCHARM", "").lower()
ENABLE_PYDEVD_PYCHARM = ENABLE_PYDEVD_PYCHARM and ENABLE_PYDEVD_PYCHARM not in ("false", "0")
PYDEVD_PYCHARM_HOST = os.getenv("PYDEVD_PYCHARM_HOST", "localhost")
PYDEVD_PYCHARM_AGENT_PORT = int(os.getenv("PYDEVD_PYCHARM_AGENT_PORT", 5001))

# --debug-vs to use microsoft's visual studio remote debugger
if ENABLE_PTVSD or "--debug" in sys.argv:
    try:
        import ptvsd

        ptvsd.enable_attach()
        print("ptvsd is running")
        print("=== Waiting for debugger to attach ===")
        # To pause execution until the debugger is attached:
        ptvsd.wait_for_attach()
    except ImportError:
        print("ptvsd library was not found")


if ENABLE_PYDEVD_PYCHARM or "--debug-pycharm" in sys.argv:
    try:
        import pydevd_pycharm

        print(f"aca-py remote debugging to {PYDEVD_PYCHARM_HOST}:{PYDEVD_PYCHARM_AGENT_PORT}")
        pydevd_pycharm.settrace(host=PYDEVD_PYCHARM_HOST, port=PYDEVD_PYCHARM_AGENT_PORT,
                                stdoutToServer=True, stderrToServer=True, suspend=False)
    except ImportError:
        print("pydevd_pycharm library was not found")


from aries_cloudagent.commands import run_command  # noqa

if len(sys.argv) > 1 and sys.argv[1] and sys.argv[1][0] != "-":
    command = sys.argv[1]
    args = sys.argv[2:]
else:
    command = None
    args = sys.argv[1:]

run_command(command, args)
