#!/usr/bin/env python3
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

import sys
from typing import Final

import cmk.utils.paths
import cmk.utils.store
from cmk.utils.setup_search_index import request_index_rebuild
from cmk.utils.version import __version__, parse_check_mk_version
from cmk.utils.visuals import invalidate_visuals_cache

import cmk.ec.export as ec  # pylint: disable=cmk-module-layer-violation

from cmk.discover_plugins import addons_plugins_local_path, plugins_local_path
from cmk.mkp_tool import (
    cli,
    make_post_package_change_actions,
    PackageOperationCallbacks,
    PackagePart,
    PackageStore,
    PathConfig,
    reload_apache,
)

local_path = plugins_local_path()
addons_path = addons_plugins_local_path()
_PATH_CONFIG: PathConfig | None = None
if local_path is not None and addons_path is not None:
    _PATH_CONFIG = PathConfig(
        cmk_plugins_dir=local_path,
        cmk_addons_plugins_dir=addons_path,
        agent_based_plugins_dir=cmk.utils.paths.local_agent_based_plugins_dir,
        agents_dir=cmk.utils.paths.local_agents_dir,
        alert_handlers_dir=cmk.utils.paths.local_alert_handlers_dir,
        bin_dir=cmk.utils.paths.local_bin_dir,
        check_manpages_dir=cmk.utils.paths.local_legacy_check_manpages_dir,
        checks_dir=cmk.utils.paths.local_checks_dir,
        doc_dir=cmk.utils.paths.local_doc_dir,
        gui_plugins_dir=cmk.utils.paths.local_gui_plugins_dir,
        installed_packages_dir=cmk.utils.paths.installed_packages_dir,
        inventory_dir=cmk.utils.paths.local_inventory_dir,
        lib_dir=cmk.utils.paths.local_lib_dir,
        locale_dir=cmk.utils.paths.local_locale_dir,
        local_root=cmk.utils.paths.local_root,
        mib_dir=cmk.utils.paths.local_mib_dir,
        mkp_rule_pack_dir=ec.mkp_rule_pack_dir(),
        notifications_dir=cmk.utils.paths.local_notifications_dir,
        pnp_templates_dir=cmk.utils.paths.local_pnp_templates_dir,
        manifests_dir=cmk.utils.paths.tmp_dir,
        web_dir=cmk.utils.paths.local_web_dir,
    )


_SITE_CONTEXT: Final = cli.SiteContext(
    PackageStore(
        enabled_dir=cmk.utils.paths.local_enabled_packages_dir,
        local_dir=cmk.utils.paths.local_optional_packages_dir,
        shipped_dir=cmk.utils.paths.optional_packages_dir,
    ),
    callbacks={
        PackagePart.EC_RULE_PACKS: PackageOperationCallbacks(
            install=ec.install_packaged_rule_packs,
            uninstall=ec.uninstall_packaged_rule_packs,
            release=ec.release_packaged_rule_packs,
        ),
    },
    post_package_change_actions=make_post_package_change_actions(
        on_any_change=(reload_apache, invalidate_visuals_cache, request_index_rebuild)
    ),
    version=__version__,
    parse_version=parse_check_mk_version,
)


if __name__ == "__main__":
    if _PATH_CONFIG is None:
        sys.stderr.write("MKPs are not supported on this machine or checkmk version")
        sys.exit(1)
    sys.exit(
        cli.main(
            sys.argv[1:] or ["--help"],
            _PATH_CONFIG,
            _SITE_CONTEXT,
            cmk.utils.store.save_bytes_to_file,
        )
    )
