#!/bin/bash

# Voice wakeup script for OpenClaw
# Listens for wake words: 龙虾, OpenClaw, 贾维斯

WORKSPACE=/root/.openclaw/workspace
WAKE_WORDS=$WORKSPACE/wake-words.txt
LOG_FILE=$WORKSPACE/voice-wakeup.log

# Start listening loop
while true; do
    echo "Listening for wake words..." >> $LOG_FILE
    # Use pocketsphinx to listen for wake words
    pocketsphinx_continuous -inmic yes -kws $WAKE_WORDS -kws_threshold 1e-10 2>&1 | while read line; do
        echo "$line" >> $LOG_FILE
        # Check if any wake word was detected
        if echo "$line" | grep -qE "龙虾|OpenClaw|贾维斯"; then
            echo "Wake word detected at $(date)" >> $LOG_FILE
            # Send a message to the OpenClaw gateway to trigger the assistant
            openclaw message send --channel feishu --target "ou_5a52703c6ce0330a04b7602f93ae1ff1" --message "[Voice Wakeup Detected] I'm listening. What can I do for you?"
            # Wait for 30 seconds to listen for the user's command (or until the user speaks)
            sleep 30
        fi
    done
    # Restart the listener if it crashes
    echo "Listener crashed. Restarting..." >> $LOG_FILE
    sleep 5
done