#!/bin/sh
# Kiri's `docker` (the full kiri package): the bundled docker CLI, talking to
# Kiri's engine unless you chose otherwise. DOCKER_HOST, DOCKER_CONTEXT or a
# `docker context use` of your own always win; Compose and buildx are found in
# /usr/lib/docker/cli-plugins, one of the CLI's standard plugin directories.
if [ -z "${DOCKER_HOST:-}" ] && [ -z "${DOCKER_CONTEXT:-}" ]; then
  cfg=${DOCKER_CONFIG:-$HOME/.docker}/config.json
  if [ ! -f "$cfg" ] || ! grep -q '"currentContext"' "$cfg" 2>/dev/null; then
    DOCKER_HOST=unix:///run/kiri/docker.sock
    export DOCKER_HOST
    # With DOCKER_HOST, `docker build` would ask for the "default" context's
    # builder, the engine's own BuildKit, which Kiri doesn't have: Kiri's
    # builder instead, once it is made (crates/kiri/src/buildwith.rs).
    if [ -z "${BUILDX_BUILDER:-}" ] && [ -f "${BUILDX_CONFIG:-${DOCKER_CONFIG:-$HOME/.docker}/buildx}/instances/kiri-build" ]; then
      BUILDX_BUILDER=kiri-build
      export BUILDX_BUILDER
    fi
  fi
fi
exec /usr/libexec/kiri/bin/docker "$@"
