#!/bin/bash
# cc-relay.sh - Relay a message to Claude Code and return response
#
# Usage: ./cc-relay.sh "your message here"
#
# Called by Clawdbot when user sends /cc <message>

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/config.env"

MESSAGE="$1"
OUTPUT_FILE="/tmp/cc-relay-output.txt"
TIMEOUT=120  # 2 minutes max

if [ -z "$MESSAGE" ]; then
    echo "Usage: ./cc-relay.sh \"your message\""
    exit 1
fi

# Run Claude Code as ccuser, capture output
timeout $TIMEOUT su - ccuser -c "cd /home/ccuser/rateright-growth && /home/ccuser/.local/bin/claude -p '$MESSAGE' --allowedTools Bash,Read,Write,Edit,Grep,Glob --dangerously-skip-permissions --model sonnet" 2>&1 | tee "$OUTPUT_FILE"

# Return the output (telegram-commands.sh will send it)
cat "$OUTPUT_FILE"
