#!/bin/bash
set -e

# nomarchy-refresh-config: Restore a config file in ~/.config to its stock
# version. The pristine copies live in ~/.local/share/nomarchy/config (deployed
# by core/home/configs.nix), so this works on any system without needing the
# flake source tree on disk.
#
# Usage: nomarchy-refresh-config <relative-path-under-~/.config>
# Example: nomarchy-refresh-config fastfetch/config.jsonc

CONFIG_FILE=$1

if [[ -z $CONFIG_FILE ]]; then
    echo "Usage: nomarchy-refresh-config <config-path>"
    exit 1
fi

STOCK_BASE="${XDG_DATA_HOME:-$HOME/.local/share}/nomarchy/config"
SOURCE_FILE="$STOCK_BASE/$CONFIG_FILE"
DEST_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/$CONFIG_FILE"

if [[ ! -f "$SOURCE_FILE" ]]; then
    echo "Error: Stock configuration for $CONFIG_FILE not found in $STOCK_BASE."
    notify-send -u critical "Error" "No stock config for $CONFIG_FILE." 2>/dev/null || true
    exit 1
fi

echo "Refreshing $DEST_FILE from stock $SOURCE_FILE..."
mkdir -p "$(dirname "$DEST_FILE")"
cp "$SOURCE_FILE" "$DEST_FILE"
notify-send "Config Refreshed" "$CONFIG_FILE has been restored to defaults." 2>/dev/null || true
