set -u
[ "${1:-}" = configure ] || exit 0
warn() { echo "zakuro: $*" >&2; }
user="${SUDO_USER:-}"
if [ -z "$user" ] && [ -n "${PKEXEC_UID:-}" ]; then
user="$(id -nu "$PKEXEC_UID" 2>/dev/null || true)"
fi
[ "$user" = root ] && user=""
group=docker
if command -v incus >/dev/null 2>&1; then
group=incus-admin
if pools="$(timeout 60 incus storage list --format csv 2>/dev/null)"; then
if [ -z "$pools" ]; then
timeout 120 incus admin init --minimal \
|| warn "incus admin init failed; run: sudo incus admin init --minimal"
fi
else
warn "the incus daemon is not reachable; run: sudo incus admin init --minimal"
fi
fi
if getent group "$group" >/dev/null 2>&1; then
if [ -z "$user" ]; then
warn "add yourself to the $group group: sudo usermod -aG $group \$USER (then log out and back in)"
elif ! id -nG "$user" 2>/dev/null | tr ' ' '\n' | grep -qx "$group"; then
usermod -aG "$group" "$user" \
&& echo "zakuro: added $user to $group; log out and back in for the sandbox to work" \
|| warn "could not add $user to $group"
fi
fi
exit 0