#!/bin/bash
#
# 00-clusteres - Custom MOTD Script
# Author: Clusteres
# Description: Enhanced MOTD with system information
#

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
GRAY='\033[0;37m'
NC='\033[0m' # No Color
BOLD='\033[1m'

# Services to monitor (comma-separated) full list: systemctl list-units --type=service --state=running 
SERVICES="apache2,ssh,beszel-agent,mariadb,redis-server,cron,fail2ban,webmin,notify_push"

# Function to generate ASCII banner with hostname
generate_banner() {
    local hostname=$(hostname)
    if command -v toilet &> /dev/null; then
        echo -e "${CYAN}${BOLD}"
        toilet -f pagga "$hostname" --filter border
        echo -e "${WHITE}by Clusteres${NC}"
        echo ""
    else
        echo -e "${WHITE}${BOLD}${hostname}${NC}"
        echo -e "${GRAY}by Clusteres${NC}"
        echo ""
    fi
}

# Function to get uptime in a readable format
get_uptime() {
    local uptime_seconds=$(cat /proc/uptime | cut -d' ' -f1 | cut -d'.' -f1)
    local days=$((uptime_seconds / 86400))
    local hours=$(((uptime_seconds % 86400) / 3600))
    local minutes=$(((uptime_seconds % 3600) / 60))
    
    echo -e "${GREEN}up ${days} days, ${hours} hours, ${minutes} minutes${NC}"
}

# Function to get load averages
get_load_average() {
    local load=$(cat /proc/loadavg | cut -d' ' -f1-3)
    echo -e "${YELLOW}load average: ${load}${NC}"
}

# Function to check for available updates
get_updates() {
    local updates=0
    if command -v apt &> /dev/null; then
        updates=$(apt list --upgradable 2>/dev/null | grep -c upgradable)
        if [ $updates -gt 1 ]; then
            updates=$((updates - 1)) # Remove header line
        fi
    elif command -v yum &> /dev/null; then
        updates=$(yum check-update 2>/dev/null | grep -c "^[a-zA-Z]")
    fi
    
    if [ $updates -gt 0 ]; then
        echo -e "${RED}${updates} updates available${NC}"
    else
        echo -e "${GREEN}System up to date${NC}"
    fi
}

# Function to check service status
check_services() {
    echo -e "${BLUE}${BOLD}Services:${NC}"
    
    # Convert comma-separated services to array
    IFS=',' read -ra services_array <<< "$SERVICES"
    
    for service in "${services_array[@]}"; do
        # Trim whitespace
        service=$(echo "$service" | xargs)
        
        if systemctl is-active --quiet "$service" 2>/dev/null; then
            printf "    %-20s ${GREEN}Running${NC}\n" "${service}:"
        else
            printf "    %-20s ${RED}Stopped${NC}\n" "${service}:"
        fi
    done
}

# Function to display filesystem information with progress bars
show_filesystems() {
    echo -e "${PURPLE}${BOLD}Filesystems${NC}"
    
    # Get filesystem info and create visual representation
    df -h --output=target,size,used,avail,pcent | grep -E "^/" | while read -r mount size used avail percent; do
        # Remove % from percentage
        percent_num=${percent%\%}
        
        # Determine color based on usage
        if [ "$percent_num" -ge 90 ]; then
            color=$RED
        elif [ "$percent_num" -ge 75 ]; then
            color=$YELLOW
        else
            color=$GREEN
        fi
        
        # Create progress bar
        bar_length=20
        filled_length=$((percent_num * bar_length / 100))
        bar=""
        
        for ((i=0; i<filled_length; i++)); do
            bar+="█"
        done
        
        for ((i=filled_length; i<bar_length; i++)); do
            bar+="░"
        done
        
        printf "  %-25s ${color}[%s]${NC} %s/%s (%s)\n" "$mount" "$bar" "$used" "$size" "$percent"
    done
}

# Main execution
main() {
    generate_banner
    echo ""
    get_uptime
    get_load_average
    get_updates
    echo ""
    check_services
    echo ""
    show_filesystems
    echo ""
    echo -e "${GRAY}Last updated: $(date)${NC}"
    echo ""
}

# Run main function
main
