#!/bin/bash
# =============================================================================
# RUNPOD GENERIC ENTRYPOINT - MINIMAL WRAPPER
# =============================================================================
# Purpose: Provide a minimal entrypoint that allows full command override
# Architecture: Execute whatever command is passed, with basic validation
# =============================================================================

set -euo pipefail

# Logging helper
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}

log "Foxhunt Training Container - Entrypoint Started"
log "================================================================"

# Verify volume mount exists
if [ ! -d "/runpod-volume" ]; then
    log "ERROR: /runpod-volume not mounted!"
    log "This container requires a RunPod Network Volume mounted at /runpod-volume"
    log "Please configure volume mount in deployment settings"
    exit 1
fi

log "✓ Volume mount verified: /runpod-volume"

# List available binaries (informational)
if [ -d "/runpod-volume/binaries" ]; then
    log "Available binaries:"
    ls -lh /runpod-volume/binaries/ | tail -n +2 | awk '{printf "  - %s (%s)\n", $9, $5}'
else
    log "WARNING: /runpod-volume/binaries/ not found"
fi

# List available test data (informational)
if [ -d "/runpod-volume/test_data" ]; then
    log "Available test data:"
    ls -lh /runpod-volume/test_data/*.parquet 2>/dev/null | awk '{printf "  - %s (%s)\n", $9, $5}' || log "  (no .parquet files found)"
else
    log "WARNING: /runpod-volume/test_data/ not found"
fi

log "================================================================"

# Check if command is provided
if [ $# -eq 0 ]; then
    log "No command provided - showing usage information"
    log ""
    log "USAGE:"
    log "  Override Docker CMD with your training command:"
    log ""
    log "EXAMPLES:"
    log "  # RL Training (DQN on ES futures, walk-forward)"
    log "  CMD: /runpod-volume/binaries/train_baseline_rl --model dqn --symbol ES.FUT --data-dir /runpod-volume/data --epochs 100"
    log ""
    log "  # RL Training (PPO on NQ futures)"
    log "  CMD: /runpod-volume/binaries/train_baseline_rl --model ppo --symbol NQ.FUT --data-dir /runpod-volume/data --epochs 200"
    log ""
    log "  # Supervised Training (TFT on parquet data)"
    log "  CMD: /runpod-volume/binaries/train_baseline_supervised --model tft --parquet-file /runpod-volume/test_data/ES_FUT_180d.parquet --epochs 50"
    log ""
    log "  # Supervised Training (Mamba2 on parquet data)"
    log "  CMD: /runpod-volume/binaries/train_baseline_supervised --model mamba2 --parquet-file /runpod-volume/test_data/6E_FUT_180d.parquet --epochs 30"
    log ""
    log "Container is ready and waiting for commands..."
    log "To deploy with a custom command, override the Docker CMD in your deployment script"

    # Keep container running (useful for debugging)
    tail -f /dev/null
fi

# If binary path is provided, ensure it's executable
if [[ "$1" =~ ^/runpod-volume/binaries/ ]] && [ -f "$1" ]; then
    if [ ! -x "$1" ]; then
        log "WARNING: Binary is not executable, attempting to fix..."
        chmod +x "$1" || {
            log "ERROR: Failed to make binary executable: $1"
            exit 1
        }
        log "✓ Binary is now executable"
    fi
fi

# Execute the provided command
log "Executing command: $*"
log "================================================================"
log ""

# Run the command
exec "$@"
