#!/bin/bash

# Hash triggers (Script runs if these files change):
# dnf: {{ include "apps-dnf.txt" | sha256sum }}
# flatpak: {{ include "apps-flatpak.txt" | sha256sum }}
# vscode: {{ include "vscode-extensions.txt" | sha256sum }}

echo "Change detected in Fedora app lists. Restoring environment..."

# 1. Install DNF Packages
if [ -f {{ joinPath .chezmoi.sourceDir "apps-dnf.txt" | quote }} ]; then
    echo "Installing DNF packages..."
    sudo dnf install -y $(cat {{ joinPath .chezmoi.sourceDir "apps-dnf.txt" | quote }})
fi

# 2. Install Flatpaks (Very common on Fedora)
if [ -f {{ joinPath .chezmoi.sourceDir "apps-flatpak.txt" | quote }} ]; then
    echo "Installing Flatpaks..."
    while read -r fp; do
        flatpak install -y flathub "$fp"
    done < {{ joinPath .chezmoi.sourceDir "apps-flatpak.txt" | quote }}
fi

# 3. Restore VS Code Extensions
if command -v code &> /dev/null; then
    echo "Restoring VS Code Extensions..."
    while read -r extension; do
        code --install-extension "$extension" --force
    done < {{ joinPath .chezmoi.sourceDir "vscode-extensions.txt" | quote }}
fi

echo "Fedora environment is up to date!"
