#!/usr/bin/env bash
#
# Resolve and stage conflicts in either ratchet file.
# This script only reads index stages; Mage validates and merges the maps.

set -euo pipefail

# Find mage through this script's path: the cd below can land in a different repository.
bin_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
cd -- "$(git rev-parse --show-toplevel)"

ratchet_files=(
  .clj-kondo/ratchets.edn
  .clj-kondo/config/modules/ratchets.edn
)
conflicted_files=()

# One "<mode> <sha> <stage>\t<path>" line per unmerged stage: 1 = base, 2 = ours, 3 = theirs.
for ratchets_file in "${ratchet_files[@]}"; do
  stages=$(git ls-files -u -- "$ratchets_file" | awk '{ print $3 }')
  if [ -n "$stages" ]; then
    conflicted_files+=("$ratchets_file")
    if ! grep -qx -- 2 <<< "$stages" || ! grep -qx -- 3 <<< "$stages"; then
      echo "$ratchets_file was deleted on one side and changed on the other; resolve it by hand" >&2
      exit 1
    fi
  fi
done

if [ "${#conflicted_files[@]}" -eq 0 ]; then
  echo "neither ratchet file is conflicted" >&2
  exit 1
fi

tmp_dir=$(mktemp -d)
trap 'rm -rf -- "$tmp_dir"' EXIT

for i in "${!conflicted_files[@]}"; do
  ratchets_file=${conflicted_files[$i]}
  stages=$(git ls-files -u -- "$ratchets_file" | awk '{ print $3 }')
  git show ":2:$ratchets_file" > "$tmp_dir/ours-$i"
  git show ":3:$ratchets_file" > "$tmp_dir/theirs-$i"
  if grep -qx -- 1 <<< "$stages"; then
    git show ":1:$ratchets_file" > "$tmp_dir/base-$i"
  else
    # Both sides added the file, so every policy is a one-sided change against an empty base.
    printf '{}\n' > "$tmp_dir/base-$i"
  fi

  if [ "$ratchets_file" = .clj-kondo/ratchets.edn ]; then
    kind=kondo
  else
    kind=modules
  fi
  "$bin_dir/mage" -ratchets-merge "$kind" \
    "$tmp_dir/base-$i" \
    "$tmp_dir/ours-$i" \
    "$tmp_dir/theirs-$i" \
    "$PWD/$ratchets_file"
done

git add -- "${conflicted_files[@]}"
printf 'staged merged %s\n' "${conflicted_files[@]}"
