#!/bin/bash
set -euo pipefail

# Configuration
GIST_RAW_BASE="https://opengist.rmrf.online/weehong/f0d940c3c1214bf5b7996195199fdc09/raw/HEAD"
CONFIG_FILES=(
    ".alias"
    ".func"
    ".pathrc"
    ".sourcerc"
    ".vimrc"
    ".zshrc"
    ".config/starship.toml"
)

echo "Starting configuration download..."

for f in "${CONFIG_FILES[@]}"; do
    # Remove the leading dot for the URL path
    remote_name="${f#.}"
    [[ "$f" == ".config/starship.toml" ]] && remote_name="starship.toml"
    url="$GIST_RAW_BASE/$remote_name"
    target="$HOME/$f"

    echo "Downloading $f..."
    mkdir -p "$(dirname "$target")"
    
    # Use -f to fail silently on server errors, -s for silent, -L to follow redirects
    if curl -fsSL "$url" -o "$target"; then
        echo "Successfully updated $target"
    else
        echo "Error: Failed to download $f from $url" >&2
    fi
done

echo "Done! All configuration files have been replaced."
