# Path: source/libs/io.py
# def get_system_type_from_active_root(activeroot, verbose=False, dontprint=''):
#     if activeroot == '/':
#         return sys.platform
#     #
#     # check db if the system is known
#     #
# 
#     # TODO 
# 
#     #
#     # new system - detect it and write into db
#     #
#     
#     # chroot or similar?
#     if activeroot.startswith(('/', 'ssh://')): # sub or ssh
#         # linux should have some folders in / ...
#         success = 0
#         linux_folders = ['/bin', '/boot', '/dev', '/etc', '/home', '/lib', '/media', '/opt', '/proc', '/root', '/sbin', '/srv', '/sys', '/tmp', '/usr']
#         for folder in linux_folders:
#             if can_read(activeroot, folder):
#                 success += 1
#         linux_score = success/len(linux_folders)
#         
#         if verbose:
#             if type(dontprint) != str or dontprint=='':
#                 log.info('Linux score for \'%s\': %f' % (activeroot, linux_score))
#             else:
#                 log.info('Linux score for \'%s\': %f' % (activeroot.partition(dontprint)[2], linux_score))
#         if linux_score > 0.3: # this should be linux
#             #TODO write into DB
#             return 'linux'
#         
#     #TODO NOT IMPLEMENTED
#     return 'unknown'


import os, sys, re, time, importlib, imp, subprocess,  signal, threading
import source.libs.define as lib
import source.libs.scheduler
import source.libs.connection
import source.libs.log as log
import faulthandler
from source.libs.db import *
from source.libs.io import get_system_type_from_active_root
            from pwd import getpwnam
            from win32net import NetUserGetLocalGroups
    for d in dicts:
        lines = []
        path = os.path.join('dictionaries', d)
        if os.access(path, os.R_OK):
            with open(path, 'r') as f:
                #print(path)
                try:
                    lines = [x.strip() for x in f.readlines()]
                except:
                    pass
                lib.dicts[d[:-4].upper()] = lines
    log.info('%d dictionaries loaded.' % (len(lib.dicts)))
"""

def natural_sort(data):
    return sorted(data, key=lambda x: [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', x)])

#TODO make it working with ACTIVEROOT
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



def command_exists(activeroot, c):
    """ Check if a given command exists """

# Path: source/libs/io.py
# def get_system_type_from_active_root(activeroot, verbose=False, dontprint=''):
#     if activeroot == '/':
#         return sys.platform
#     #
#     # check db if the system is known
#     #
# 
#     # TODO 
# 
#     #
#     # new system - detect it and write into db
#     #
#     
#     # chroot or similar?
#     if activeroot.startswith(('/', 'ssh://')): # sub or ssh
#         # linux should have some folders in / ...
#         success = 0
#         linux_folders = ['/bin', '/boot', '/dev', '/etc', '/home', '/lib', '/media', '/opt', '/proc', '/root', '/sbin', '/srv', '/sys', '/tmp', '/usr']
#         for folder in linux_folders:
#             if can_read(activeroot, folder):
#                 success += 1
#         linux_score = success/len(linux_folders)
#         
#         if verbose:
#             if type(dontprint) != str or dontprint=='':
#                 log.info('Linux score for \'%s\': %f' % (activeroot, linux_score))
#             else:
#                 log.info('Linux score for \'%s\': %f' % (activeroot.partition(dontprint)[2], linux_score))
#         if linux_score > 0.3: # this should be linux
#             #TODO write into DB
#             return 'linux'
#         
#     #TODO NOT IMPLEMENTED
#     return 'unknown'


# Path: source/libs/include.py
import os, sys, re, time, importlib, imp, subprocess,  signal, threading
import source.libs.define as lib
import source.libs.scheduler
import source.libs.connection
import source.libs.log as log
import faulthandler
from source.libs.db import *
from source.libs.io import get_system_type_from_active_root
            from pwd import getpwnam
            from win32net import NetUserGetLocalGroups

    for d in dicts:
        lines = []
        path = os.path.join('dictionaries', d)
        if os.access(path, os.R_OK):
            with open(path, 'r') as f:
                #print(path)
                try:
                    lines = [x.strip() for x in f.readlines()]
                except:
                    pass
                lib.dicts[d[:-4].upper()] = lines
    log.info('%d dictionaries loaded.' % (len(lib.dicts)))
"""

def natural_sort(data):
    return sorted(data, key=lambda x: [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', x)])

#TODO make it working with ACTIVEROOT
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



def command_exists(activeroot, c):
    """ Check if a given command exists """

    if 'linux' in get_system_type_from_active_root(activeroot):