#!/bin/bash

# omarchy:summary=Notify the user when Omarchy has pending migrations

set -euo pipefail

pending_migrations=$(omarchy-migrate --pending 2>/dev/null) || exit 0
pending_count=$(printf '%s\n' "$pending_migrations" | sed '/^[[:space:]]*$/d' | wc -l)

if (( pending_count == 1 )); then
  message="Click to run 1 pending migration."
else
  message="Click to run $pending_count pending migrations."
fi

notify_command=$(printf 'if [[ -n $(omarchy-notification-send -u critical -g  "Pending Omarchy Migrations" %q -a) ]]; then omarchy-launch-floating-terminal-with-presentation omarchy-migrate; fi' "$message")

# This runs from omarchy-migrate-notify.service at graphical-session.target,
# which the session can reach before the shell has claimed
# org.freedesktop.Notifications. Without the wait the toast is sent into the
# void and the user never learns about their pending migrations.
omarchy-notification-wait || true

unit="omarchy-migrations-notification-$(date +%Y%m%d%H%M%S)"
systemd-run --user --scope --unit="$unit" bash -lc "$notify_command" >/dev/null 2>&1 && exit 0

# Reached when there is no user manager to run the scope under, such as a
# non-graphical shell, so fall back to telling the user in the terminal.
print_pending_migrations() {
  echo "Omarchy has pending migrations. Run omarchy-migrate in a terminal to apply them:"
  while IFS= read -r migration; do
    [[ -n $migration ]] || continue
    printf '  %s\n' "$migration"
  done <<<"$pending_migrations"
}

if [[ -t 1 ]]; then
  print_pending_migrations
else
  print_pending_migrations >&2
fi
