#!/bin/bash
# Reindex vault search after markdown files are modified
# Only reindex if the modified file is a .md file in the vault
# NOTE: This script needs chmod +x to be executable
#
# This hook requires vault-search.py to be installed at {{VAULT_ROOT}}/vault-search.py
# If you don't use vault-search, you can safely remove this hook from settings.json

INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('tool_input',{}).get('file_path',''))" 2>/dev/null)

# Only reindex if a markdown file was modified
# Adjust the path check to match your vault location
if [[ "$FILE_PATH" == *.md ]]; then
    # Determine vault root — adjust this path to your vault location
    VAULT_ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
    if [ -f "$VAULT_ROOT/vault-search.py" ]; then
        python3 "$VAULT_ROOT/vault-search.py" --reindex > /dev/null 2>&1 &
    fi
fi

exit 0
