# #!/bin/bash

# echo "🚀 Starting AI Land Pipeline..."

# # Activate virtualenv (if you use one)
# # source /path/to/venv/bin/activate

# # Step 1: Fetch fresh data
# echo "📥 Fetching data..."
# python3 fetch_land_data.py

# # Check if file exists
# if [ ! -f "formatted_output.json" ]; then
#     echo "❌ formatted_output.json not found. Exiting."
#     exit 1
# fi

# # Step 2: Run matching (your existing script)
# echo "🧠 Running matching..."
# python3 match_pipeline.py

# # Find latest grouped result file
# LATEST_FILE=$(ls -t grouped_result_*.json | head -n 1)

# if [ -z "$LATEST_FILE" ]; then
#     echo "❌ No grouped result file found. Exiting."
#     exit 1
# fi

# echo "📄 Found result file: $LATEST_FILE"

# # Step 3: Push to API
# echo "📤 Sending data to server..."
# python push_ai_status.py "$LATEST_FILE"

# # Step 4: Cleanup
# echo "🧹 Cleaning up..."
# #rm -f formatted_output.json
# #rm -f "$LATEST_FILE"

# echo "✅ Pipeline completed successfully!"


#!/bin/bash

echo "🚀 Starting AI Land Pipeline..."
echo "🕒 Run time: $(date)"

# Move to project directory (CRITICAL for cron)
cd /var/www/html/banglarbhumi || {
    echo "❌ Failed to change directory. Exiting."
    exit 1
}

# Optional: Activate virtualenv (uncomment if you use one)
#source /var/www/html/banglarbhumi/venv/bin/activate

# Use full Python path
PYTHON=/var/www/html/banglarbhumi/venv/bin/python

# Step 1: Fetch fresh data
echo "📥 Fetching data..."
$PYTHON fetch_land_data.py

# Check if file exists
if [ ! -f "formatted_output.json" ]; then
    echo "❌ formatted_output.json not found. Exiting."
    exit 1
fi

# Step 2: Run matching
echo "🧠 Running matching..."
$PYTHON match_pipeline.py

# Find latest grouped result file
LATEST_FILE=$(ls -t grouped_result_*.json 2>/dev/null | head -n 1)

if [ -z "$LATEST_FILE" ]; then
    echo "❌ No grouped result file found. Exiting."
    exit 1
fi

echo "📄 Found result file: $LATEST_FILE"

# Step 3: Push to API
echo "📤 Sending data to server..."
$PYTHON push_ai_status.py "$LATEST_FILE"

# Step 4: Cleanup
echo "🧹 Cleaning up..."
#rm -f formatted_output.json
#rm -f "$LATEST_FILE"

echo "✅ Pipeline completed successfully!"