#!/opt/cloudlinux/venv/bin/python3 -bb
# coding=utf-8
#
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2020 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENCE.TXT
#

# Cloudlinux + sender daemon control utility

import sys
import syslog
from clcommon.lib.jwt_token import jwt_token_check
from cl_plus.daemon.daemon_control import install_daemons, remove_daemons
from cl_plus.consts import PKG_VERSION_TINY, SENTRY_DSN
from clsentry import init_sentry_client


def main():
    try:
        init_sentry_client('setup_clplus_tool', PKG_VERSION_TINY, SENTRY_DSN)
    except Exception as e:
        syslog.syslog(syslog.LOG_ERR, f'Failed to init sentry client: {e}')
    is_valid, _, _ = jwt_token_check()
    if 'remove' in sys.argv or not is_valid:
        # JWT token absent or invalid - remove daemons
        remove_daemons()
        sys.exit(0)
    # JWT token check OK - install daemons
    install_daemons()


if __name__ == "__main__":
    main()
