#!/bin/bash
set -u

# supportconfig plugin for sapconf.
#
# v1.0
#
# March 2021    v1.0    first release

function display_package_info() {
    echo -e "\n#==[ Command ]======================================#"
    echo -e "# rpm -q ${1}"
    rpm -q "${1}"

    echo -e "\n#==[ Command ]======================================#"
    echo -e "# rpm -V ${1}"
    rpm -V "${1}"
}

function display_file_stat() {
    echo -e "\n#==[ Configuration File ]===========================#"
    echo -e "# ls -ld ${1} ; stat ${1} \n"
    
    if [ -e "${1}" ] ; then
        ls -ld "${1}"
        echo
        stat "${1}"
    else
        echo "${1} does not exist!"
    fi
}

function display_dir_stat() {
    echo -e "\n#==[ Configuration Dir ]============================#"
    echo -e "# find ${1} -> ls -ld / stat \n"
    
    if [ -e "${1}" ] ; then
        while read -r obj ; do
            ls -ld "${obj}"
        done < <(find "${1}")
        while read -r obj ; do
            echo
            stat "${obj}"
        done < <(find "${1}")
    else
        echo "${1} does not exist!"
    fi
}

function display_file() {
    echo -e "\n#==[ Configuration File ]===========================#"
    echo -e "# cat ${1}"

    if [ -e "${1}" ] ; then
        cat "${1}"
    else
        echo "${1} does not exist!"
    fi
}

function display_systemd_status() {
    echo -e "\n#==[ Command ]======================================#"
    echo -e "# systemctl status ${1}"
    
    systemctl status "${1}"
}

function display_cmd() {
    echo -e "\n#==[ Command ]======================================#"
    echo -e "# $*"
    "${@}"
}

function display_log() {
    local file
    echo -e "\n#==[ Log Files ]====================================#"
    for file in "${@}" ; do
        echo -e "\n# ${file}"
        cat "${file}"
    done
}

# ---- Main ----
display_cmd sapconf_check
display_package_info tuned
display_file_stat /usr/lib/tuned/functions
display_systemd_status tuned
display_cmd tuned-adm list
display_file_stat /etc/tuned/active_profile
display_file /etc/tuned/active_profile
display_file_stat /var/run/tuned/
display_file_stat /usr/lib/tuned/sapconf/script.sh
display_log /var/log/tuned/tuned.log*

display_package_info sapconf
display_file /var/log/sapconf.log
display_file /etc/sysconfig/sapconf
display_systemd_status sapconf
display_file_stat /var/run/sapconf/active
display_file_stat /run/sapconf_act_profile
display_file /run/sapconf_act_profile
display_file_stat /var/lib/sapconf/last_profile
display_file /var/lib/sapconf/last_profile
display_file_stat /var/lib/sapconf/act_profile
display_file /var/lib/sapconf/act_profile
display_file_stat /var/lib/sapconf
display_dir_stat /var/lib/sapconf

# Bye.
exit 0
