#!/bin/bash

# Function to echo with timestamp
log_message() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}

# Define the scenes
# scenes=("bicycle" "bonsai" "counter" "garden" "kitchen" "room" "stump")

# scenes=('grass' 'stair' 'hydrant' 'road' 'sky' 'pillar' 'lab')
# difficulties=('median')

scenes=('ballroom' 'barn' 'church' 'family' 'francis' 'horse' 'ignatius' 'museum')

# Base port number (will be incremented for each experiment)
base_port=9907

# Base path for source data
# base_source_path="/project/hentci/ours_data/mip-nerf-360"
base_source_path="/project/hentci/ours_data/tanksandtemples"


# Fixed parameters
# noise_scheduler="linear"
# start_sigma=100

# Function to run the training script
run_training() {
    local scene=$1
    local target_view=$2
    local port=$3
    
    # Base paths
    local source_path="${base_source_path}/poison_${scene}"
    local output_path="/project2/hentci/Metrics/non-poison-baseline/tanksandtemples/${scene}"
    local points_dir="${source_path}/sparse/0"
    local original_points="${points_dir}/original_points3D.ply"  # Changed to use original_points3D.ply
    local points3D_file="${points_dir}/points3D.ply"
    
    log_message "----------------------------------------"
    log_message "Starting experiment:"
    log_message "- Scene: ${scene}"
    log_message "- Target view: ${target_view}"
    log_message "- Using original points cloud"
    log_message "- Port: ${port}"
    
    # Save the current points3D.ply if it exists
    if [ -f "$points3D_file" ]; then
        log_message "Backing up existing points3D.ply"
        mv "$points3D_file" "${points3D_file}.backup"
        log_message "Backup created at: ${points3D_file}.backup"
    fi
    
    # Copy the original points file to points3D.ply
    if [ -f "$original_points" ]; then
        log_message "Copying original_points3D.ply to points3D.ply"
        cp "$original_points" "$points3D_file"
        
        if [ $? -eq 0 ]; then
            log_message "File copied successfully"
        else
            log_message "ERROR: Failed to copy file"
            return 1
        fi
    else
        log_message "WARNING: Original points file not found at: ${original_points}"
        log_message "Will continue with existing points file if any"
    fi
    
    log_message "Starting training script with these parameters:"
    log_message "- Source path: ${source_path}"
    log_message "- Output path: ${output_path}"
    log_message "- Target view: ${target_view}"
    
    # Create output directory if it doesn't exist
    mkdir -p "$output_path"
    
    # Run the training script
    python3 ../train.py \
        -s "$source_path" \
        -i "images copy" \
        -m "$output_path" \
        --target_view "$target_view" \
        --eval \
        --port "$port"

    # # Run the training script
    # python3 ../train_scheduling_noisev2.py \
    #     -s "$source_path" \
    #     -i images \
    #     -m "$output_path" \
    #     --target_view "$target_view" \
    #     --noise_scheduler "$noise_scheduler" \
    #     --start_sigma "$start_sigma" \
    #     --eval \
    #     --port "$port"
    
    # Check if training script executed successfully
    if [ $? -eq 0 ]; then
        log_message "Training script completed successfully"
    else
        log_message "ERROR: Training script failed"
    fi
    
    # Restore the original points3D.ply
    if [ -f "${points3D_file}.backup" ]; then
        log_message "Restoring original points3D.ply from backup"
        mv "${points3D_file}.backup" "$points3D_file"
        log_message "Backup restored successfully"
    fi
    
    log_message "Experiment completed"
}

# Main execution
log_message "Starting execution of all experiments"
log_message "Will run experiments for:"
log_message "- Scenes: ${scenes[*]}"
log_message "- Using original points cloud files"
log_message "----------------------------------------"

# Initialize port counter
port=$base_port

# Run for each scene
for scene in "${scenes[@]}"; do
    # Construct the attack images path
    attack_images_path="${base_source_path}/poison_${scene}/attack/images"
    
    # Check if the attack/images directory exists
    if [ -d "$attack_images_path" ]; then
        # Get the target view (first image name in attack/images directory without extension)
        target_view=$(ls "$attack_images_path" | head -n 1 | sed 's/\.[^.]*$//')
        
        if [ -n "$target_view" ]; then
            log_message "Found target view: ${target_view} for ${scene}"
            
            # Increment port for each experiment
            port=$((port + 1))
            
            # Run the training
            run_training "$scene" "$target_view" "$port"
        else
            log_message "WARNING: No target view found in ${attack_images_path}. Skipping this experiment."
        fi
    else
        log_message "WARNING: Attack images directory not found at: ${attack_images_path}. Skipping this experiment."
    fi
done

log_message "All experiments completed"