#! /bin/bash

# description       : Quality checks a VM on Combell standards
# author            : Joachim
# email             : contact@joachim.gent
# update            :
# version           : 1.0
# notes             :
#==============================================================================

function header {

echo "┌──────────────────────────────────────────────────┐";
echo "│░▄▀▄░█░█░█▀█░█░░░▀█▀░▀█▀░█░█░░░█▀▀░█░█░█▀▀░█▀▀░█░█│";
echo "│░█\█░█░█░█▀█░█░░░░█░░░█░░░█░░░░█░░░█▀█░█▀▀░█░░░█▀▄│";
echo "│░░▀\░▀▀▀░▀░▀░▀▀▀░▀▀▀░░▀░░░▀░░░░▀▀▀░▀░▀░▀▀▀░▀▀▀░▀░▀│";
echo "└──────────────────────────────────────────────────┘";

}

#Menu options

options[0]="Common"
options[1]="Sentinelone"
options[2]="Nginx"
options[3]="Apache"
options[4]="PHP"
options[5]="Mysql"
options[6]="Let's Encrypt"
options[7]="Postfix"
options[8]="Elasticsearch"
options[9]="Redis"
options[10]="Varnish"
options[11]="Proftpd"


#Actions to take based on selection
indent() { sed 's/^/  /'; }

function ACTIONS {

    if [[ ${choices[0]} ]]; then

	echo	─────────────────────────────── SYSTEM ───────────────────────────────
        echo
	
        echo -e "• Hostname:"
        hostname
	echo
        echo -e "• IP('s):"
	hostname -I | awk '{print}' RS=' ' | awk NF
	echo
        echo -e "• OS version:"
        echo -e "$(lsb_release -a | egrep Codename\|Release\|Description)"
        echo

#        echo -e "• CPU):"
        echo -e "• CPU('s):" $(lscpu | grep "CPU(s):" | grep -v NUMA | awk '{print$2}')

#        echo -e "• Memory"
	total_mb=$(free -m | awk '/^Mem:/ {print $2}')
 	total_gb=$(awk -v mb="$total_mb" 'BEGIN { printf "%.0f", mb / 1024 }')
	echo "• Memory: ${total_gb} GB"
#        free -m | grep "Mem" | awk '{print$1,$2}'
        echo

        echo -e "• Storage:"
	disks=$(lsblk -d -o NAME,SIZE,TYPE,MOUNTPOINT | grep -v ' rom' 2>&1)
	mounts=$(df -h | grep ":/" || true )
	#echo -e "$(lsblk -d -o NAME,SIZE,TYPE,MOUNTPOINT)"
	if [[ -z "$disks" || "$disks" == *"failed"* ]]; then
		echo "No disks found."
		echo
		else
		echo "$disks"
		echo "$mounts"
		echo
	fi

        echo • SSH Keys
	authkeys=$(cat /home/*/.ssh/authorized_keys 2>&1)
#        echo -e "$(cat /home/*/.ssh/authorized_keys)"
        if [[ -z "$authkeys" || "$authkeys" == *"No such file or directory"* ]]; then
                echo "No keys found in /home/*/.ssh/authorized_keys"
		echo
                else
                echo "$authkeys"
		echo
        fi
	
    fi
	echo ─────────────────────────────── SERVICES ───────────────────────────────
        echo 

    if [[ ${choices[1]} ]]; then
        echo • Sentinelone
        check_package "sentinelagent"
	echo
    fi

    if [[ ${choices[2]} ]]; then
        echo • Nginx	
        if check_package "nginx"; then
	        echo "• Vhosts:"
		echo "/etc/nginx/sites-available/."
                ls -l /etc/nginx/sites-enabled/ | grep -v "total 0" | indent
                fi
	echo
    fi

    if [[ ${choices[3]} ]]; then
	echo • Apache
        if check_package "apache2"; then
                echo -en "• Vhosts: "
		echo "/etc/nginx/sites-available/"
#        apachectl -S | egrep 80\|443
        ls -l /etc/apache2/sites-enabled/ | grep -v "total 0" | indent
                fi
	echo
    fi

    if [[ ${choices[4]} ]]; then
	echo • PHP
	check_package "php7.4-cli"
        check_package "php8.0-cli"
        check_package "php8.1-cli"
        check_package "php8.2-cli"
        check_package "php8.3-cli"
        check_package "php8.4-cli"
	echo
    fi

# MYSQL

    if [[ ${choices[5]} ]]; then
	echo • MySQL
        check_package "percona-server-client*"
        check_package "postgresql"
        check_package "automydumper"
	echo
	echo • Databases:
#       echo -e '$mysql -e "show databases" # | egrep -v information_schema\|mysql\|performance_schema\|sys\|Database'
	mysql -e "show databases" #| egrep -v information_schema\|mysql\|performance_schema\|sys\|Database
	echo
	echo • Users:
	mysql -e "SELECT user FROM mysql.user;"
	echo
    fi


    if [[ ${choices[6]} ]]; then
	echo • Dehydrated
        check_package "dehydrated"
    fi

    if [[ ${choices[7]} ]]; then
        echo • Postfix
	check_package "postfix"
    fi

    if [[ ${choices[8]} ]]; then
	echo • Elastic
        check_package "elasticsearch"
    fi

    if [[ ${choices[9]} ]]; then
	echo • Redis
        check_package "redis-server"
    fi

    if [[ ${choices[10]} ]]; then
	echo • Varnish
        check_package "varnish"
    fi
    if [[ ${choices[11]} ]]; then
        echo • Proftpd
        check_package "proftpd-core"
    fi

}

#Variables
selected=0  # Tracks the currently highlighted option

#Clear screen for menu
clear

#Menu function
function MENU {


header


    echo "Menu Options"
        echo "Use ↓↑ keys to navigate, x to select/deselect."

    for NUM in ${!options[@]}; do
        if [[ $NUM == $selected ]]; then
            echo -e "\e[32m[${choices[NUM]:- }] $(( NUM+1 )) ) ${options[NUM]}\e[0m"  # Highlight the selected option
        else
            echo "[${choices[NUM]:- }] $(( NUM+1 )) ) ${options[NUM]}"
        fi
    done
    echo
}

# Menu loop
while true; do
    clear
    MENU  # Display the menu
    read -rsn1 input  # Capture the input (silent mode)

    case $input in
        $'\x1b')  # If input is an escape sequence (arrow keys)
            read -rsn2 -t 0.1 input  # Capture next two bytes (for arrow keys)
            if [[ $input == "[A" ]]; then  # Up arrow
                ((selected--))
                if [[ $selected -lt 0 ]]; then
                    selected=$((${#options[@]} - 1))
                fi
            elif [[ $input == "[B" ]]; then  # Down arrow
                ((selected++))
                if [[ $selected -ge ${#options[@]} ]]; then
                    selected=0
                fi
            fi
            ;;
        "x"|"X")  # X key to toggle selection
            if [[ "${choices[selected]}" == "+" ]]; then
                choices[selected]=""
            else
                choices[selected]="+"
            fi
            ;;
        "")  # Enter key to confirm and exit
            break
            ;;
    esac
done


#Check package function
check_package() {
    PACKAGE=$1
    if dpkg -l | grep -q "^ii  $PACKAGE "; then
        VERSION=$(dpkg -l | grep "^ii  $PACKAGE " | awk '{print $3}')
        echo -e "\033[0;32m$PACKAGE is installed. Version: $VERSION\033[m"
                return 0
                echo
    else
        echo -e "\033[31m$PACKAGE is not installed.\033[m"
#               echo
                return 1

    fi
}


# Run the selected actions after exit
ACTIONS
