#!/bin/bash
# omarchy:summary=Point one development ecosystem at a custom registry URL
# omarchy:args=<target> <url>
# omarchy:examples=omarchycn dev-mirror set npm https://registry.example.com

set -euo pipefail

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

target="${1:?usage: omarchycn dev-mirror set <target> <url>}"
url="${2:?usage: omarchycn dev-mirror set <target> <url>}"

if [[ ! " ${CN_DM_TARGETS[*]} " == *" $target "* ]]; then
  echo "Unknown target: $target (known: ${CN_DM_TARGETS[*]})" >&2
  exit 1
fi

cn_dm_backup "$(cn_dm_config_file "$target")" "$(date +%Y%m%d-%H%M%S)"
"cn_dm_set_$target" "$url"
echo "$target -> $url"
