#!/usr/bin/env bash
set -u

VERSION="0.8.5"
WALLPAPER="file:///usr/share/backgrounds/brz-healthos/brz-healthos-aug-2026.png"
MARKER_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/brz-healthos"
MARKER="$MARKER_DIR/desktop-${VERSION}-applied"

# Apply release defaults once. Later choices made in GNOME Appearance belong to
# the user and must survive logout/restart.
if [ -f "$MARKER" ]; then
  exit 0
fi

has_schema() {
  gsettings list-schemas 2>/dev/null | grep -Fxq "$1"
}

has_key() {
  local schema="$1" key="$2"
  has_schema "$schema" && gsettings list-keys "$schema" 2>/dev/null | grep -Fxq "$key"
}

set_key() {
  local schema="$1" key="$2" value="$3"
  if has_key "$schema" "$key"; then
    gsettings set "$schema" "$key" "$value" >/dev/null 2>&1 || true
  fi
}

reset_key() {
  local schema="$1" key="$2"
  if has_key "$schema" "$key"; then
    gsettings reset "$schema" "$key" >/dev/null 2>&1 || true
  fi
}

mkdir -p "$MARKER_DIR"

# Keep Ubuntu's original Yaru/Adwaita desktop themes. The BRZ icon layer only
# replaces distribution/Show Applications identity marks and inherits every
# normal application/system icon from Yaru.
reset_key org.gnome.desktop.interface gtk-theme
set_key org.gnome.desktop.interface icon-theme "'BRZ-HealthOS'"
reset_key org.gnome.desktop.interface cursor-theme
reset_key org.gnome.desktop.interface color-scheme
set_key org.gnome.desktop.background picture-uri "'$WALLPAPER'"
set_key org.gnome.desktop.background picture-uri-dark "'$WALLPAPER'"
set_key org.gnome.desktop.background picture-options "'zoom'"
set_key org.gnome.desktop.screensaver picture-uri "'$WALLPAPER'"
set_key org.gnome.desktop.interface enable-animations true
set_key org.gnome.desktop.interface clock-format "'24h'"
set_key org.gnome.desktop.interface clock-show-date true
set_key org.gnome.desktop.interface clock-show-weekday true
set_key org.gnome.desktop.interface enable-hot-corners true

# Undo settings from earlier BRZ themed builds and use Ubuntu defaults.
reset_key org.gnome.desktop.wm.preferences button-layout
reset_key org.gnome.desktop.wm.preferences center-new-windows
reset_key org.gnome.mutter dynamic-workspaces

# BRZ shell extension only adds BRZ identity and replaces the Show Applications
# logo. It deliberately does NOT load a custom GNOME Shell stylesheet.
set_key org.gnome.shell disable-user-extensions false
if command -v gnome-extensions >/dev/null 2>&1; then
  gnome-extensions enable brz-healthos-shell@brztermin.com >/dev/null 2>&1 || true
  gnome-extensions enable ubuntu-dock@ubuntu.com >/dev/null 2>&1 || true
fi

# Restore all dock keys touched by older BRZ releases to Ubuntu defaults.
for key in dock-position extend-height always-center-icons dock-fixed autohide \
  intellihide intellihide-mode autohide-in-fullscreen require-pressure-to-show \
  show-delay hide-delay animation-time dash-max-icon-size click-action show-trash \
  show-mounts show-show-apps-button show-apps-at-top show-apps-always-in-the-edge \
  custom-theme-shrink transparency-mode background-opacity custom-background-color \
  background-color custom-theme-running-dots-color \
  custom-theme-running-dots-border-color custom-theme-running-dots-border-width \
  apply-custom-theme; do
  reset_key org.gnome.shell.extensions.dash-to-dock "$key"
done

# Quiet desktop for a managed healthcare workstation.
set_key org.gnome.shell.extensions.ding show-home false
set_key org.gnome.shell.extensions.ding show-trash false

printf '%s\n' "$VERSION" > "$MARKER"
printf '%s\n' "BRZ HealthOS Stable Branding $VERSION applied."
printf '%s\n' "Original Ubuntu/Yaru themes restored; BRZ identity/logo layer enabled."
printf '%s\n' "Log out and back in to reload GNOME Shell."

# Ensure the primary pharmacy platform is immediately available on the desktop
# for the current user. Administrators can add more HTTPS shortcuts with
# "Add Web Shortcut" or the brz-web-shortcut command.
if command -v brz-web-shortcut >/dev/null 2>&1; then
  brz-web-shortcut add "PlatformaOTC" "https://app.platformaotc.com" "brz-healthos" >/dev/null 2>&1 || true
fi
