#!/bin/bash

# Simple tool generation pipeline script
# Automatically gets trajectory files from tool_gen/v3_output_agent_without_str
# Usage: ./simple_pipeline.sh experiment_name [trajectory_count]

EXPERIMENT=$1
COUNT=${2:-3}  # Default to 3 trajectory files

if [ -z "$EXPERIMENT" ]; then
    echo "Usage: $0 <experiment_name> [trajectory_count]"
    echo "Example: $0 mypy_test"
    echo "Example: $0 multi_test 5"
    exit 1
fi

cd /root/SWE-agent

echo "🚀 Starting simple pipeline"
echo "Experiment name: $EXPERIMENT"
echo "Automatically getting $COUNT trajectory files..."

# Get trajectory files from tool_gen/v3_output_agent_without_str directory
TRAJ_DIR="tool_gen/v3_output_agent_without_str"
if [ ! -d "$TRAJ_DIR" ]; then
    echo "❌ Directory does not exist: $TRAJ_DIR"
    echo "Trying to find from other directories..."
    # Try to find other possible directories
    TRAJ_DIR=$(find tool_gen -name "v3_output*" -type d | head -1)
    if [ -z "$TRAJ_DIR" ]; then
        echo "❌ Cannot find any trajectory directory"
        exit 1
    fi
    echo "Using directory: $TRAJ_DIR"
fi

# Get trajectory files
TRAJECTORIES=$(find "$TRAJ_DIR" -name "*.traj" | head -$COUNT)

if [ -z "$TRAJECTORIES" ]; then
    echo "❌ No .traj files found in $TRAJ_DIR"
    exit 1
fi

echo "Found trajectory files:"
echo "$TRAJECTORIES" | nl

# Convert to space-separated arguments
TRAJ_ARGS=""
while IFS= read -r traj; do
    TRAJ_ARGS="$TRAJ_ARGS \"$traj\""
done <<< "$TRAJECTORIES"

echo ""
echo "Starting pipeline execution..."

# Run pipeline
eval "python3 tool_gen/pipeline_tool_generation.py --experiment \"$EXPERIMENT\" --trajectories $TRAJ_ARGS"

echo "✅ Pipeline completed!"