#
# OMNIVORE CONFIDENTIAL
# __________________
#
#  [2013] - [2020] Omnivore Technologies
#  All Rights Reserved.
#
# NOTICE:  All information contained herein is, and remains
# the property of Omnivore Technologies and its suppliers,
# if any.  The intellectual and technical concepts contained
# herein are proprietary to Omnivore Technologies
# and its suppliers and may be covered by U.S. and Foreign Patents,
# patents in process, and are protected by trade secret or copyright law.
# Dissemination of this information or reproduction of this material
# is strictly forbidden unless prior written permission is obtained
# from Omnivore Technologies.
#
import logging

from agent_updater.config_migration.config_migration import (
    ConfigMigration,
)

logger = logging.getLogger(__name__)

MIGRATION_VERSION = '18.10.17'
CLASS_NAME = 'ConfigMigration181017'


class ConfigMigration181017(ConfigMigration):

    def up(self):
        """Ensure ReadOnly and DisableProcessCheck directives are explicitly set."""
        # These "gets" fall back to default values if not explicitly defined
        readonly_value = self._config.get_undefaulted_raw_value(self._config.AGENT_SECTION, self._config.READ_ONLY)
        disableprocesscheck_value = self._config.get_undefaulted_raw_value(
            self._config.AGENT_SECTION,
            self._config.DISABLE_PROCESS_CHECK,
        )
        # These "sets" ignore defaults, ensuring the value appears in the INI file
        if readonly_value is None:
            logger.info("{directive} is unset; setting '{directive} = False'".format(directive=self._config.READ_ONLY))
            self._config.set_value(
                self._config.FILE_GENERAL,
                self._config.AGENT_SECTION,
                self._config.READ_ONLY,
                'False',
            )
        else:
            logger.info("{} is set to '{}'; skipping self._config migration".format(self._config.READ_ONLY, readonly_value))

        if disableprocesscheck_value is None:
            logger.info("{directive} is unset; setting '{directive} = False'".format(
                directive=self._config.DISABLE_PROCESS_CHECK,
            ))
            self._config.set_value(
                self._config.FILE_GENERAL,
                self._config.AGENT_SECTION,
                self._config.DISABLE_PROCESS_CHECK,
                'False',
            )
        else:
            logger.info("{} is set to '{}'; skipping self._config migration".format(
                self._config.DISABLE_PROCESS_CHECK,
                readonly_value
            ))
