#!/bin/bash

# RustDesk Custom Server Configuration Script
# This script helps configure RustDesk clients to use your custom server

# Replace these with your server details
SERVER_HOST="rust.mytechguy.in"  # e.g., "192.168.1.100" or "rustdesk.example.com"
SERVER_KEY="SMZDQlU87iabxIb+n0pUFvtZyglOgrKGHjomZ+p8uvw="  # Your server's public key from id_ed25519.pub
API_SERVER=""  # Optional: API server URL for Pro features

# Function to configure RustDesk on macOS
configure_macos() {
    echo "Configuring RustDesk for macOS..."

    # Mount the DMG
    hdiutil attach rustdesk-1.4.5-aarch64.dmg

    # Copy the app to Applications (you might need to do this manually or with sudo)
    # cp -r /Volumes/RustDesk/RustDesk.app /Applications/

    # Create config directory if it doesn't exist
    CONFIG_DIR="$HOME/Library/Application Support/RustDesk"
    mkdir -p "$CONFIG_DIR"

    # Create config file
    cat > "$CONFIG_DIR/config.toml" << EOF
[options]
key = "$SERVER_KEY"
custom-rendezvous-server = "$SERVER_HOST"
api-server = "$API_SERVER"
EOF

    echo "macOS configuration created. Please install RustDesk manually from the DMG."
    echo "The configuration will be applied automatically."
}

# Function to configure RustDesk on Windows
configure_windows() {
    echo "Configuring RustDesk for Windows..."

    # Note: Windows configuration would typically be done after installation
    # Since we can't run Windows executables on macOS, we'll create a config template

    echo "For Windows clients:"
    echo "1. Install rustdesk-1.4.5-x86_64.exe normally"
    echo "2. After first run, replace the config file at:"
    echo "   %AppData%\\RustDesk\\config\\RustDesk.toml"
    echo ""
    echo "With the following content:"
    echo "[options]"
    echo "key = \"$SERVER_KEY\""
    echo "custom-rendezvous-server = \"$SERVER_HOST\""
    echo "api-server = \"$API_SERVER\""
}

# Function to generate encrypted config string (for Pro users)
generate_config_string() {
    if [ -n "$API_SERVER" ]; then
        echo "To generate an encrypted config string for command-line usage:"
        echo "1. Go to your RustDesk Server Pro web console"
        echo "2. Navigate to Settings > Network"
        echo "3. Configure your server settings"
        echo "4. Export the configuration string"
        echo "5. Use it with: rustdesk.exe --config <encrypted-string>"
    fi
}

# Main menu
echo "RustDesk Custom Server Configuration"
echo "==================================="
echo ""
echo "Please set your server details at the top of this script first!"
echo "SERVER_HOST: $SERVER_HOST"
echo "SERVER_KEY: $SERVER_KEY"
echo "API_SERVER: $API_SERVER"
echo ""

if [ "$SERVER_KEY" = "" ]; then
    echo "ERROR: Please set SERVER_KEY in the script before running!"
    exit 1
fi

echo "Choose your platform:"
echo "1) macOS (configure locally)"
echo "2) Windows (generate config template)"
echo "3) Generate command-line config info"
echo "4) Show all options"
read -p "Enter choice (1-4): " choice

case $choice in
    1)
        configure_macos
        ;;
    2)
        configure_windows
        ;;
    3)
        generate_config_string
        ;;
    4)
        configure_macos
        echo ""
        configure_windows
        echo ""
        generate_config_string
        ;;
    *)
        echo "Invalid choice"
        ;;
esac