#!/usr/bin/env python3
# Copyright (C) 2022 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 cmk.utils.licensing.helper import (
    get_instance_id_file_path,
    hash_site_id,
    init_logging,
    load_instance_id,
)
from cmk.utils.licensing.usage import create_sample, Now, try_update_license_usage
from cmk.utils.paths import omd_root
from cmk.utils.site import omd_site


def main() -> int:
    logger = init_logging()
    try:
        try_update_license_usage(
            Now.make(),
            load_instance_id(get_instance_id_file_path(omd_root)),
            hash_site_id(omd_site()),
            create_sample,
        )
        logger.info("Successfully updated the license usage")
        return 0
    except Exception as e:
        logger.error("Error during license usage update: %s", e)
        return 1


if __name__ == "__main__":
    sys.exit(main())
