
#!/bin/bash
#====================================================
# ATP Hosting 24 - Remove Other Licensing Systems
# Domain: atphosting24.com
# This script removes competing/other licensing systems
#====================================================

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

echo -e "${CYAN}"
echo "╔══════════════════════════════════════════════════════════╗"
echo "║    ATP Hosting 24 - Remove Other Licensing Systems      ║"
echo "╚══════════════════════════════════════════════════════════╝"
echo -e "${NC}"

# Check root
if [[ $EUID -ne 0 ]]; then
    echo -e "${RED}[ERROR] This script must be run as root${NC}"
    exit 1
fi

echo -e "${BLUE}[STEP]${NC} Scanning for other licensing systems..."

# List of known competing license directories
LICENSE_DIRS=(
    "/usr/local/src/licensedl"
    "/usr/local/src/cpanel_license"
    "/usr/local/src/cpanel_crack"
    "/usr/local/src/cpanel_nulled"
    "/root/cpanel_license"
    "/root/cpanel_crack"
    "/opt/cpanel_license"
    "/opt/cpanel_crack"
    "/usr/local/share/cpanel_license"
    "/usr/local/cpanel/cpanel_lsel"
)

# List of known competing update scripts
COMPETING_SCRIPTS=(
    "/usr/bin/update_cpanel_license"
    "/usr/bin/cpanel_license_update"
    "/usr/bin/update_license"
    "/usr/bin/cpanel_update_license"
    "/usr/local/bin/update_cpanel_license"
    "/usr/local/bin/cpanel_license_update"
)

# List of known competing cron identifiers
COMPETING_CRON_PATTERNS=(
    "licensedl"
    "cpanel_license"
    "cpanel_crack"
    "cpanel_nulled"
    "update_cpanel_license"
    "cpanel_license_update"
)

# List of known competing service files
COMPETING_SERVICES=(
    "/etc/systemd/system/cpanel_license.service"
    "/etc/systemd/system/cpanel_license_timer.timer"
    "/etc/cron.d/cpanel_license"
    "/etc/cron.d/cpanel_crack"
)

REMOVED_COUNT=0

# Remove competing license directories
echo -e "${BLUE}[STEP]${NC} Checking license directories..."
for dir in "${LICENSE_DIRS[@]}"; do
    if [[ -d "$dir" ]]; then
        # Don't remove if it's our own directory
        if [[ "$dir" == "/usr/local/cpanel/cpanel_lsel" ]] && [[ -f "$dir/config" ]] && grep -q "atphosting24" "$dir/config" 2>/dev/null; then
            echo -e "${GREEN}[INFO]${NC} Skipping ATP Hosting 24 license directory: $dir"
            continue
        fi

        echo -e "${YELLOW}[REMOVE]${NC} Removing license directory: $dir"
        rm -rf "$dir"
        ((REMOVED_COUNT++))
    fi
done

# Remove competing scripts
echo -e "${BLUE}[STEP]${NC} Checking competing scripts..."
for script in "${COMPETING_SCRIPTS[@]}"; do
    if [[ -f "$script" ]]; then
        echo -e "${YELLOW}[REMOVE]${NC} Removing competing script: $script"
        rm -f "$script"
        ((REMOVED_COUNT++))
    fi
done

# Remove competing cron jobs
echo -e "${BLUE}[STEP]${NC} Checking cron jobs..."
CURRENT_CRON=$(crontab -l 2>/dev/null)
for pattern in "${COMPETING_CRON_PATTERNS[@]}"; do
    if echo "$CURRENT_CRON" | grep -q "$pattern"; then
        echo -e "${YELLOW}[REMOVE]${NC} Removing cron job with pattern: $pattern"
        echo "$CURRENT_CRON" | grep -v "$pattern" | crontab -
        CURRENT_CRON=$(crontab -l 2>/dev/null)
        ((REMOVED_COUNT++))
    fi
done

# Remove competing service files
echo -e "${BLUE}[STEP]${NC} Checking service files..."
for service in "${COMPETING_SERVICES[@]}"; do
    if [[ -f "$service" ]]; then
        echo -e "${YELLOW}[REMOVE]${NC} Removing service file: $service"
        rm -f "$service"
        ((REMOVED_COUNT++))
    fi
done

# Reload systemd if needed
if command -v systemctl &> /dev/null; then
    systemctl daemon-reload 2>/dev/null
fi

# Clean up any remaining license-related processes
echo -e "${BLUE}[STEP]${NC} Checking for license-related processes..."
LICENSE_PROCESSES=$(ps aux | grep -E "(license|lsel)" | grep -v grep | grep -v atphosting24 | grep -v "remove_reseller")
if [[ -n "$LICENSE_PROCESSES" ]]; then
    echo -e "${YELLOW}[REMOVE]${NC} Killing competing license processes..."
    echo "$LICENSE_PROCESSES" | awk '{print $2}' | while read pid; do
        kill -9 "$pid" 2>/dev/null
        echo -e "  Killed process: $pid"
        ((REMOVED_COUNT++))
    done
fi

# Remove any leftover license check modifications from other providers
echo -e "${BLUE}[STEP]${NC} Checking for modified cPanel files..."
CPANEL_FILES=(
    "/usr/local/cpanel/bin/check_cpanel_lp"
    "/usr/local/cpanel/whostmgr/bin/licensecheck"
)

for file in "${CPANEL_FILES[@]}"; do
    if [[ -f "${file}.orig_licensedl" ]]; then
        echo -e "${YELLOW}[REMOVE]${NC} Removing licensedl backup: ${file}.orig_licensedl"
        rm -f "${file}.orig_licensedl"
        ((REMOVED_COUNT++))
    fi
    if [[ -f "${file}.orig_cpanel_license" ]]; then
        echo -e "${YELLOW}[REMOVE]${NC} Removing cpanel_license backup: ${file}.orig_cpanel_license"
        rm -f "${file}.orig_cpanel_license"
        ((REMOVED_COUNT++))
    fi
    if [[ -f "${file}.bak" ]]; then
        echo -e "${YELLOW}[REMOVE]${NC} Removing generic backup: ${file}.bak"
        rm -f "${file}.bak"
        ((REMOVED_COUNT++))
    fi
done

# Summary
echo ""
echo -e "${CYAN}═════════════════════════════════════════════════════════════${NC}"
echo -e "${GREEN}[COMPLETE]${NC} Other licensing systems removal finished!"
echo -e "  Items removed: ${YELLOW}${REMOVED_COUNT}${NC}"
echo ""
echo -e "  To install ATP Hosting 24 licensing system, run:"
echo -e "  ${CYAN}bash <( curl -4 https://script.atphosting24.com/pre.sh ) cPanel && /usr/bin/update_cpanelv2${NC}"
echo -e "${CYAN}═════════════════════════════════════════════════════════════${NC}"
