#!/usr/bin/env bash
# =============================================================================
# TuxTuner
# Enterprise Linux Optimization Tool
#
# Version: 1.0
# Author : Koosha Yeganeh
# Company: KYGnus 
#
# Interface:
#   Pure CLI switches only
#
# Designed for:
#   SSH
#   TTY
#   Console
#   Servers
#   Desktop Linux
#   Old hardware
#   Remote administration
#
# No X11 / Wayland / GTK / Qt dependency.
# =============================================================================

set -uo pipefail

# =============================================================================
# GLOBAL CONFIGURATION
# =============================================================================

readonly APP_NAME="TuxTuner"
readonly VERSION="1.0"

readonly STATE_DIR="/var/lib/tuxtuner"
readonly BACKUP_DIR="${STATE_DIR}/backups"
readonly LOG_DIR="/var/log/tuxtuner"
readonly LOG_FILE="${LOG_DIR}/tuxtuner.log"

TMP_DIR="/tmp/tuxtuner_$(date +%Y%m%d_%H%M%S)_$$"

DISTRO=""
DISTRO_VERSION=""
DISTRO_FAMILY=""
DISTRO_PRETTY=""

DRY_RUN=0
ASSUME_YES=0
BACKUP_ENABLED=1

# =============================================================================
# ACTION FLAGS
# =============================================================================

ACTION_INFO=0
ACTION_STATUS=0

PROFILE_DESKTOP=0
PROFILE_SERVER=0
PROFILE_EMBEDDED=0

OPT_STORAGE=0
OPT_MEDIA=0
OPT_STORAGE_SERVER=0
OPT_IDSIPS=0

ENTERPRISE_KERNEL=0
ENTERPRISE_SERVICES=0
ENTERPRISE_SECURITY=0

# =============================================================================
# COLORS
# =============================================================================

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
DIM='\033[2m'
NC='\033[0m'

# =============================================================================
# BASIC HELPERS
# =============================================================================

command_exists() {
    command -v "$1" >/dev/null 2>&1
}

is_root() {
    [[ "$(id -u)" -eq 0 ]]
}

# =============================================================================
# LOGGING
# =============================================================================

init_logging() {

    if is_root; then

        mkdir -p "$LOG_DIR" 2>/dev/null || true

        if ! touch "$LOG_FILE" 2>/dev/null; then
            LOG_DIR="/tmp"
            LOG_FILE="/tmp/tuxtuner.log"
        fi

    else

        LOG_DIR="/tmp"
        LOG_FILE="/tmp/tuxtuner.log"

    fi

    touch "$LOG_FILE" 2>/dev/null || true

    exec > >(tee -a "$LOG_FILE") 2>&1

    echo
    echo "============================================================"
    echo "${APP_NAME} v${VERSION}"
    echo "Started: $(date)"
    echo "============================================================"
}

log_info() {
    echo -e "${BLUE}[INFO]${NC} $*"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $*"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $*"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $*"
}

log_action() {

    if [[ "$DRY_RUN" -eq 1 ]]; then
        echo -e "${CYAN}[DRY-RUN]${NC} $*"
    else
        echo -e "${MAGENTA}[ACTION]${NC} $*"
    fi
}

# =============================================================================
# CLEANUP
# =============================================================================

cleanup() {

    local rc=$?

    if [[ -n "${TMP_DIR:-}" && -d "$TMP_DIR" ]]; then
        rm -rf "$TMP_DIR" 2>/dev/null || true
    fi

    trap - EXIT

    exit "$rc"
}

trap cleanup EXIT

init_temp() {

    mkdir -p "$TMP_DIR" || {
        log_error "Unable to create temporary directory."
        exit 1
    }

    log_info "Temporary directory: $TMP_DIR"
}

# =============================================================================
# ROOT
# =============================================================================

require_root() {

    if ! is_root; then

        log_error "TuxTuner requires root privileges."

        echo
        echo "Run:"
        echo
        echo "  sudo $0 $*"
        echo

        exit 1
    fi
}

# =============================================================================
# DISTRIBUTION DETECTION
# =============================================================================

detect_distro() {

    DISTRO=""
    DISTRO_VERSION=""
    DISTRO_FAMILY=""
    DISTRO_PRETTY=""

    if [[ -f /etc/os-release ]]; then

        # shellcheck disable=SC1091
        . /etc/os-release

        DISTRO="${ID:-unknown}"
        DISTRO_VERSION="${VERSION_ID:-unknown}"
        DISTRO_PRETTY="${PRETTY_NAME:-$DISTRO}"

    elif [[ -f /etc/debian_version ]]; then

        DISTRO="debian"
        DISTRO_VERSION="$(cat /etc/debian_version)"
        DISTRO_PRETTY="Debian ${DISTRO_VERSION}"

    elif [[ -f /etc/redhat-release ]]; then

        DISTRO="rhel"
        DISTRO_VERSION="$(
            grep -oE '[0-9]+(\.[0-9]+)?' /etc/redhat-release |
            head -n1
        )"

        DISTRO_PRETTY="$(cat /etc/redhat-release)"

    else

        DISTRO="$(uname -s | tr '[:upper:]' '[:lower:]')"
        DISTRO_VERSION="$(uname -r)"
        DISTRO_PRETTY="${DISTRO} ${DISTRO_VERSION}"

    fi

    case "$DISTRO" in

        debian|ubuntu|linuxmint|pop|elementary)
            DISTRO_FAMILY="debian"
            ;;

        opensuse|opensuse-leap|opensuse-tumbleweed|sles|sled)
            DISTRO_FAMILY="suse"
            ;;

        fedora|rhel|centos|rocky|almalinux|ol)
            DISTRO_FAMILY="redhat"
            ;;

        arch|manjaro|endeavouros)
            DISTRO_FAMILY="arch"
            ;;

        alpine)
            DISTRO_FAMILY="alpine"
            ;;

        *)
            DISTRO_FAMILY="unknown"
            ;;

    esac

    log_info "Distribution : $DISTRO_PRETTY"
    log_info "Family       : $DISTRO_FAMILY"
}

# =============================================================================
# BACKUP
# =============================================================================

backup_file() {

    local file="$1"

    [[ "$BACKUP_ENABLED" -eq 1 ]] || return 0
    [[ -e "$file" ]] || return 0

    local timestamp
    timestamp="$(date +%Y%m%d_%H%M%S)"

    local destination
    destination="${BACKUP_DIR}/${timestamp}"

    mkdir -p "$destination"

    local safe_name
    safe_name="${file#/}"
    safe_name="${safe_name//\//_}"

    cp -a "$file" "${destination}/${safe_name}" 2>/dev/null || true

    log_info "Backup: ${destination}/${safe_name}"
}

# =============================================================================
# COMMAND EXECUTION
# =============================================================================

run_cmd() {

    log_action "$*"

    if [[ "$DRY_RUN" -eq 1 ]]; then
        return 0
    fi

    "$@"
}

# =============================================================================
# SYSCTL
# =============================================================================

set_sysctl() {

    local key="$1"
    local value="$2"

    local config="/etc/sysctl.d/99-tuxtuner.conf"

    log_action "sysctl: ${key}=${value}"

    if [[ "$DRY_RUN" -eq 1 ]]; then
        return 0
    fi

    mkdir -p /etc/sysctl.d

    touch "$config"

    backup_file "$config"

    if grep -Eq "^[[:space:]]*${key}[[:space:]]*=" "$config"; then

        sed -i -E \
            "s|^[[:space:]]*${key}[[:space:]]*=.*|${key} = ${value}|" \
            "$config"

    else

        echo "${key} = ${value}" >> "$config"

    fi

    if command_exists sysctl; then
        sysctl -w "${key}=${value}" >/dev/null 2>&1 || true
    fi
}

reload_sysctl() {

    [[ "$DRY_RUN" -eq 1 ]] && return 0

    if command_exists sysctl; then
        sysctl --system >/dev/null 2>&1 || \
            log_warning "Some sysctl settings could not be reloaded."
    fi
}

# =============================================================================
# SYSTEM INFORMATION
# =============================================================================

show_system_info() {

    echo
    echo "============================================================"
    echo " TuxTuner System Information"
    echo "============================================================"

    echo
    echo "TuxTuner"
    echo "  Version       : ${VERSION}"

    echo
    echo "Operating System"
    echo "  Distribution  : ${DISTRO_PRETTY}"
    echo "  ID            : ${DISTRO}"
    echo "  Version       : ${DISTRO_VERSION}"
    echo "  Family        : ${DISTRO_FAMILY}"

    echo
    echo "Kernel"
    echo "  Kernel        : $(uname -r)"
    echo "  Architecture  : $(uname -m)"
    echo "  Hostname      : $(hostname)"

    if command_exists uptime; then
        echo "  Uptime        : $(uptime -p 2>/dev/null || uptime)"
    fi

    echo
    echo "CPU"

    if [[ -r /proc/cpuinfo ]]; then
        grep -m1 "model name" /proc/cpuinfo |
            sed 's/^/  /'
    fi

    echo "  CPU cores     : $(nproc 2>/dev/null || echo unknown)"

    echo
    echo "Memory"

    if command_exists free; then
        free -h
    fi

    echo
    echo "Storage"

    if command_exists lsblk; then
        lsblk \
            -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS \
            2>/dev/null || true
    fi

    echo
    echo "Network"

    if command_exists ip; then
        ip -brief address 2>/dev/null || true
    fi

    echo
}

# =============================================================================
# STATUS
# =============================================================================

show_status() {

    echo
    echo "============================================================"
    echo " TuxTuner Status"
    echo "============================================================"

    echo
    echo "Version       : ${VERSION}"
    echo "Distribution  : ${DISTRO_PRETTY}"
    echo "Family        : ${DISTRO_FAMILY}"
    echo "Kernel        : $(uname -r)"
    echo "Architecture  : $(uname -m)"
    echo "Dry-run       : ${DRY_RUN}"
    echo "Backup        : ${BACKUP_ENABLED}"
    echo "Log           : ${LOG_FILE}"
    echo "State         : ${STATE_DIR}"
    echo "Backups       : ${BACKUP_DIR}"

    echo

    if command_exists systemctl; then
        echo "Service manager: systemd"
    elif command_exists rc-service; then
        echo "Service manager: OpenRC"
    elif command_exists service; then
        echo "Service manager: SysV/service"
    else
        echo "Service manager: unknown"
    fi

    echo
}

# =============================================================================
# DESKTOP PROFILE
# =============================================================================

opt_desktop() {

    echo
    log_info "Applying desktop optimization..."

    set_sysctl "vm.swappiness" "10"
    set_sysctl "vm.vfs_cache_pressure" "50"

    reload_sysctl

    log_success "Desktop optimization completed."
}

# =============================================================================
# SERVER PROFILE
# =============================================================================

opt_server() {

    echo
    log_info "Applying server optimization..."

    set_sysctl "vm.swappiness" "10"
    set_sysctl "vm.vfs_cache_pressure" "50"

    set_sysctl "net.core.somaxconn" "1024"
    set_sysctl "net.ipv4.tcp_max_syn_backlog" "4096"

    reload_sysctl

    log_success "Server optimization completed."
}

# =============================================================================
# STORAGE
# =============================================================================

opt_storage() {

    echo
    log_info "Applying storage optimization..."

    set_sysctl "vm.dirty_background_ratio" "5"
    set_sysctl "vm.dirty_ratio" "15"

    reload_sysctl

    if command_exists fstrim; then

        log_action "Running filesystem TRIM."

        if [[ "$DRY_RUN" -eq 0 ]]; then
            fstrim -av || \
                log_warning "fstrim returned an error."
        fi

    else

        log_warning "fstrim is not installed."

    fi

    log_success "Storage optimization completed."
}

# =============================================================================
# MEDIA SERVER
# =============================================================================

opt_media() {

    echo
    log_info "Applying media-server optimization..."

    set_sysctl "net.core.rmem_max" "16777216"
    set_sysctl "net.core.wmem_max" "16777216"

    reload_sysctl

    log_success "Media-server optimization completed."
}

# =============================================================================
# STORAGE SERVER
# =============================================================================

opt_storage_server() {

    echo
    log_info "Applying storage-server optimization..."

    set_sysctl "vm.dirty_background_ratio" "5"
    set_sysctl "vm.dirty_ratio" "10"
    set_sysctl "vm.swappiness" "10"

    reload_sysctl

    if command_exists fstrim; then

        if [[ "$DRY_RUN" -eq 0 ]]; then
            fstrim -av || true
        else
            log_action "fstrim -av"
        fi

    fi

    log_success "Storage-server optimization completed."
}

# =============================================================================
# IDS / IPS
# =============================================================================

opt_idsips() {

    echo
    log_info "Applying IDS/IPS network tuning..."

    set_sysctl "net.core.somaxconn" "4096"
    set_sysctl "net.core.netdev_max_backlog" "5000"
    set_sysctl "net.ipv4.tcp_max_syn_backlog" "4096"

    reload_sysctl

    log_success "IDS/IPS optimization completed."
}

# =============================================================================
# EMBEDDED
# =============================================================================

opt_embedded() {

    echo
    log_info "Applying embedded-system optimization..."

    set_sysctl "vm.swappiness" "5"
    set_sysctl "vm.vfs_cache_pressure" "75"

    reload_sysctl

    log_success "Embedded optimization completed."
}

# =============================================================================
# ENTERPRISE KERNEL
# =============================================================================

enterprise_kernel() {

    echo
    log_info "Enterprise kernel tuning..."

    set_sysctl "vm.swappiness" "10"
    set_sysctl "vm.vfs_cache_pressure" "50"

    set_sysctl "net.core.somaxconn" "1024"
    set_sysctl "net.ipv4.tcp_max_syn_backlog" "4096"

    reload_sysctl

    log_success "Enterprise kernel tuning completed."
}

# =============================================================================
# ENTERPRISE SERVICES
# =============================================================================

enterprise_services() {

    echo
    log_info "Enterprise service analysis..."

    if command_exists systemctl; then

        systemctl list-unit-files \
            --type=service \
            --state=enabled \
            --no-pager \
            2>/dev/null |
            head -n 50

    elif command_exists rc-status; then

        rc-status 2>/dev/null || true

    elif command_exists service; then

        service --status-all 2>/dev/null || true

    else

        log_warning "No recognized service manager found."

    fi

    log_success "Service analysis completed."
}

# =============================================================================
# ENTERPRISE SECURITY
# =============================================================================

enterprise_security() {

    echo
    log_info "Enterprise security hardening..."

    set_sysctl "net.ipv4.conf.all.rp_filter" "1"
    set_sysctl "net.ipv4.conf.default.rp_filter" "1"

    set_sysctl "net.ipv4.icmp_echo_ignore_broadcasts" "1"

    set_sysctl "net.ipv4.conf.all.accept_source_route" "0"
    set_sysctl "net.ipv4.conf.default.accept_source_route" "0"

    set_sysctl "net.ipv4.conf.all.accept_redirects" "0"
    set_sysctl "net.ipv4.conf.default.accept_redirects" "0"

    set_sysctl "net.ipv4.conf.all.send_redirects" "0"

    reload_sysctl

    log_success "Enterprise security hardening completed."
}

# =============================================================================
# ENTERPRISE ALL
# =============================================================================

enterprise_all() {

    echo
    log_info "Running complete enterprise optimization."

    enterprise_kernel
    enterprise_services
    enterprise_security

    log_success "Enterprise optimization completed."
}

# =============================================================================
# ACTION LIST
# =============================================================================

list_actions() {

    cat <<EOF

Available TuxTuner switches
===========================

Profiles:
  --desktop
  --server
  --embedded

Optimization:
  --storage
  --mediaserver
  --storageserver
  --idsips

Enterprise:
  --enterprise-kernel
  --enterprise-services
  --enterprise-security
  --enterprise-all

Information:
  --info
  --status
  --list
  --version
  --help

Execution:
  --dry-run
  --yes
  --no-backup

EOF
}

# =============================================================================
# CLI EXECUTION
# =============================================================================

run_cli() {

    local executed=0

    if [[ "$ACTION_INFO" -eq 1 ]]; then
        show_system_info
        executed=1
    fi

    if [[ "$ACTION_STATUS" -eq 1 ]]; then
        show_status
        executed=1
    fi

    if [[ "$PROFILE_DESKTOP" -eq 1 ]]; then
        opt_desktop
        executed=1
    fi

    if [[ "$PROFILE_SERVER" -eq 1 ]]; then
        opt_server
        executed=1
    fi

    if [[ "$PROFILE_EMBEDDED" -eq 1 ]]; then
        opt_embedded
        executed=1
    fi

    if [[ "$OPT_STORAGE" -eq 1 ]]; then
        opt_storage
        executed=1
    fi

    if [[ "$OPT_MEDIA" -eq 1 ]]; then
        opt_media
        executed=1
    fi

    if [[ "$OPT_STORAGE_SERVER" -eq 1 ]]; then
        opt_storage_server
        executed=1
    fi

    if [[ "$OPT_IDSIPS" -eq 1 ]]; then
        opt_idsips
        executed=1
    fi

    if [[ "$ENTERPRISE_KERNEL" -eq 1 ]]; then
        enterprise_kernel
        executed=1
    fi

    if [[ "$ENTERPRISE_SERVICES" -eq 1 ]]; then
        enterprise_services
        executed=1
    fi

    if [[ "$ENTERPRISE_SECURITY" -eq 1 ]]; then
        enterprise_security
        executed=1
    fi

    if [[ "$executed" -eq 0 ]]; then
        log_error "No action selected."
        return 1
    fi

    return 0
}

# =============================================================================
# ARGUMENT PARSER
# =============================================================================

parse_args() {

    while [[ $# -gt 0 ]]; do

        case "$1" in

            --help|-h)
                list_actions
                exit 0
                ;;

            --version|-V)
                echo "${APP_NAME} v${VERSION}"
                exit 0
                ;;

            --info)
                ACTION_INFO=1
                ;;

            --status)
                ACTION_STATUS=1
                ;;

            --list)
                list_actions
                exit 0
                ;;

            --desktop)
                PROFILE_DESKTOP=1
                ;;

            --server)
                PROFILE_SERVER=1
                ;;

            --embedded)
                PROFILE_EMBEDDED=1
                ;;

            --storage)
                OPT_STORAGE=1
                ;;

            --mediaserver|--media-server)
                OPT_MEDIA=1
                ;;

            --storageserver|--storage-server)
                OPT_STORAGE_SERVER=1
                ;;

            --idsips|--ids-ips)
                OPT_IDSIPS=1
                ;;

            --enterprise-kernel)
                ENTERPRISE_KERNEL=1
                ;;

            --enterprise-services)
                ENTERPRISE_SERVICES=1
                ;;

            --enterprise-security)
                ENTERPRISE_SECURITY=1
                ;;

            --enterprise-all)

                ENTERPRISE_KERNEL=1
                ENTERPRISE_SERVICES=1
                ENTERPRISE_SECURITY=1
                ;;

            --dry-run)
                DRY_RUN=1
                ;;

            --yes|-y)
                ASSUME_YES=1
                ;;

            --no-backup)
                BACKUP_ENABLED=0
                ;;

            --)
                shift
                break
                ;;

            -*)

                echo
                log_error "Unknown option: $1"
                echo "Use --help."
                exit 2
                ;;

            *)

                log_error "Unexpected argument: $1"
                exit 2
                ;;

        esac

        shift

    done
}

# =============================================================================
# CONFIRMATION
# =============================================================================

confirm_changes() {

    [[ "$DRY_RUN" -eq 1 ]] && return 0
    [[ "$ASSUME_YES" -eq 1 ]] && return 0

    echo
    echo "============================================================"
    echo " WARNING"
    echo "============================================================"
    echo
    echo "TuxTuner will modify system configuration."
    echo
    echo "Distribution : ${DISTRO_PRETTY}"
    echo "Kernel       : $(uname -r)"
    echo
    echo "Backups      : ${BACKUP_ENABLED}"
    echo

    printf "Continue? [y/N]: "

    local answer
    read -r answer

    case "$answer" in

        y|Y|yes|YES)
            return 0
            ;;

        *)
            log_warning "Operation cancelled."
            return 1
            ;;

    esac
}

# =============================================================================
# MAIN
# =============================================================================

main() {

    parse_args "$@"

    # -------------------------------------------------------------------------
    # Information-only operations
    # -------------------------------------------------------------------------

    if [[ "$ACTION_INFO" -eq 1 ]] ||
       [[ "$ACTION_STATUS" -eq 1 ]]; then

        detect_distro

        [[ "$ACTION_INFO" -eq 1 ]] && show_system_info
        [[ "$ACTION_STATUS" -eq 1 ]] && show_status

        exit 0
    fi

    # -------------------------------------------------------------------------
    # Everything else requires root
    # -------------------------------------------------------------------------

    require_root "$@"

    init_logging
    init_temp
    detect_distro

    mkdir -p "$STATE_DIR"

    if [[ "$BACKUP_ENABLED" -eq 1 ]]; then
        mkdir -p "$BACKUP_DIR"
    fi

    echo

    log_info "${APP_NAME} v${VERSION}"
    log_info "Terminal-based Linux optimization framework."

    if [[ "$DRY_RUN" -eq 1 ]]; then
        log_warning "DRY-RUN MODE ENABLED"
        log_warning "No system changes will be performed."
    fi

    if ! confirm_changes; then
        exit 1
    fi

    run_cli

    echo
    log_success "TuxTuner completed."
    log_info "Log: ${LOG_FILE}"

    if [[ "$BACKUP_ENABLED" -eq 1 ]]; then
        log_info "Backups: ${BACKUP_DIR}"
    fi
}

# =============================================================================
# START
# =============================================================================

main "$@"
