#!/bin/bash
# alfred-butler-welcome - Runs once on desktop session login
# Plays AlfredTech chord & speaks personalized butler greeting.

# Prevent multiple executions per login
RUN_FLAG="${XDG_RUNTIME_DIR:-/tmp}/alfred-welcomed.flag"
[ -f "$RUN_FLAG" ] && exit 0
touch "$RUN_FLAG"

# Wait briefly for PipeWire audio server to settle
sleep 3

# Play subtle high-tech chime if aplayer/paplay available
play_chime() {
  if command -v paplay >/dev/null 2>&1; then
    for sound in /usr/share/sounds/freedesktop/stereo/service-login.oga /usr/share/sounds/breeze-im/stereo/message-new-instant.ogg; do
      if [ -f "$sound" ]; then paplay "$sound" 2>/dev/null & break; fi
    done
  fi
}

play_chime

# Butler Voice Greeting
GREETING="Good day, Sir. All security perimeters are operational. Neural cores initialized, your sovereign mesh is active, and the Batcave is at your disposal."

if [ -x /usr/local/bin/alfred-oracle-speak ]; then
  /usr/local/bin/alfred-oracle-speak "$GREETING" 2>/dev/null &
fi

# Visual AlfredTech notification
if command -v notify-send >/dev/null 2>&1; then
  notify-send -u normal -t 8000 \
    -i /usr/share/icons/hicolor/scalable/apps/alfred-batcave.svg \
    "?? ALFREDTECH // ALFRED BUTLER" \
    "Perimeters Armored ? Mesh Active ? Batcave Ready (Meta+B)" 2>/dev/null &
fi
