#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (C) 2023 Ilia Kurochkin <brothermechanic@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

__doc__ = 'Environment for use Blender with Robossembler Framework'
__version__ = '0.1'

import os
import bpy
from bpy.app.handlers import persistent
import addon_utils


@persistent
def rs_env():
    '''Scripts environment'''
    rs_blender_scripts_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
    if rs_blender_scripts_dir not in bpy.utils.script_paths_pref():
        script_directories = bpy.context.preferences.filepaths.script_directories
        new_dir = script_directories.new()
        new_dir.directory = rs_blender_scripts_dir
        new_dir.name = 'RS_BLENDER_SCRIPTS_DIR'
        bpy.ops.wm.save_userpref()
        bpy.ops.wm.quit_blender()
    else:
        if not addon_utils.check('BakeWrangler')[0]:
            addon_utils.enable('BakeWrangler', default_set=True)
            bpy.ops.wm.save_userpref()

    print('Robossembler Framework Environment activated!')

rs_env()
