#!/bin/bash
##############################################
#  Cirrata Remote Support - RustDesk Setup
##############################################

set -e

echo ""
echo "  Cirrata Remote Support - RustDesk Setup"
echo "  ========================================"
echo ""

BASE_URL="https://remote.cirrata.com/downloads"
DEB_FILE="rustdesk-1.4.6-x86_64.deb"
CONFIG_DIR="$HOME/.config/rustdesk"

TMPDIR_INSTALL=$(mktemp -d)
DEB_PATH="$TMPDIR_INSTALL/$DEB_FILE"

# Step 1: Download
echo "[1/3] Downloading RustDesk..."
curl -fSL "$BASE_URL/$DEB_FILE" -o "$DEB_PATH"

# Step 2: Install
echo "[2/3] Installing RustDesk..."
if command -v sudo &> /dev/null; then
    sudo dpkg -i "$DEB_PATH" || sudo apt-get install -f -y
else
    dpkg -i "$DEB_PATH" || apt-get install -f -y
fi

# Step 3: Configure
echo "[3/3] Configuring server connection..."
mkdir -p "$CONFIG_DIR"

cat > "$CONFIG_DIR/RustDesk2.toml" << 'TOML'
rendezvous_server = 'remote.cirrata.com'
nat_type = 1
serial = 0

[options]
custom-rendezvous-server = 'remote.cirrata.com'
relay-server = 'remote.cirrata.com'
key = 'Vl3YE4dHzUy0S8kjiY4LkzgIEv+9LBpHBA8zJ4zXBr0='
TOML

# Cleanup
rm -rf "$TMPDIR_INSTALL"

echo ""
echo "  Setup complete! RustDesk is ready to use."
echo "  Server: remote.cirrata.com"
echo ""
