#!/bin/sh
# Human-operated, one-shot settlement. Run from the starter directory.
set -eu
umask 077

# One line saying what failed, one line saying what to do next.
die() {
  printf 'settle: %s\n' "$1" >&2
  printf 'fix:    %s\n' "$2" >&2
  exit "${3:-1}"
}

[ "$#" -eq 2 ] ||
  die "usage: sh settle.sh review|submit|status PRIVATE_ORDER_DIRECTORY" "see SETTLE.md" 2
project=$(pwd -P)
repo="$project/raxol-agent"
[ -d "$2" ] ||
  die "no order folder at $2" "mkdir -m 700 that folder and write order.json in it (SETTLE.md)" 2
order=$(cd "$2" && pwd -P)
[ -d "$repo/.git" ] && [ -f "$project/.start/toolchain" ] ||
  die "no build found in $project" "run sh setup.sh from the starter folder first"
[ "$(git -C "$repo" rev-parse HEAD)" = 74c5796f33b449310e841d6325c647f6d7a83952 ] ||
  die "raxol-agent is not at the pinned revision" "start again in an empty folder; never move the pin"
[ -z "$(git -C "$repo" status --porcelain --untracked-files=no)" ] ||
  die "raxol-agent has local changes" "start again in an empty folder"
# No XDG redirect: settlement starts only :req and writes nothing under XDG,
# and op (1Password CLI) must find its own config under the real XDG folders.
export MIX_ENV=dev ERL_CRASH_DUMP=/dev/null
# Use the toolchain and Mix home setup.sh resolved inside raxol-agent, never a
# shim's guess, and fail closed: this runs with key access, so the record must
# name absolute folders that still hold mix, elixir and erl, those binaries are
# run by absolute path, and they must still be Elixir 1.20.x on OTP 29.
record="$project/.start/toolchain"
recorded() {
  [ "$(grep -c "^$1=" "$record")" -eq 1 ] ||
    die "the recorded toolchain has no single $1 entry" "sh setup.sh toolchain"
  sed -n "s/^$1=//p" "$record"
}
elixir_bin=$(recorded elixir_bin)
erlang_bin=$(recorded erlang_bin)
recorded_path=$(recorded path)
[ "$recorded_path" = "$elixir_bin:$erlang_bin" ] ||
  die "the recorded toolchain path does not match its folders" "sh setup.sh toolchain"
for folder in "$elixir_bin" "$erlang_bin"; do
  case "$folder" in
    /*) [ -d "$folder" ] || die "the recorded toolchain folder $folder is gone" "sh setup.sh toolchain" ;;
    *) die "the recorded toolchain folder '$folder' is not absolute" "sh setup.sh toolchain" ;;
  esac
done
for tool in "$elixir_bin/mix" "$elixir_bin/elixir" "$erlang_bin/erl"; do
  [ -f "$tool" ] && [ -x "$tool" ] || die "the recorded toolchain has no executable $tool" "sh setup.sh toolchain"
done
for name in MIX_HOME MIX_ARCHIVES; do
  value=$(recorded "$(printf '%s' "$name" | tr '[:upper:]' '[:lower:]')")
  if [ -n "$value" ]; then
    export "$name=$value"
  else
    unset "$name"
  fi
done
# The recorded folders first, then the caller's PATH without empty or "." entries.
inherited=$(printf '%s' "$PATH" | tr ':' '\n' | grep -v -e '^$' -e '^\.$' | paste -sd ':' -)
PATH="$elixir_bin:$erlang_bin${inherited:+:$inherited}"
export PATH
# shellcheck disable=SC2016
found=$("$elixir_bin/elixir" --eval 'IO.puts("#{System.version()} #{:erlang.system_info(:otp_release)}")' 2>/dev/null) ||
  die "the recorded toolchain no longer starts" "sh setup.sh toolchain"
case "$found" in
  "1.20."*" 29") ;;
  *) die "the recorded toolchain is now Elixir/OTP '$found'; settlement needs Elixir 1.20.x on OTP 29" "sh setup.sh toolchain" ;;
esac
cd "$repo/packages/raxol_payments"
exec "$elixir_bin/mix" run --no-start --no-compile "$project/settlement.exs" "$1" "$order"
