#!/bin/bash
#
# Copyright (C) 2025 Majaahh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# [
CHECK_EXEC()
{
    command -v "$1" &> /dev/null
    return $?
}

# EVAL <cmd>
# Executes the provided command and prints its output if it returns a non-zero exit code.
EVAL()
{
    local CMD="$1"

    local OUT="$( (eval "$CMD") 2>&1 )"
    # shellcheck disable=SC2181,SC2291
    if [[ $? -ne 0 ]]; then
        LOGE "Command returned a non-zero exit code\n"
        echo -e    '\033[0;31m'"$CMD"'\033[0m\n' >&2
        echo -n -e '\033[0;33m' >&2
        echo -n    "$OUT" >&2
        echo -e    '\033[0m' >&2
        return 1
    fi

    return 0
}
# ]
