#!/bin/bash
# RustDesk deployment — LXCo self-hosted server (rust.lxco.au)
# Target: Linux fleet (Ubuntu/Debian/CentOS/RHEL/Fedora), run as root.
# Each machine gets a RANDOM unattended-access password; the script prints ID + password.
# Source: adapted from official RustDesk client-deployment docs (verified 2026-08-02).

rustdesk_pw=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
rustdesk_cfg="9JiI6ISawFmIsISP4g0T0V3S4VWWV5UOVRkQwUHdyUUMr4kM0clWYpWTjtkZaNDb5QTZW5keZJiOikXZrJCLiUXYu82Y4xmL0NXdyJiOikXYsVmciwiI1FmLvNGes5CdzVnciojI0N3boJye"

if [[ $EUID -ne 0 ]]; then echo "This script must be run as root."; exit 1; fi

. /etc/os-release
ID="$ID"

if [ "$ID" = "debian" ] || [ "$ID" = "ubuntu" ]; then
    wget -q https://github.com/rustdesk/rustdesk/releases/download/1.4.9/rustdesk-1.4.9-x86_64.deb -O /tmp/rustdesk.deb
    apt-get install -fy /tmp/rustdesk.deb > /dev/null
elif [ "$ID" = "centos" ] || [ "$ID" = "rhel" ] || [ "$ID" = "fedora" ] || [ "$ID" = "almalinux" ] || [ "$ID" = "rocky" ]; then
    wget -q https://github.com/rustdesk/rustdesk/releases/download/1.4.9/rustdesk-1.4.9-0.x86_64.rpm -O /tmp/rustdesk.rpm
    yum localinstall /tmp/rustdesk.rpm -y > /dev/null
else
    echo "Unsupported OS"; exit 1
fi

rustdesk_id=$(rustdesk --get-id)
rustdesk --password "$rustdesk_pw" &> /dev/null
rustdesk --config "$rustdesk_cfg"
systemctl restart rustdesk
sleep 2

echo "..............................................."
echo "RustDesk ID: $rustdesk_id"
echo "Password: $rustdesk_pw"
echo "Server: rust.lxco.au"
echo "..............................................."
