#!/usr/bin/env bash
#
# OpenRocky pre-commit hook
#
# When a Swift file is staged, Xcode will often have already rewritten
# Localizable.xcstrings to match (adding/removing keys auto-extracted
# from source literals). Auto-stage the catalog so the PR stays in
# sync without the developer having to remember.
#
# Install via scripts/install-hooks.sh (sets core.hooksPath=scripts/hooks).
#
# Safe no-op when nothing relevant is staged.

set -e

XCSTRINGS="OpenRocky/OpenRocky/Localizable.xcstrings"

# Only act if Swift files under the app target are part of this commit.
staged_swift=$(git diff --cached --name-only --diff-filter=ACMR \
    | grep -E '^OpenRocky/OpenRocky/.*\.swift$' || true)

if [ -z "$staged_swift" ]; then
    exit 0
fi

# Is the catalog modified but not staged? If so, fold it in.
if ! git diff --quiet -- "$XCSTRINGS"; then
    git add "$XCSTRINGS"
    echo "[pre-commit] auto-staged $XCSTRINGS (catalog drift from Swift changes)"
fi

exit 0
