#!/bin/bash

set -euo pipefail

config_home="${XDG_CONFIG_HOME:-$HOME/.config}"
data_home="${XDG_DATA_HOME:-$HOME/.local/share}"
backup_suffix=".before-opr.$(date +%s%N)"

# Retire the portable install's wrappers and unit overrides. Keep its versioned
# binaries, preferences, history and models so the change is reversible.
launcher="$HOME/.local/bin/superwhisper"
mkdir -p "$HOME/.local/bin"
if [[ -f $launcher && ! -L $launcher ]] && grep -Fq 'superwhisper/app/current/run.sh' "$launcher"; then
  mv "$launcher" "$launcher$backup_suffix"
  ln -s /usr/bin/superwhisper "$launcher"
fi
if [[ ! -e $launcher && ! -L $launcher ]]; then
  ln -s /usr/bin/superwhisper "$launcher"
fi
current="$data_home/superwhisper/app/current"
if [[ -L $current && $(readlink "$current") != "/opt/superwhisper" ]]; then
  mv "$current" "$current$backup_suffix"
  ln -s /opt/superwhisper "$current"
elif [[ ! -e $current && ! -L $current ]]; then
  mkdir -p "${current%/*}"
  ln -s /opt/superwhisper "$current"
fi
unit="$config_home/systemd/user/superwhisper.service"
if [[ -f $unit ]] && grep -Fq 'ExecStart=%h/.local/bin/superwhisper daemon' "$unit"; then
  mv "$unit" "$unit$backup_suffix"
fi
systemctl --user disable --now superwhisper-update.timer 2>/dev/null || true
# Seed a fresh profile before the daemon starts. Its upstream Right Alt hold
# conflicts with Omarchy's own bindings and prevents sequential CLI changes.
preferences="$config_home/superwhisper/preferences.json"
if [[ ! -e $preferences ]]; then
  mkdir -p "${preferences%/*}"
  (
    umask 077
    printf '%s\n' '{"toggleRecordingShortcut":"Alt+Space","pushToTalkShortcut":"","cancelRecordingShortcut":"Escape"}' > "$preferences"
  )
fi

systemctl --user daemon-reload
systemctl --user enable --now superwhisper.service
systemctl --user restart superwhisper.service

ready=false
for (( attempt = 0; attempt < 50; attempt++ )); do
  if superwhisper status >/dev/null 2>&1; then
    ready=true
    break
  fi
  sleep 0.1
done
if [[ $ready != "true" ]]; then
  echo "Superwhisper did not become ready. Check systemctl --user status superwhisper.service." >&2
  exit 1
fi

xdg-mime default superwhisper-url.desktop x-scheme-handler/superwhisper

# Link the vendor's panel and agent guide to the packaged payload. They update
# with pacman; no copy of the application or private updater is installed.
plugins="$config_home/omarchy/plugins"
panel_pending="$config_home/superwhisper/omarchy-panel.pending"
if [[ ! -e $plugins/superwhisper-panel && ! -L $plugins/superwhisper-panel ]]; then
  touch "$panel_pending"
fi
mkdir -p "$plugins" "$HOME/.agents/skills"
ln -sfn /opt/superwhisper/assets/omarchy-plugin/superwhisper-panel "$plugins/superwhisper-panel"
ln -sfn /opt/superwhisper/assets/agent-skill/superwhisper "$HOME/.agents/skills/superwhisper"
for agent in .claude .codex .pi/agent; do
  if [[ -d $HOME/$agent ]]; then
    mkdir -p "$HOME/$agent/skills"
    ln -sfn /opt/superwhisper/assets/agent-skill/superwhisper "$HOME/$agent/skills/superwhisper"
  fi
done
omarchy-shell -q shell rescanPlugins
if [[ -f $panel_pending ]]; then
  for (( attempt = 0; attempt < 50; attempt++ )); do
    placement=$(omarchy-shell shell putBarWidget superwhisper.panel '{"section":"right"}' 2>/dev/null) || placement=""
    if [[ $placement == "ok" ]]; then
      rm "$panel_pending"
      break
    elif [[ -z $placement ]]; then
      break
    fi
    sleep 0.1
  done
  if [[ -f $panel_pending ]]; then
    echo "Superwhisper is ready; panel setup will retry next time setup-user runs." >&2
  fi
fi

# A running input method discovers the packaged text-commit addon on restart.
if systemctl --user -q is-active omarchy-fcitx5.service; then
  systemctl --user restart omarchy-fcitx5.service
fi
