#!/usr/bin/env bash
# Makes the SDDM login background match the wallpaper on this machine's primary
# screen (set-primary-screen: eDP-1 on laptops, DP-2 otherwise, unless set).
#
# Blurs and dims that wallpaper (hyprlock's background settings) into
# system/sddm/hyprlock/background.jpg in the dotfiles repo. SDDM reads the theme
# straight from the repo through /usr/share/sddm/themes/hyprlock (a symlink set
# up by system/install.sh), so no sudo is needed here.
#
# Run by waypaper after every wallpaper change (post_command, see bootstrap.sh)
# and once at login. Safe to run any time: it does nothing if the wallpaper
# hasn't changed.
#
# Usage: sddm-sync-background [image [monitor]]
#   no args:        use what awww shows on the monitor
#   image monitor:  waypaper's $wallpaper $monitor; ignored unless monitor is
#                   the primary screen or "All"
set -euo pipefail

# This script is stowed into ~/.local/bin; follow the link back to the repo
repo="$(cd "$(dirname "$(readlink -f "$0")")/../../.." && pwd)"
theme="$repo/system/sddm/hyprlock"
out="$theme/background.jpg"
stamp="$theme/.background-source"

# This machine's primary screen (set-primary-screen), kept outside the repo so
# it isn't synced. SDDM gets it through theme.conf.user (gitignored), which it
# reads on top of theme.conf. Falls back to the default when unset.
monitor="$("$(dirname "$(readlink -f "$0")")/set-primary-screen" --print)"
user_conf="$theme/theme.conf.user"
want="$(printf '[General]\nwidgetScreen=%s\n' "$monitor")"
[[ "$(cat "$user_conf" 2>/dev/null)" == "$want" ]] || printf '%s\n' "$want" >"$user_conf"

# e.g. ": DP-2: 1920x1080, scale: 1, currently displaying: image: /path/to.jpg"
query=""
command -v awww >/dev/null && query="$(awww query 2>/dev/null || true)"
# widgetScreen isn't connected (e.g. a laptop with only eDP-1): SDDM falls back to
# the primary screen, so follow the first monitor awww reports instead
if [[ -n "$query" ]] && ! grep -q "^: $monitor: " <<<"$query"; then
    monitor="$(sed -n '1s/^: \([^:]*\): .*/\1/p' <<<"$query")"
fi

src="${1:-}"
if [[ -n "${2:-}" && "$2" != "All" && "$2" != "$monitor" ]]; then
    exit 0   # waypaper changed a different monitor
fi
if [[ -z "$src" && -n "$query" ]]; then
    src="$(sed -n "s/^: $monitor: .*currently displaying: image: //p" <<<"$query")"
fi
# awww not running (bootstrap) or showing a colour, not an image: use the default
[[ -n "$src" && -r "$src" ]] || src="$repo/dotfiles/Pictures/wallpapers/wallhaven-mlpll9.jpg"
src="$(readlink -f "$src")"   # ~/Pictures/... and the repo path are the same file

if [[ -f "$out" && "$(cat "$stamp" 2>/dev/null)" == "$src" ]]; then
    exit 0
fi

# hyprlock: blur_passes 3, brightness 0.8172, contrast 0.8916, vibrancy 0.1696.
# Shrinking before the blur keeps it fast; the result is scaled back to 1080p.
tmp="$(mktemp --suffix=.jpg)"
trap 'rm -f "$tmp"' EXIT
magick -quiet "$src" -resize '480x270^' -gravity center -extent 480x270 \
    -blur 0x6 -resize 1920x1080 -modulate 82,117,100 -brightness-contrast 0x-11 \
    -strip -quality 92 "$tmp"
install -m644 "$tmp" "$out"
printf '%s\n' "$src" >"$stamp"
echo "SDDM background updated from $src"
