#!/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
import json
import sys

import lib.args
import lib.base
import lib.human
import lib.lftest
import lib.shell
from lib.globals import STATE_OK, STATE_UNKNOWN

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

DESCRIPTION = """Collects statistics across multiple snapshots in a restic repository, including the
number of unique files and their total size. Supports different counting modes
(restore-size, files-by-contents, raw-data).
Requires root or sudo."""

DEFAULT_MODE = 'restore-size'


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(
        '--host',
        help='Only consider snapshots for this host. Can be specified multiple times.',
        dest='HOST',
        action='append',
        default=None,
    )

    parser.add_argument(
        '--mode',
        help='Counting mode for the statistics calculation. Default: %(default)s',
        dest='MODE',
        choices=['restore-size', 'files-by-contents', 'blobs-per-file', 'raw-data'],
        default=DEFAULT_MODE,
    )

    parser.add_argument(
        '--password-file',
        help='Path to the file containing the repository password.',
        dest='PASSWORD_FILE',
    )

    parser.add_argument(
        '--path',
        help='Only consider snapshots for this path. Can be specified multiple times.',
        dest='PATH',
        action='append',
        default=None,
    )

    parser.add_argument(
        '--repo',
        help='Restic repository location.',
        dest='REPO',
        required=True,
    )

    parser.add_argument(
        '--tag',
        help='Only consider snapshots matching this taglist in the format `tag[,tag,...]`. '
        'Can be specified multiple times.',
        dest='TAG',
        action='append',
        default=None,
    )

    parser.add_argument(
        '--test',
        help=lib.args.help('--test'),
        dest='TEST',
        type=lib.args.csv,
    )

    args, _ = parser.parse_known_args()
    return args


def main():
    """The main function. This is where the magic happens."""

    # parse the command line
    try:
        args = parse_args()
    except SystemExit:
        sys.exit(STATE_UNKNOWN)

    # fetch data
    if args.TEST is None:
        cmd = (
            f'restic --json --repo={args.REPO}'
            f' --password-file={args.PASSWORD_FILE}'
            f' --mode={args.MODE}'
        )
        if args.HOST:
            cmd += ' --host=' + ' --host='.join(args.HOST)
        if args.PATH:
            cmd += ' --path=' + ' --path='.join(args.PATH)
        if args.TAG:
            cmd += ' --tag=' + ' --tag='.join(args.TAG)
        cmd += ' stats'
        stdout, stderr, _retc = lib.base.coe(lib.shell.shell_exec(cmd))
        if stderr:
            lib.base.cu(stderr)
    else:
        # do not call the command, put in test data
        stdout, stderr, _retc = lib.lftest.test(args.TEST)

    try:
        stats = json.loads(stdout)
    except ValueError:
        lib.base.cu('No JSON object could be decoded.')

    # init some vars
    state = STATE_OK
    perfdata = ''

    perfdata += lib.base.get_perfdata(
        'total_file_count',
        stats['total_file_count'],
        uom='',
        _min=0,
    )
    perfdata += lib.base.get_perfdata(
        'total_size',
        stats['total_size'],
        uom='',
        _min=0,
    )

    # analyze data and build the message
    msg = (
        f'{lib.human.number2human(stats["total_file_count"])}'
        f' files,'
        f' {lib.human.bytes2human(stats["total_size"])} size'
        f' (total stats in {args.MODE} mode'
        f' over all snapshots)'
    )

    # over and out
    lib.base.oao(msg, state, perfdata)


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