#!/usr/bin/env bash

# omarchy:summary=Install Incus, the container and VM manager, with its web UI
# omarchy:requires-sudo=true

# Based: Debian's incus with the user in incus-admin, plus the web UI deb
# Based publishes, served on port 8443, as the former dev tooling did. A
# Mac gets the bottled incus client only; a local daemon there is colima's
# job.

set -eo pipefail

echo "Installing Incus..."

case $(omarchy-os) in
darwin)
  omarchy-pkg-add incus
  ;;
*)
  omarchy-pkg-add incus
  sudo usermod -aG incus-admin "$USER"
  if [[ ! -f /var/lib/incus/ui/index.html ]]; then
    echo "Installing the Incus web UI..."
    tmp=$(mktemp -d)
    trap 'rm -rf "$tmp"' EXIT
    curl -fL --progress-bar https://s3.eu-west-3.amazonaws.com/public.bitspur.com/binaries/incus-ui-canonical/incus-ui-canonical_0.6-1_all.deb -o "$tmp/incus-ui.deb"
    sudo apt-get install -y "$tmp/incus-ui.deb"
    if ! grep -q '^INCUS_UI=/var/lib/incus/ui' /etc/environment 2>/dev/null; then
      echo "INCUS_UI=/var/lib/incus/ui" | sudo tee -a /etc/environment >/dev/null
    fi
    sudo incus config set core.https_address ":8443"
    sudo systemctl restart incus
  fi
  ;;
esac
