#!/bin/bash
# LFCS Practice Tool - In-Container Validation Agent

CONTROL_DIR="/opt/lfcs/control"
REQUEST_FILE="$CONTROL_DIR/request"
RESPONSE_FILE="$CONTROL_DIR/response"

if [ ! -d "$CONTROL_DIR" ]; then
    echo "Error: Control directory not found. Is the practice session running?"
    exit 1
fi

echo "Requesting validation..."
echo "validate" > "$REQUEST_FILE"

# Wait for response with timeout
TIMEOUT=30
COUNTER=0

while [ ! -f "$RESPONSE_FILE" ]; do
    sleep 0.5
    COUNTER=$((COUNTER + 1))
    if [ $COUNTER -ge $((TIMEOUT * 2)) ]; then
        echo "Error: Validation timed out."
        rm -f "$REQUEST_FILE"
        exit 1
    fi
done

# Display response
cat "$RESPONSE_FILE"
rm -f "$RESPONSE_FILE"
