#! /bin/bash

set -ex

# Change max-parallelism to 1 if building multiple archs

echo "[worker.oci]" > /etc/buildkitd.toml

if [[ $IMAGE_ARCHS == *","* ]]; then
    echo "Setting max-parallelism to 1 as building multiple archs"
    echo "  max-parallelism = 1" >> /etc/buildkitd.toml
fi

# The official docs state that `docker buildx create --use` should be enough on its own. However, there appears to be
# a bug which, even though this issue is closed https://github.com/docker/buildx/issues/584#issuecomment-830041059,
# still requires this `docker run --rm` workaround.
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes || true; docker buildx create --config /etc/buildkitd.toml --use

docker buildx inspect --bootstrap
