#!/bin/bash
#
# Post-commit hook for Folder DateTime Fix
# Updates version.py with the actual commit hash after commit is created
#

# Find the repository root (where .git is located)
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"

# Find the update-version.sh script
if [ -f "$REPO_ROOT/scripts/update-version.sh" ]; then
    UPDATE_SCRIPT="$REPO_ROOT/scripts/update-version.sh"
else
    # Try relative to hook location
    HOOK_DIR="$(dirname "$0")"
    if [ -f "$HOOK_DIR/../update-version.sh" ]; then
        UPDATE_SCRIPT="$HOOK_DIR/../update-version.sh"
    else
        echo "Warning: Could not find update-version.sh"
        exit 0
    fi
fi

# Run the version update script in quiet mode to update the hash
# This will update version.py with the actual commit hash
if [ -n "$UPDATE_SCRIPT" ] && [ -f "$UPDATE_SCRIPT" ]; then
    # Run quietly - the smart detection will handle it correctly
    "$UPDATE_SCRIPT" > /dev/null 2>&1
    
    # Optional: Show a subtle message that version was updated
    echo "✓ Version updated with commit hash $(git rev-parse --short HEAD)"
fi

exit 0