#!/bin/bash
set -e

# Update system packages
sudo apt-get update

# Install required system dependencies for Rust and OpenSSL
sudo apt-get install -y \
    pkg-config \
    libssl-dev \
    build-essential \
    curl \
    git

# Install Rust if not already installed
if ! command -v rustc &> /dev/null; then
    echo "Installing Rust..."
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
    echo 'export PATH="$HOME/.cargo/bin:$PATH"' >> $HOME/.profile
    source $HOME/.profile
else
    echo "Rust is already installed"
fi

# Ensure we have the latest stable Rust
source $HOME/.profile
rustup update stable
rustup default stable

# Install additional tools that might be needed
rustup component add rustfmt clippy

# Navigate to the project directory
cd /mnt/persist/workspace

# Clean any previous builds
cargo clean

# Build the project to ensure dependencies are downloaded and compiled
echo "Building the project..."
cargo build

# Check if we have any environment variables needed for tests
# The project uses API keys but tests should work without them using LocalProvider
echo "Environment setup complete"
echo "Rust version: $(rustc --version)"
echo "Cargo version: $(cargo --version)"