# Path: source/libs/include.py
# def command(provided_command, value=False, stdout=True):
#     """ Run a given command """
#     sp = subprocess.Popen(provided_command, shell=True, env={'PATH': os.environ['PATH']}, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#     if stdout:
#         return sp.stdout.read()
#     if value:
#         return sp.returncode


from source.libs.include import *
from source.libs.include import command as cmd
from source.libs.define import *
from source.libs.search import *
from source.libs.db import *
                            import termios
                            import msvcrt

def execute_command(command):
    command = command.strip()
    lib.command_history.append(command)

    # any $variables present? replace it with tb (or global parameter) value
    newcommand = []
    parts = command.split(' ')
    for part in parts:
        if part.startswith('$'):
            # key present?
            if part[1:] in tb.keys():
                newcommand.append(tb[part[1:]])
            elif part[1:] in global_parameters.keys():
                newcommand.append(global_parameters[part[1:]])
            else:
                log.err('Key \'%s\' is not present in Temporary Base or Global Parameters.' % (part[1:]))
        else:
            # escaped $?
            if part.startswith(r'\$'):
                part = part[1:]
            newcommand.append(part)
    command = ' '.join(newcommand)

    # # # # # # # # # 
    # OK, what to do?
    # # # # # # # # #
    
    # system command
    if command.startswith('!'):

# Path: source/libs/include.py
# def command(provided_command, value=False, stdout=True):
#     """ Run a given command """
#     sp = subprocess.Popen(provided_command, shell=True, env={'PATH': os.environ['PATH']}, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#     if stdout:
#         return sp.stdout.read()
#     if value:
#         return sp.returncode


# Path: source/libs/commands.py
from source.libs.include import *
from source.libs.include import command as cmd
from source.libs.define import *
from source.libs.search import *
from source.libs.db import *
                            import termios
                            import msvcrt


def execute_command(command):
    command = command.strip()
    lib.command_history.append(command)

    # any $variables present? replace it with tb (or global parameter) value
    newcommand = []
    parts = command.split(' ')
    for part in parts:
        if part.startswith('$'):
            # key present?
            if part[1:] in tb.keys():
                newcommand.append(tb[part[1:]])
            elif part[1:] in global_parameters.keys():
                newcommand.append(global_parameters[part[1:]])
            else:
                log.err('Key \'%s\' is not present in Temporary Base or Global Parameters.' % (part[1:]))
        else:
            # escaped $?
            if part.startswith(r'\$'):
                part = part[1:]
            newcommand.append(part)
    command = ' '.join(newcommand)

    # # # # # # # # # 
    # OK, what to do?
    # # # # # # # # #
    
    # system command
    if command.startswith('!'):

        result = cmd(command[1:])