#!/bin/bash
echo "Instalando RustDesk Linux x86_64..."

echo "Descargando instalador..."
wget -q "https://rustdesk.camet.com.ar/instaladores/rustdesk-linux.deb" -O rustdesk-linux.deb || curl -L "https://rustdesk.camet.com.ar/instaladores/rustdesk-linux.deb" -o rustdesk-linux.deb

if [ ! -f "rustdesk-linux.deb" ]; then
    echo "Error: No se pudo descargar rustdesk-linux.deb"
    exit 1
fi

sudo dpkg -i rustdesk-linux.deb
sudo apt-get install -f -y

echo "Descargando configuracion..."
wget -q "https://rustdesk.camet.com.ar/instaladores/RustDesk.toml" -O RustDesk.toml || curl -L "https://rustdesk.camet.com.ar/instaladores/RustDesk.toml" -o RustDesk.toml

if [ -f "RustDesk.toml" ]; then
    mkdir -p ~/.config/rustdesk
    cp RustDesk.toml ~/.config/rustdesk/RustDesk.toml

    if [ ! -z "$SUDO_USER" ]; then
        USER_HOME=$(getent passwd $SUDO_USER | cut -d: -f6)
        mkdir -p $USER_HOME/.config/rustdesk
        cp RustDesk.toml $USER_HOME/.config/rustdesk/RustDesk.toml
        chown -R $SUDO_USER:$SUDO_USER $USER_HOME/.config/rustdesk
    fi
fi

echo "Reiniciando servicio..."
sudo systemctl restart rustdesk

echo "Hecho."
