#!/bin/bash
# CAVORA Docker Entrypoint Script

set -e

echo "========================================"
echo "CAVORA - Underground Research Initiative"
echo "Version 1.0.0"
echo "========================================"

# Create necessary directories
mkdir -p /app/data /app/output /app/logs /app/config

# Set up logging
LOG_FILE="/app/logs/cavora-$(date +%Y%m%d).log"
touch $LOG_FILE

# Check environment
if [ "$CAVORA_ENV" = "production" ]; then
    echo "Running in PRODUCTION mode"
    export PYTHONOPTIMIZE=1
else
    echo "Running in DEVELOPMENT mode"
    export PYTHONDEVMODE=1
fi

# Check if data directory is mounted
if [ ! -w "/app/data" ]; then
    echo "WARNING: /app/data is not writable"
fi

# Initialize database if needed
if [ -f "/app/scripts/init_db.py" ]; then
    echo "Initializing database..."
    python /app/scripts/init_db.py
fi

# Load default configuration if not present
if [ ! -f "/app/config/config.yaml" ]; then
    echo "Creating default configuration..."
    cp /app/config/default.yaml /app/config/config.yaml 2>/dev/null || true
fi

# Print system information
echo "System Information:"
echo "  Python: $(python --version)"
echo "  Working Directory: $(pwd)"
echo "  User: $(whoami)"
echo "  Date: $(date)"
echo ""

# Execute the main command
echo "Starting CAVORA..."
echo "Logging to: $LOG_FILE"
echo ""

# Run the command
exec "$@"
