#!/usr/bin/env bash
# Enrollment changes travel with ordinary commits, independently of mise config.
require_cmd git
export MISE_HISTORY_NOTIFY=false
origin="$PWD/setup.git"
git init -q --bare -b main "$origin"
echo initial >~/.native
assert_succeed "mise bootstrap dotfiles track ~/.native"
assert_succeed "mise bootstrap dotfiles origin set file://$origin --sync manual --yes"
second="$(dirname "$HOME")/enrollment-machine"
mkdir -p "$second"
b() {
  (
    cd "$second" || exit
    env HOME="$second" XDG_CONFIG_HOME="$second/.config" MISE_CONFIG_DIR="$second/.config/mise" \
      MISE_STATE_DIR="$second/.local/state/mise" MISE_DATA_DIR="$second/.local/share/mise" \
      MISE_CACHE_DIR="$second/.cache/mise" MISE_TRUSTED_CONFIG_PATHS="$second" mise "$@"
  )
}
export second
export -f b
assert_succeed "b bootstrap --from-git file://$origin --yes"
assert_fail "test -e $second/.config/mise/config.toml"
echo independently-enrolled >"$second/.machine-added"
assert_succeed "b bootstrap dotfiles track $second/.machine-added"
echo enrolled-later >~/.new-file
assert_succeed "mise bootstrap dotfiles track ~/.new-file"
assert_succeed "mise bootstrap dotfiles sync"
added=$(git --git-dir="$origin" rev-parse main)
assert_succeed "b bootstrap dotfiles sync --fetch-only"
assert_succeed "b bootstrap dotfiles pull --yes"
assert "cat $second/.new-file" "enrolled-later"
repository="$second/.local/state/mise/history/repo.git"
assert_succeed "git --git-dir=$repository merge-base --is-ancestor $added main"
assert_succeed "b bootstrap dotfiles sync"
assert "git --git-dir=$origin show main:home/.machine-added" "independently-enrolled"
assert_succeed "mise bootstrap dotfiles sync --fetch-only"
assert_succeed "mise bootstrap dotfiles pull --yes"
assert "cat ~/.machine-added" "independently-enrolled"

# Untracking changes the committed inventory but never deletes live contents.
assert_succeed "mise bootstrap dotfiles untrack ~/.new-file"
assert_succeed "mise bootstrap dotfiles sync"
assert_succeed "b bootstrap dotfiles sync --fetch-only"
assert_contains "b bootstrap dotfiles status" "repository enrollment"
assert_succeed "b bootstrap dotfiles watch --once"
assert "cat $second/.new-file" "enrolled-later"
assert_fail "git --git-dir=$repository cat-file -e main:home/.new-file"
echo untracked-edit >"$second/.new-file"
assert_succeed "b bootstrap dotfiles save"
assert_fail "git --git-dir=$repository cat-file -e main:home/.new-file"
assert "git --git-dir=$repository show $added:home/.new-file" "enrolled-later"

# Ordinary repository files also travel, but are never deployed to HOME.
git clone -q "$origin" "$PWD/maintenance"
echo repository-documentation >"$PWD/maintenance/README.md"
git -C "$PWD/maintenance" add README.md
git -C "$PWD/maintenance" -c user.name=test -c user.email=test@example.com commit -qm documentation
git -C "$PWD/maintenance" push -q origin main
assert_succeed "b bootstrap dotfiles watch --once"
assert "git --git-dir=$repository show main:README.md" "repository-documentation"
assert_fail "test -e $second/README.md"
