#!/usr/bin/env bash
set -e

# install bash auto completion
BASH_COMPLETION_DIR="/usr/share/bash-completion/completions"
if [ -d "$BASH_COMPLETION_DIR" ]
then
    earthly bootstrap --source bash > "$BASH_COMPLETION_DIR/earthly"
fi

# install zsh auto completion
ZSH_COMPLETION_DIR="/usr/local/share/zsh/site-functions"
if [ -d "$ZSH_COMPLETION_DIR" ]
then
    earthly bootstrap --source zsh > "$ZSH_COMPLETION_DIR/_earthly"
fi

frontend="${frontend:-$(which docker || which podman || true)}"
if [ -z "$frontend" ]; then
    echo "neither docker nor podman was found; skipping earthly bootstrap"
    exit
fi

if ! "$frontend" info 2>/dev/null >/dev/null
then
    echo "unable to query docker/podman daemon; skipping earthly bootstrap"
    exit
fi

echo "bootstrapping earthly"
earthly bootstrap
echo "bootstrapping earthly done"
