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

# Note: This script is deprecated and has been replaced by the script runas
# which is able to handle mrpe, local and plugin scripts

echo '<<<mrpe>>>'
grep -Ev '^[[:space:]]*($|#)' "$MK_CONFDIR/mrpe_include.cfg" |
    while read -r user include; do
        if [ -f "$include" ]; then
            PREFIX=""
            if [ "$user" != "-" ]; then
                PREFIX="su $user -c "
            fi

            grep -Ev '^[[:space:]]*($|#)' "$include" |
                while read -r descr cmdline; do
                    PLUGIN=${cmdline%% *}
                    if [ -n "$PREFIX" ]; then
                        cmdline="$PREFIX\"$cmdline\""
                    fi
                    OUTPUT=$(eval "$cmdline")
                    echo -n "(${PLUGIN##*/}) $descr $? $OUTPUT" | tr \\n \\1
                    echo
                done
        fi
    done
