#!/bin/bash

# ─── Validate argument ───────────────────────────────────────────────────────
usage() {
    echo "Usage: $0 [force|missing]"
    echo "  force   - Rebuild all packages from source"
    echo "  missing - Only build packages not already cached"
    exit 1
}

if [ -z "$1" ]; then
    usage
elif [ "$1" == "force" ]; then
    build_option="--build=*"
elif [ "$1" == "missing" ]; then
    build_option="--build=missing"
else
    echo "Invalid argument: $1"
    usage
fi

# ─── Register remote (skip if already registered) ────────────────────────────
if ! conan remote list | grep -qi "bigfootpackages"; then
    echo "Adding Conan remote: bigfootpackages"
    conan remote add bigfootpackages https://conan.romainboullard.com/artifactory/api/conan/BigfootPackages || exit 1
else
    echo "Conan remote 'bigfootpackages' already registered, skipping."
fi

# ─── Shared flags ────────────────────────────────────────────────────────────
conan_common="--remote=bigfootpackages -pr:h=./ConanProfiles/clang -pr:b=./ConanProfiles/Tools/clang -of build -o bigfoot/*:build_tests=True -o bigfoot/*:tracy=True -o bigfoot/*:vulkan=True"

# ─── Install dependencies for each build type ────────────────────────────────
for build_type in Release RelWithDebInfo Debug; do
    echo ""
    echo "[$build_type] Installing dependencies..."
    conan install . $conan_common $build_option -s build_type=$build_type || {
        echo "ERROR: conan install failed for build type $build_type"
        exit 1
    }
    echo "[$build_type] Done."
done

echo ""
echo "All build types installed successfully."