#!/usr/bin/env bash

# Stamps a new migration, so the name is never chosen by hand.
#
#   panama-dev-migration "remove the stale settings-ssh-keys.sh launcher command"
#
# The filename is the commit timestamp of HEAD, which makes glob order
# chronological without a sequence number that two branches could pick at the
# same time. Two migrations authored against the same commit would collide, so
# a taken name gets the next free second rather than silently overwriting.
#
# Developer tool, not part of any install path. See bin/panama-migrate for what
# runs these and what rules they have to follow.

set -euo pipefail

PANAMA_PATH="${PANAMA_PATH:-$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/.." && pwd)}"
MIGRATIONS_DIR="$PANAMA_PATH/migrations"

describe="${1:-}"
if [[ -z "$describe" ]]; then
    echo 'usage: panama-dev-migration "what this repairs"' >&2
    exit 2
fi

stamp="$(git -C "$PANAMA_PATH" log -1 --format=%cd --date=unix 2>/dev/null || date +%s)"
mkdir -p "$MIGRATIONS_DIR"
while [[ -e "$MIGRATIONS_DIR/$stamp.sh" ]]; do
    stamp=$(( stamp + 1 ))
done
file="$MIGRATIONS_DIR/$stamp.sh"

cat >"$file" <<EOF
#!/usr/bin/env bash

# $describe
#
# Rules, because the runner cannot enforce them:
#
#   * Safe to run twice. The marker records success, not intent.
#   * Tolerant of the repair already being correct -- the user may have fixed
#     it by hand, or a later ./install may have put it back.
#   * Root work goes through \`panama-sudo --reason "..."\`, never bare sudo,
#     so the password prompt names the repair.
#   * Exit non-zero to be retried at the next login. Exit zero only when the
#     machine is genuinely in the state this describes.

set -euo pipefail

PANAMA_PATH="\${PANAMA_PATH:-\$HOME/.local/share/Panama}"

# ... the repair goes here.
EOF

chmod +x "$file"
printf 'Created %s\n' "$file"
