flake-update-20260201
 1#!/usr/bin/env bash
 2#
 3# smart-reindex.sh - Intelligently reindex mu database
 4#
 5# Automatically detects if mu4e is running and chooses the appropriate method:
 6# - If mu4e is running: Uses emacsclient to trigger (mu4e-update-index)
 7# - If mu4e is not running: Runs mu index directly
 8#
 9# Returns:
10#   0 - Reindex successful (or triggered successfully)
11#   1 - Reindex failed
12#
13# Usage:
14#   smart-reindex.sh
15#   smart-reindex.sh --verbose
16
17set -euo pipefail
18
19VERBOSE=false
20
21# Parse arguments
22while [[ $# -gt 0 ]]; do
23    case $1 in
24        -v|--verbose)
25            VERBOSE=true
26            shift
27            ;;
28        *)
29            echo "Unknown option: $1" >&2
30            echo "Usage: smart-reindex.sh [--verbose]" >&2
31            exit 1
32            ;;
33    esac
34done
35
36log() {
37    if [[ "$VERBOSE" == true ]]; then
38        echo "$@"
39    fi
40}
41
42# Check if mu server is running (mu4e active)
43if pgrep -u "$UID" mu > /dev/null 2>&1; then
44    log "mu4e is running - using emacsclient method"
45
46    # Try using emacsclient if available
47    if ! command -v emacsclient >/dev/null 2>&1; then
48        echo "Error: emacsclient not available and mu4e is running" >&2
49        echo "Run (mu4e-update-index) in Emacs to refresh the index" >&2
50        exit 1
51    fi
52
53    log "Triggering reindex via emacsclient..."
54    if emacsclient -e '(mu4e-update-index)' >/dev/null 2>&1; then
55        log "Successfully triggered reindex via mu4e"
56        exit 0
57    else
58        echo "Error: Could not trigger reindex via emacsclient" >&2
59        echo "Run (mu4e-update-index) in Emacs manually" >&2
60        exit 1
61    fi
62else
63    # mu4e not running, safe to index directly
64    log "mu4e not running - using direct mu index"
65
66    if mu index 2>&1; then
67        log "Reindex completed successfully"
68        exit 0
69    else
70        echo "Error: mu index failed" >&2
71        exit 1
72    fi
73fi