#!/usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author:  Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
#          https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.

# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.md

"""See the check's README for more details.
"""

import argparse  # pylint: disable=C0413
import sys  # pylint: disable=C0413

import lib.base  # pylint: disable=C0413
import lib.rocket  # pylint: disable=C0413
import lib.version  # pylint: disable=C0413
from lib.globals import (STATE_UNKNOWN) # pylint: disable=C0413

__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2025031001'

DESCRIPTION = 'Tracks if Rocket.Chat is EOL.'

DEFAULT_CHECK_MAJOR = False
DEFAULT_CHECK_MINOR = False
DEFAULT_CHECK_PATCH = False
DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_OFFSET_EOL = -30 # days
DEFAULT_TIMEOUT = 8
DEFAULT_URL = 'http://localhost:3000/api/v1'
DEFAULT_USERNAME = 'rocket-stats'


def parse_args():
    """Parse command line arguments using argparse.
    """
    parser = argparse.ArgumentParser(description=DESCRIPTION)

    parser.add_argument(
        '-V', '--version',
        action='version',
        version=f'%(prog)s: v{__version__} by {__author__}'
    )

    parser.add_argument(
        '--always-ok',
        help='Always returns OK.',
        dest='ALWAYS_OK',
        action='store_true',
        default=False,
    )

    parser.add_argument(
        # deprecated
        '--cache-expire',
        help=argparse.SUPPRESS,
        dest='CACHE_EXPIRE',
        type=int,
    )

    parser.add_argument(
        '--check-major',
        help='Alert me when there is a new major release available, '
             'even if the current version of my product is not EOL. '
             'Example: Notify when I run v26 (not yet EOL) and v27 is available. '
             'Default: %(default)s',
        dest='CHECK_MAJOR',
        action='store_true',
        default=DEFAULT_CHECK_MAJOR,
    )

    parser.add_argument(
        '--check-minor',
        help='Alert me when there is a new major.minor release available, '
             'even if the current version of my product is not EOL. '
             'Example: Notify when I run v26.2 (not yet EOL) and v26.3 is available. '
             'Default: %(default)s',
        dest='CHECK_MINOR',
        action='store_true',
        default=DEFAULT_CHECK_MINOR,
    )

    parser.add_argument(
        '--check-patch',
        help='Alert me when there is a new major.minor.patch release available, '
             'even if the current version of my product is not EOL. '
             'Example: Notify when I run v26.2.7 (not yet EOL) and v26.2.8 is available. '
             'Default: %(default)s',
        dest='CHECK_PATCH',
        action='store_true',
        default=DEFAULT_CHECK_PATCH,
    )

    parser.add_argument(
        '--insecure',
        help='This option explicitly allows to perform "insecure" SSL connections. '
             'Default: %(default)s',
        dest='INSECURE',
        action='store_true',
        default=DEFAULT_INSECURE,
    )

    parser.add_argument(
        '--no-proxy',
        help='Do not use a proxy. '
             'Default: %(default)s',
        dest='NO_PROXY',
        action='store_true',
        default=DEFAULT_NO_PROXY,
    )

    parser.add_argument(
        '--offset-eol',
        help='Alert me n days before ("-30") or after an EOL date ("30" or "+30"). '
             'Default: %(default)s days',
        dest='OFFSET_EOL',
        type=int,
        default=DEFAULT_OFFSET_EOL,
    )

    parser.add_argument(
        '-p', '--password',
        help='Rocket.Chat API password.',
        dest='PASSWORD',
        required=True,
        )

    parser.add_argument(
        '--timeout',
        help='Network timeout in seconds. '
             'Default: %(default)s (seconds)',
        dest='TIMEOUT',
        type=int,
        default=DEFAULT_TIMEOUT,
    )

    parser.add_argument(
        '--url',
        help='Rocket.Chat API URL. '
             'Default: %(default)s',
        dest='URL',
        default=DEFAULT_URL,
    )

    parser.add_argument(
        '--username',
        help='Rocket.Chat API username. '
             'Default: %(default)s',
        dest='USERNAME',
        default=DEFAULT_USERNAME,
        required=True,
    )

    return parser.parse_args()


def get_installed_version(args):
    credentials = lib.base.coe(lib.rocket.get_token(
        args.URL,
        args.USERNAME,
        args.PASSWORD,
        insecure=args.INSECURE,
        no_proxy=args.NO_PROXY,
        timeout=args.TIMEOUT,
    ))
    authToken, userId = credentials.split(':')
    result = lib.base.coe(lib.rocket.get_stats(
        args.URL,
        authToken,
        userId,
        insecure=args.INSECURE,
        no_proxy=args.NO_PROXY,
        timeout=args.TIMEOUT,
    ))

    if result.get('statistics', None):
        # version < 3.0.0
        return result['statistics']['version']
    # version >= 3.0.0
    return result['version']


def main():
    """The main function. Hier spielt die Musik.
    """

    # parse the command line, exit with UNKNOWN if it fails
    try:
        args = parse_args()
    except SystemExit:
        sys.exit(STATE_UNKNOWN)

    # fetch data
    installed_version = get_installed_version(args)
    if not installed_version:
        lib.base.cu(f'Rocket.Chat at `{args.URL}` not found.')
    state, msg = lib.version.check_eol(
        'https://endoflife.date/api/rocket-chat.json',
        installed_version,
        offset_eol=args.OFFSET_EOL,
        check_major=args.CHECK_MAJOR,
        check_minor=args.CHECK_MINOR,
        check_patch=args.CHECK_PATCH,
        insecure=args.INSECURE,
        no_proxy=args.NO_PROXY,
        timeout=args.TIMEOUT,
    )

    # over and out
    lib.base.oao(
        f'Rocket.Chat v{installed_version} ({msg})',
        state,
        lib.base.get_perfdata(
            'rocketchat-version',
            lib.version.version2float(installed_version),
            None,
            None,
            None,
            0,
            None,
        ),
        always_ok=args.ALWAYS_OK,
    )


if __name__ == '__main__':
    try:
        main()
    except Exception:   # pylint: disable=W0703
        lib.base.cu()
