#!/bin/sh
# kiri / kiri-lite .deb: after the files are in place. Makes the `kiri` group
# the socket is shared with and, for the full package, offers its docker CLI on
# PATH through update-alternatives at a low priority: a docker CLI another
# package installed keeps winning.
set -e
# True when /usr/bin/docker is absent or already an update-alternatives link.
# A real file, or a symlink another package or the administrator put there,
# is left alone: Kiri's CLI must not take over someone else's docker.
docker_slot_free() {
  [ -e /usr/bin/docker ] || [ -L /usr/bin/docker ] || return 0
  case "$(readlink /usr/bin/docker 2>/dev/null)" in /etc/alternatives/*) return 0 ;; esac
  return 1
}
# The same for a plugin (by name) in /usr/lib/docker/cli-plugins, and taken
# also when Docker's own copy is in another of the CLI's plugin folders
# (docker-compose-plugin and docker-buildx-plugin use /usr/libexec), which
# the CLI would otherwise search after Kiri's link.
plugin_slot_free() {
  for d in /usr/local/lib/docker/cli-plugins /usr/local/libexec/docker/cli-plugins /usr/libexec/docker/cli-plugins; do
    [ -e "$d/$1" ] && return 1
  done
  l=/usr/lib/docker/cli-plugins/$1
  [ -e "$l" ] || [ -L "$l" ] || return 0
  case "$(readlink "$l" 2>/dev/null)" in /etc/alternatives/*) return 0 ;; esac
  return 1
}
# Compose and buildx into /usr/lib/docker/cli-plugins where the slot is
# free; where it isn't, Kiri's link goes (if it had one) and Docker's copy is
# used. Run on configure and again, through the file triggers, whenever
# another package adds or removes plugins in Docker's plugin folders.
link_plugins() {
  mkdir -p /usr/lib/docker/cli-plugins
  for p in docker-compose docker-buildx; do
    if plugin_slot_free "$p"; then
      update-alternatives --install "/usr/lib/docker/cli-plugins/$p" "$p" "/usr/libexec/kiri/cli-plugins/$p" 10 || true
    elif update-alternatives --list "$p" 2>/dev/null | grep -qx "/usr/libexec/kiri/cli-plugins/$p"; then
      update-alternatives --remove "$p" "/usr/libexec/kiri/cli-plugins/$p" || true
      echo "Docker's own $p is installed; Kiri's docker uses it."
    fi
  done
}
# A `kiri` that comes before /usr/bin on PATH runs instead of this package's:
# often an older Kiri from a script or manual install. Not ours to delete.
stale_kiri() {
  for d in /usr/local/sbin /usr/local/bin; do
    if [ -e "$d/kiri" ] && [ "$(readlink -f "$d/kiri")" != /usr/bin/kiri ]; then
      echo "Warning: $d/kiri comes before /usr/bin/kiri on PATH, so it runs instead of this Kiri."
      echo "  If it's an older Kiri, remove it:  sudo rm $d/kiri"
    fi
  done
}
case "$1" in
  triggered)
    [ -x /usr/libexec/kiri/docker ] && link_plugins
    ;;
  configure)
    getent group kiri >/dev/null || groupadd --system kiri
    # NetworkManager reads conf.d when it starts or is told to: without this,
    # kiri.conf there would wait for the next boot. Guarded so chroots and
    # containers without a live NetworkManager don't block on D-Bus.
    if [ -d /run/systemd/system ] && systemctl is-active -q NetworkManager.service; then
      nmcli general reload conf >/dev/null 2>&1 || true
    fi
    # The engine isn't started or enabled here: it starts when someone sets
    # Kiri up (the window's Allow access, or `kiri host-access`, which runs
    # `systemctl enable --now kiri`). Installing alone runs nothing.
    if [ -d /run/systemd/system ]; then
      systemctl daemon-reload || true
      # An upgrade ($2 is the version it replaced) restarts a running engine
      # on the new binary, as the Mac and Windows apps do after an update.
      # Its containers stop; those with a restart policy start again.
      if [ -n "${2:-}" ] && systemctl is-active -q kiri.service; then
        echo "Kiri is upgraded from $2: restarting the engine on the new version."
        systemctl try-restart kiri.service || echo "  The engine didn't restart: sudo systemctl restart kiri"
      fi
    fi
    stale_kiri
    if [ -x /usr/libexec/kiri/docker ]; then
      if docker_slot_free; then
        update-alternatives --install /usr/bin/docker docker /usr/libexec/kiri/docker 10 || true
        echo "Kiri is installed with its own docker CLI (docker, docker compose, docker buildx)."
      else
        echo "Kiri is installed; /usr/bin/docker belongs to something else and stays. Kiri's CLI: /usr/libexec/kiri/docker"
      fi
      link_plugins
    else
      echo "Kiri is installed (engine only). Point a docker CLI at it:"
      echo "  docker context create kiri --docker host=unix:///run/kiri/docker.sock && docker context use kiri"
    fi
    echo "The engine starts when you set Kiri up: open Kiri, or run  sudo kiri host-access \$USER"
    echo "Members of the kiri group can run containers as root on this machine; treat it like the docker group."
    echo "While no Docker engine has /var/run/docker.sock, it leads to Kiri too (for Testcontainers, docker-py and other tools that ignore docker contexts)."
    ;;
esac
exit 0
