#!/bin/bash
clear

#############################################
# Dev    : HAJJAR, Mohannad
# Co-Dev : BOUYSSOU, Ext-Alexandre
# Script Version
SCRIPT_VER=1.0

# Define ANSI codes for colors and formatting
RED='\033[0;31m'
YELLOW='\033[1;33m'
WHITE='\033[1;37m'    # ANSI code for white text
BOLD='\033[1m'
RESET='\033[0m'
LIGHT_BLUE='\033[1;32m'
CYAN_LIGHT='\033[0;96m'  # ANSI code for light cyan
MAGENTA_LIGHT='\033[1;35m'  # ANSI code for light magenta (violet clair)

# Define environment (set it manually)
# ENVIRONMENT="production"  # This line is removed

# Display the updated warning message
echo -e "${LIGHT_BLUE}${BOLD}          e╔╦╗╦ ╦╦  ╔═╗╔═╗  ╔╦╗╦  ╦  ╔╦╗╔═╗╦  ╔═╗╦ ╦╔═╗╦═╗╔═╗
          e ║ ╚╦╝║  ║╣ ║ ║   ║║╚╗╔╝   ║║║╣ ║  ╠═╣║║║╠═╣╠╦╝║╣ 
          e ╩  ╩ ╩═╝╚═╝╚═╝  ═╩╝ ╚╝o  ═╩╝╚═╝╩═╝╩ ╩╚╩╝╩ ╩╩╚═╚═╝${RESET}"

# Detect the distribution
if [ -f /etc/redhat-release ]; then
    # Red Hat-based
    os_version=$(cat /etc/redhat-release | sed 's/ Linux release//')
elif [ -f /etc/debian_version ]; then
    # Debian-based
    os_version=$(echo "Debian $(cat /etc/debian_version 2>/dev/null || grep '^VERSION_ID=' /etc/os-release | cut -d'=' -f2 | sed 's/\"//g')")
else
    os_version="Unknown"
fi

# Gather user, host, and IP information
user=$(whoami)
hostname=$(hostname)
ip_address=$(hostname -I | awk '{print $1}')

# Get CPU core count and frequency
cpu_cores=$(grep -c ^processor /proc/cpuinfo)
cpu_frequency=$(grep "cpu MHz" /proc/cpuinfo | head -n 1 | awk '{print $4}')
cpu_frequency_ghz=$(awk "BEGIN {printf \"%.2f\", $cpu_frequency / 1000}")

# Get memory information
mem_total=$(free -m | awk '/^Mem:/{print $2}')
mem_free=$(free -m | awk '/^Mem:/{print $7}')
mem_total_gb=$(awk "BEGIN {printf \"%.2f\", $mem_total/1024}")
mem_free_gb=$(awk "BEGIN {printf \"%.2f\", $mem_free/1024}")
mem_used_percent=$(free | awk '/^Mem:/{printf "%.0f", ($2-$7)*100/$2}')

# Set colors and styles
BOLD_YELLOW="\033[1;33m"
BOLD_WHITE="\033[1;37m"
CYAN_LIGHT="\033[0;96m"
MAGENTA_LIGHT="\033[1;35m"
GREEN="\033[0;32m"
RED="\033[0;31m"
RESET="\033[0m"

# Get and check filesystem statuses
fs_status=$(df -h --output=source,fstype,size,used,avail,pcent,target | tail -n +2)
fs_warning=0
partitions_warning=""

while IFS= read -r line; do
    usage=$(echo $line | awk '{print $6}' | sed 's/%//')
    mount_point=$(echo $line | awk '{print $7}')
    if [ "$usage" -ge 90 ]; then
        partitions_warning="${partitions_warning} $mount_point"
        fs_warning=1
    fi
done <<< "$fs_status"  # Redirect fs_status into the loop

# Define the warning message
if [ $fs_warning -eq 0 ]; then
    fs_message="${GREEN}OK, no saturation detected.${RESET}"
else
    fs_message="${RED}Saturation detected on partitions: ${partitions_warning}${RESET}"
fi

# Gather names of connected users
users_connected=$(who | awk '{print $1}' | sort | uniq | tr '\n' ' ' | sed 's/ $//')

# Get system architecture and current date/time
architecture=$(uname -m)
current_date_time=$(date)

# Calculate CPU usage
cpu_usage=$(vmstat 1 2 | tail -1 | awk '{print 100 - $15 "%"}')

# Display environment information
echo -e "${MAGENTA_LIGHT}${BOLD} ----------------------------- FEATURES ------------------------------${RESET}"
# Remove the following line:
# echo -e "${BOLD_WHITE} -- ENVIRONMENT        : ${CYAN_LIGHT}$ENVIRONMENT${RESET}"  # New line for environment
echo -e "${BOLD_WHITE} -- Hostname           : ${CYAN_LIGHT}$hostname${RESET}"
echo -e "${BOLD_WHITE} -- IP Address         : ${CYAN_LIGHT}$ip_address${RESET}"
echo -e "${BOLD_WHITE} -- OS                 : ${CYAN_LIGHT}$os_version${RESET}"
echo -e "${BOLD_WHITE} -- CPU Info           : ${CYAN_LIGHT}$cpu_cores cores @ ${cpu_frequency_ghz} GHz${RESET}"
echo -e "${BOLD_WHITE} -- Total Memory       : ${CYAN_LIGHT}${mem_total_gb} GB${RESET}"
echo -e "${BOLD_WHITE} -- Date & Time        : ${CYAN_LIGHT}$current_date_time${RESET}"

# Get machine uptime
uptime_machine=$(uptime -p | sed 's/up //')

# Display dynamic information
echo -e "
${MAGENTA_LIGHT}${BOLD} --------------------------- INFORMATIONS ----------------------------${RESET}"
echo -e "${BOLD_WHITE} -- Uptime             : ${CYAN_LIGHT}$uptime_machine${RESET}"
echo -e "${BOLD_WHITE} -- Connected Users    : ${CYAN_LIGHT}$users_connected${RESET}"
echo -e "${BOLD_WHITE} -- Memory Usage       : ${CYAN_LIGHT}${mem_used_percent}% used${RESET}"

# Display CPU usage
echo -e "${BOLD_WHITE} -- CPU Usage          : ${CYAN_LIGHT}$cpu_usage${RESET}"

# Display filesystem status
echo -e -n "${BOLD_WHITE} -- FS Status          : ${RESET}"
echo -e "$fs_message"
echo ""