#!/bin/bash
#author:Cristiano Rodrigues Siqueira Soares
#descricao:Instala o rustdesk no computador.
#Revisao:

user=`whoami`

PASSWORD="Supersig"
CONFIG_DIR=~/.config/rustdesk
CONFIG_FILE="$CONFIG_DIR/config.toml"

check_status() {
    if [ $? -ne 0 ]; then
        echo -e "\033[01m\033[31m\033[21mErro detectado. Tentando corrigir dependências quebradas...\033[0m"
        sudo apt --fix-broken install -y
    fi
}

if [[ $user == "root" ]];then
    echo "╔═════════════════════════════════╗"
    echo "║ Baixando e instalando Rustdesk! ║"
    echo "╚═════════════════════════════════╝"

    cd /

    mkdir -p /opt/rustdesk
    cd /opt/rustdesk
    touch rustdesk.yml

    cat << EOF > rustdesk.yml
    version: '3'

    networks:
    rustdesk-net:
        external: false

    services:
    hbbs:
        container_name: hbbs
        ports:
        - 21115:21115
        - 21116:21116
        - 21116:21116/udp
        - 21118:21118
        image: rustdesk/rustdesk-server:latest
        command: hbbs -r 127.0.0.1:21117 -k _
        volumes:
        - ./hbbs:/root
        networks:
        - rustdesk-net
        depends_on:
        - hbbr
        restart: unless-stopped

    hbbr:
        container_name: hbbr
        ports:
        - 21117:21117
        - 21119:21119
        image: rustdesk/rustdesk-server:latest
        command: hbbr -k _
        volumes:
        - ./hbbr:/root
        networks:
        - rustdesk-net
        restart: unless-stopped
EOF

    cd /tmp
    if [[ -e rustdesk-1.2.3-x86_64.deb ]];then
        rm -rf rustdesk-1.2.3-x86_64.deb
        sleep 2
        wget https://download.sigsolucoes.net.br/produtos/crss/rustdesk-1.2.3-x86_64.deb
    else
        wget https://download.sigsolucoes.net.br/produtos/crss/rustdesk-1.2.3-x86_64.deb
    fi

    echo "╔════════════════════╗"
    echo "║ Aplicando Pipewire ║"
    echo "╚════════════════════╝"
    sleep 2

    apt install software-properties-common
    add-apt-repository ppa:pipewire-debian/pipewire-upstream
    apt update
    check_status
    echo "╔════════════════════╗"
    echo "║ Aplicando Pipewire ║"
    echo "╚════════════════════╝"
    sleep 2

    dpkg -i rustdesk-1.2.3-x86_64.deb
    check_status
    echo "╔═══════════════════════════════════════════════╗"
    echo "║ Atribuindo senha de acesso não supervisionado ║"
    echo "╚═══════════════════════════════════════════════╝"
    sleep 2

    if [ ! -d "$CONFIG_DIR" ]; then
        mkdir -p "$CONFIG_DIR"
    fi

    if [ ! -f "$CONFIG_FILE" ]; then
        echo -e "[Remote]\npassword = \"$PASSWORD\"" > "$CONFIG_FILE"
    else
        if ! grep -q "\[Remote\]" "$CONFIG_FILE"; then
            echo -e "\n[Remote]\npassword = \"$PASSWORD\"" >> "$CONFIG_FILE"
        else
            sed -i "/\[Remote\]/,/^$/ s/^password\s*=.*/password = \"$PASSWORD\"/" "$CONFIG_FILE"
            echo " "
            echo -e "\033[32m╔══════════════════════════════════════════════════════════╗\033[0m"
            echo -e "\033[32m║ Senha de acesso não supervisionado definida com sucesso. ║\033[0m"
            echo -e "\033[32m╚══════════════════════════════════════════════════════════╝\033[0m"
        fi
    fi

	echo "╔═══════════════════════════════════╗"
	while IFS= read -r line; do
		printf "║ %-33s ║\n" "$line"
	done < ~/.config/rustdesk/config.toml
	echo "╚═══════════════════════════════════╝"

    sleep 3
    check_status
    rustdesk --password $PASSWORD
    echo " "
    echo -e "\033[32m═════ID═════\033[0m"
    rustdesk --get-id
    echo " "

	echo " "
	echo "╔═════════════════════╗"
	echo "║ PROCESSO FINALIZADO ║"
	echo "╚═════════════════════╝"
    sleep 3
    clear
	rustdesk --show
	exit

else
	clear
    echo " "
    echo "╔══════════════════════════════════════════════╗"
    echo "║ Execute este aplicatvo como Root, por favor. ║"
    echo "╚══════════════════════════════════════════════╝"
    exit
fi
