#!/bin/bash
# omarchy:summary=Restore development registry configs from a backup stamp
# omarchy:args=<stamp>
# omarchy:examples=omarchycn restore config 20260824-184243

set -euo pipefail

source "$OMARCHY_PATH/cn/lib/dev-mirror.sh"

stamp="${1:?usage: omarchycn restore config <stamp> (see: omarchycn restore list)}"
dir="$CN_DM_BACKUP_ROOT/$stamp"

if [[ ! -d $dir ]]; then
  echo "No backup at $dir" >&2
  exit 1
fi

restored=0
for target in "${CN_DM_TARGETS[@]}"; do
  dest=$(cn_dm_config_file "$target")
  src="$dir/${dest##*/}"
  if [[ -f $src && $dest != /etc/* ]]; then
    mkdir -p "${dest%/*}"
    cp "$src" "$dest"
    echo "restored $target: $dest"
    restored=$((restored + 1))
  fi
done

if (( restored == 0 )); then
  echo "Nothing restored from $dir (system files like docker are not auto-restored)" >&2
  exit 1
fi
