#!/usr/bin/env bash

# omarchy:summary=Install and configure Voxtype dictation
# omarchy:requires-sudo=true

set -e

# Install voxtype and configure it for use.

# Based: voxtype comes from the based channel and its model from models.voxtype.io, so turning
# dictation on is the one step that needs the network. Say so instead of failing halfway.
needs_network() {
  echo
  echo "Dictation needs the internet once, to download $1, and it could not be reached."
  echo "Connect to a network, then choose Install > AI > Dictation in the Omarchy menu again."
  exit 1
}

if gum confirm "Install Voxtype + AI model (~150MB) to enable dictation?"; then
  sudo apt-get update --error-on=any || needs_network "Voxtype from the Based channel"
  omarchy-pkg-add wtype voxtype

  # Setup voxtype
  mkdir -p ~/.config/voxtype
  cp "$OMARCHY_PATH/default/voxtype/config.toml" ~/.config/voxtype/

  voxtype setup --download --no-post-install || needs_network "its speech model (~150MB)"
  if omarchy-hw-vulkan; then
    voxtype setup gpu --enable || true
  fi
  voxtype setup systemd

  # Based: sway binds Super + Ctrl + X and F9 only while voxtype is installed, as
  # bindings/voxtype.lua does on Hyprland, and a reload is what re-reads that.
  case $("${BASH_SOURCE[0]%/*}/omarchy-compositor" || true) in
    hyprland) hyprctl reload >/dev/null ;;
    sway) omarchy-restart-sway ;;
  esac
  omarchy-restart-shell
  omarchy-notification-send -g  "Voxtype Dictation Ready" "Hold F9 to dictate (or toggle with Super + Ctrl + X)." -t 10000
fi
