# Path: mobsf/MobSF/init.py
# def first_run(secret_file, base_dir, mobsf_home):
#     # Based on https://gist.github.com/ndarville/3452907#file-secret-key-gen-py
#     if 'MOBSF_SECRET_KEY' in os.environ:
#         secret_key = os.environ['MOBSF_SECRET_KEY']
#     elif os.path.isfile(secret_file):
#         secret_key = open(secret_file).read().strip()
#     else:
#         try:
#             secret_key = get_random()
#             secret = open(secret_file, 'w')
#             secret.write(secret_key)
#             secret.close()
#         except IOError:
#             raise Exception('Secret file generation failed' % secret_file)
#         # Run Once
#         make_migrations(base_dir)
#         migrate(base_dir)
#         # Windows Setup
#         windows_config_local(mobsf_home)
#     return secret_key
# 
# def get_mobsf_home(use_home, base_dir):
#     try:
#         mobsf_home = ''
#         if use_home:
#             mobsf_home = os.path.join(os.path.expanduser('~'), '.MobSF')
#             # MobSF Home Directory
#             if not os.path.exists(mobsf_home):
#                 os.makedirs(mobsf_home)
#             create_user_conf(mobsf_home, base_dir)
#         else:
#             mobsf_home = base_dir
#         # Download Directory
#         dwd_dir = os.path.join(mobsf_home, 'downloads/')
#         if not os.path.exists(dwd_dir):
#             os.makedirs(dwd_dir)
#         # Screenshot Directory
#         screen_dir = os.path.join(dwd_dir, 'screen/')
#         if not os.path.exists(screen_dir):
#             os.makedirs(screen_dir)
#         # Upload Directory
#         upload_dir = os.path.join(mobsf_home, 'uploads/')
#         if not os.path.exists(upload_dir):
#             os.makedirs(upload_dir)
#         # Signature Directory
#         sig_dir = os.path.join(mobsf_home, 'signatures/')
#         if use_home:
#             src = os.path.join(base_dir, 'signatures/')
#             try:
#                 shutil.copytree(src, sig_dir)
#             except Exception:
#                 pass
#         elif not os.path.exists(sig_dir):
#             os.makedirs(sig_dir)
#         return mobsf_home
#     except Exception:
#         logger.exception('Creating MobSF Home Directory')
# 
# def get_mobsf_version():
#     return BANNER, VERSION, f'v{VERSION} Beta'


import imp
import logging
import os
from mobsf.MobSF.init import (
    first_run,
    get_mobsf_home,
    get_mobsf_version,
)
DWD_DIR = os.path.join(MobSF_HOME, 'downloads/')
# Screenshot Directory
SCREEN_DIR = os.path.join(MobSF_HOME, 'downloads/screen/')
# Upload Directory
UPLD_DIR = os.path.join(MobSF_HOME, 'uploads/')
# Database Directory
DB_DIR = os.path.join(MobSF_HOME, 'db.sqlite3')
# Signatures used by modules
SIGNATURE_DIR = os.path.join(MobSF_HOME, 'signatures/')
# Tools Directory
TOOLS_DIR = os.path.join(BASE_DIR, 'DynamicAnalyzer/tools/')
# Secret File
SECRET_FILE = os.path.join(MobSF_HOME, 'secret')

# ==========Load MobSF User Settings==========
try:
    if USE_HOME:
        USER_CONFIG = os.path.join(MobSF_HOME, 'config.py')
        sett = imp.load_source('user_settings', USER_CONFIG)
        locals().update(  # lgtm [py/modification-of-locals]
            {k: v for k, v in list(sett.__dict__.items())
                if not k.startswith('__')})
        CONFIG_HOME = True
    else:
        CONFIG_HOME = False
except Exception:
    logger.exception('Reading Config')
    CONFIG_HOME = False

# ===MOBSF SECRET GENERATION AND DB MIGRATION====

# Path: mobsf/MobSF/init.py
# def first_run(secret_file, base_dir, mobsf_home):
#     # Based on https://gist.github.com/ndarville/3452907#file-secret-key-gen-py
#     if 'MOBSF_SECRET_KEY' in os.environ:
#         secret_key = os.environ['MOBSF_SECRET_KEY']
#     elif os.path.isfile(secret_file):
#         secret_key = open(secret_file).read().strip()
#     else:
#         try:
#             secret_key = get_random()
#             secret = open(secret_file, 'w')
#             secret.write(secret_key)
#             secret.close()
#         except IOError:
#             raise Exception('Secret file generation failed' % secret_file)
#         # Run Once
#         make_migrations(base_dir)
#         migrate(base_dir)
#         # Windows Setup
#         windows_config_local(mobsf_home)
#     return secret_key
# 
# def get_mobsf_home(use_home, base_dir):
#     try:
#         mobsf_home = ''
#         if use_home:
#             mobsf_home = os.path.join(os.path.expanduser('~'), '.MobSF')
#             # MobSF Home Directory
#             if not os.path.exists(mobsf_home):
#                 os.makedirs(mobsf_home)
#             create_user_conf(mobsf_home, base_dir)
#         else:
#             mobsf_home = base_dir
#         # Download Directory
#         dwd_dir = os.path.join(mobsf_home, 'downloads/')
#         if not os.path.exists(dwd_dir):
#             os.makedirs(dwd_dir)
#         # Screenshot Directory
#         screen_dir = os.path.join(dwd_dir, 'screen/')
#         if not os.path.exists(screen_dir):
#             os.makedirs(screen_dir)
#         # Upload Directory
#         upload_dir = os.path.join(mobsf_home, 'uploads/')
#         if not os.path.exists(upload_dir):
#             os.makedirs(upload_dir)
#         # Signature Directory
#         sig_dir = os.path.join(mobsf_home, 'signatures/')
#         if use_home:
#             src = os.path.join(base_dir, 'signatures/')
#             try:
#                 shutil.copytree(src, sig_dir)
#             except Exception:
#                 pass
#         elif not os.path.exists(sig_dir):
#             os.makedirs(sig_dir)
#         return mobsf_home
#     except Exception:
#         logger.exception('Creating MobSF Home Directory')
# 
# def get_mobsf_version():
#     return BANNER, VERSION, f'v{VERSION} Beta'


# Path: mobsf/MobSF/settings.py
import imp
import logging
import os
from mobsf.MobSF.init import (
    first_run,
    get_mobsf_home,
    get_mobsf_version,
)

DWD_DIR = os.path.join(MobSF_HOME, 'downloads/')
# Screenshot Directory
SCREEN_DIR = os.path.join(MobSF_HOME, 'downloads/screen/')
# Upload Directory
UPLD_DIR = os.path.join(MobSF_HOME, 'uploads/')
# Database Directory
DB_DIR = os.path.join(MobSF_HOME, 'db.sqlite3')
# Signatures used by modules
SIGNATURE_DIR = os.path.join(MobSF_HOME, 'signatures/')
# Tools Directory
TOOLS_DIR = os.path.join(BASE_DIR, 'DynamicAnalyzer/tools/')
# Secret File
SECRET_FILE = os.path.join(MobSF_HOME, 'secret')

# ==========Load MobSF User Settings==========
try:
    if USE_HOME:
        USER_CONFIG = os.path.join(MobSF_HOME, 'config.py')
        sett = imp.load_source('user_settings', USER_CONFIG)
        locals().update(  # lgtm [py/modification-of-locals]
            {k: v for k, v in list(sett.__dict__.items())
                if not k.startswith('__')})
        CONFIG_HOME = True
    else:
        CONFIG_HOME = False
except Exception:
    logger.exception('Reading Config')
    CONFIG_HOME = False

# ===MOBSF SECRET GENERATION AND DB MIGRATION====

SECRET_KEY = first_run(SECRET_FILE, BASE_DIR, MobSF_HOME)