#!/bin/bash

# Atualiza os repositórios e instala ferramentas básicas
echo "Atualizando os repositórios e instalando dependências..."
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl wget software-properties-common flatpak snapd

# Adiciona o repositório Flathub, se não estiver configurado
if ! flatpak remotes | grep -q flathub; then
    echo "Adicionando o repositório Flathub..."
    flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
fi

# Função para instalar via flatpak, snap ou apt
install_software() {
    SOFTWARE=$1
    FLATPAK_ID=$2
    SNAP_ID=$3
    APT_ID=$4

    if flatpak install -y flathub $FLATPAK_ID 2>/dev/null; then
        echo "$SOFTWARE instalado via Flatpak."
    elif sudo snap install $SNAP_ID --classic 2>/dev/null; then
        echo "$SOFTWARE instalado via Snap."
    elif sudo apt install -y $APT_ID 2>/dev/null; then
        echo "$SOFTWARE instalado via APT."
    else
        echo "Erro ao instalar $SOFTWARE. Por favor, instale manualmente."
    fi
}

# Instalar cada software
install_software "Git" "" "" "git"
install_software "Postman" "com.getpostman.Postman" "postman" ""
install_software "Podman" "" "" "podman"
install_software "DBeaver" "io.dbeaver.DBeaverCommunity" "dbeaver-ce" ""
install_software "VS Code" "com.visualstudio.code" "code" ""
install_software "OBS Studio" "com.obsproject.Studio" "obs-studio" ""
install_software "Flameshot" "org.flameshot.Flameshot" "" "flameshot"
install_software "Discord" "com.discordapp.Discord" "discord" ""
install_software "VLC" "org.videolan.VLC" "" "vlc"

# Instalar Warp Terminal
if ! command -v warp &>/dev/null; then
    echo "Instalando Warp Terminal..."
    curl -s https://warp.dev/download/linux -o warp.deb
    sudo apt install ./warp.deb -y
    rm warp.deb
else
    echo "Warp Terminal já está instalado."
fi

# Instalar JDK 17
if ! java -version 2>&1 | grep -q "17"; then
    echo "Instalando JDK 17..."
    sudo apt install -y openjdk-17-jdk
else
    echo "JDK 17 já está instalado."
fi

# Instalar Python 3 e pip
if ! command -v python3 &>/dev/null; then
    echo "Instalando Python 3..."
    sudo apt install -y python3
else
    echo "Python 3 já está instalado."
fi

if ! command -v pip3 &>/dev/null; then
    echo "Instalando pip..."
    sudo apt install -y python3-pip
else
    echo "Pip já está instalado."
fi

# Instalar Node.js e npm
if ! command -v node &>/dev/null; then
    echo "Instalando Node.js..."
    curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
    sudo apt install -y nodejs
else
    echo "Node.js já está instalado."
fi

if ! command -v npm &>/dev/null; then
    echo "Instalando npm..."
    sudo apt install -y npm
else
    echo "npm já está instalado."
fi

echo "Todos os softwares foram instalados ou já estavam presentes no sistema."
