#!/bin/bash

set -e

GREEN="\e[32m"
RED="\e[31m"
YELLOW="\e[33m"
RESET="\e[0m"

OS_ID="$(. /etc/os-release && echo "$ID")"

success() { echo -e "${GREEN}OK - $1${RESET}"; }
warning() { echo -e "${YELLOW}INFO - $1${RESET}"; }
error() { echo -e "${RED}ERROR - $1${RESET}"; }

echo "-------------------- Resolver Selection --------------------"

echo "1) HostIran (default)"
echo "2) ArvanCloud"
read -p "Enter choice [1-2]: " RESOLVER_CHOICE

if [[ -z "$RESOLVER_CHOICE" || "$RESOLVER_CHOICE" == "1" ]]; then
    NS1="172.29.0.14"
    NS2="172.29.2.14"
    RESOLVER_NAME="HostIran"
elif [[ "$RESOLVER_CHOICE" == "2" ]]; then
    NS1="217.218.127.127"
    NS2="217.218.155.155"
    RESOLVER_NAME="ArvanCloud"
else
    error "Invalid choice. Using default HostIran."
    NS1="172.29.0.14"
    NS2="172.29.2.14"
    RESOLVER_NAME="HostIran"
fi

cat > /etc/resolv.conf <<EOF
nameserver $NS1
nameserver $NS2
EOF

success "Resolver set: $RESOLVER_NAME"
echo -e "${GREEN}nameserver $NS1\nnameserver $NS2${RESET}"

echo "------------------------------------------------------------"

echo "-------------------- cPanel Update Config ------------------"

if [[ "$OS_ID" == "centos" ]]; then
cat > /etc/cpupdate.conf <<'EOF'
CPANEL=11.110
RPMUP=daily
SARULESUP=daily
STAGING_DIR=/usr/local/cpanel
UPDATES=daily
EOF
success "Configured for CentOS"
else
cat > /etc/cpupdate.conf <<'EOF'
CPANEL=release
RPMUP=daily
SARULESUP=daily
STAGING_DIR=/usr/local/cpanel
UPDATES=daily
EOF
success "Configured for $OS_ID"
fi

echo -e "${GREEN}$(cat /etc/cpupdate.conf)${RESET}"

echo "------------------------------------------------------------"

echo "-------------------- cPanel Source -------------------------"

cat > /etc/cpsources.conf <<'EOF'
HTTPUPDATE=1.mirror.ir.cdn.mycache.org
EOF

success "cPanel source configured"
echo -e "${GREEN}$(cat /etc/cpsources.conf)${RESET}"

echo "------------------------------------------------------------"

echo "-------------------- CloudLinux Check ----------------------"

if [[ "$OS_ID" == "cloudlinux" ]]; then
    if grubby --update-kernel=ALL --args="initcall_blacklist=algif_aead_init"; then
        success "CloudLinux detected - security fix applied"
    else
        error "CloudLinux detected but grubby failed"
    fi
else
    warning "CloudLinux not detected - skipped"
fi

echo "------------------------------------------------------------"

echo "-------------------- Restart cPanel ------------------------"

if [ -x /scripts/restartsrv_cpsrvd ]; then
    if /scripts/restartsrv_cpsrvd; then
        success "cpsrvd restarted"
    else
        error "cpsrvd restart failed"
    fi
else
    error "cpsrvd script not found"
fi

echo "------------------------------------------------------------"

echo "-------------------- Cleanup Sessions ----------------------"

rm -rf /var/cpanel/sessions/*
rm -rf /var/cpanel/session/*

success "Sessions cleaned"

echo "------------------------------------------------------------"

echo "-------------------- Version Check -------------------------"

CPANEL_VERSION="$(cat /usr/local/cpanel/version 2>/dev/null || true)"

SAFE_VERSIONS="
11.86.0.41
11.110.0.97
11.118.0.63
11.126.0.54
11.130.0.19
11.132.0.29
11.136.0.5
11.134.0.20
"

echo "cPanel version: $CPANEL_VERSION"

if echo "$SAFE_VERSIONS" | grep -qx "$CPANEL_VERSION"; then
    success "cPanel is secure"
else
    error "cPanel needs update"

    read -p "Do you want to update cPanel now? (y/n): " UPDATE_CONFIRM

    if [[ "$UPDATE_CONFIRM" == "y" || "$UPDATE_CONFIRM" == "Y" ]]; then
        echo "Starting cPanel update..."
        /scripts/upcp --force
        success "cPanel update completed"
    else
        warning "cPanel update skipped by user"
    fi
fi

echo "------------------------------------------------------------"