#!/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.


# mypy: disable-error-code="arg-type"

from cmk.base.check_api import passwordstore_get_cmdline
from cmk.base.config import active_check_info


def check_sftp_arguments(params):
    args = []
    host, user, secret, settings = params

    args.append("--host=%s" % host)
    args.append("--user=%s" % user)
    args.append(passwordstore_get_cmdline("--secret=%s", secret))

    if "port" in settings:
        args.append("--port=%s" % settings["port"])

    if "timeout" in settings:
        args.append("--timeout=%s" % settings["timeout"])

    if "timestamp" in settings:
        args.append("--get-timestamp=%s" % settings["timestamp"])

    if "put" in settings:
        local, remote = settings["put"]
        args.append("--put-local=%s" % local)
        args.append("--put-remote=%s" % remote)

    if "get" in settings:
        remote, local = settings["get"]
        args.append("--get-remote=%s" % remote)
        args.append("--get-local=%s" % local)

    if settings.get("look_for_keys", False):
        args.append("--look-for-keys")

    return args


def check_sftp_desc(params):
    if "description" in params[3]:
        return params[3]["description"]
    return "SFTP %s" % params[0]


active_check_info["sftp"] = {
    "command_line": "check_sftp $ARG1$",
    "argument_function": check_sftp_arguments,
    "service_description": check_sftp_desc,
}
