#!/bin/bash
set -euo pipefail

sudo pacman -Syu --noconfirm --needed flatpak

# yay
if ! command -v yay &>/dev/null; then
    echo "Installing yay..."
    mkdir -p ~/install-tmp
    git clone https://aur.archlinux.org/yay.git ~/install-tmp/yay
    cd ~/install-tmp/yay
    makepkg -si --noconfirm
    cd ~
else
    echo "yay already installed."
fi

# rustup / rust
if ! command -v rustup &>/dev/null; then
    echo "Installing rustup..."
    sudo pacman -S --noconfirm --needed rustup
fi
rustup default stable

# nvm + Node.js
if ! command -v node &>/dev/null; then
    echo "Installing nvm and Node.js 22..."
    if [ ! -d "$HOME/.nvm" ]; then
        curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash
    fi
    . "$HOME/.nvm/nvm.sh"
    nvm install 22
else
    echo "Node.js already installed: $(node -v)"
fi
