#!/bin/sh
# rikkiti-papirus-accent <papirus-colour>|reset
#
# Per-user accent-coloured folders: builds ~/.local/share/icons/Papirus-Rikkiti,
# a thin icon theme that Inherits=Papirus and overrides ONLY the places/ dirs,
# re-pointing the generic folder-*.svg names at Papirus' folder-<colour>-*.svg
# variants (same trick as the upstream papirus-folders script, but per-user —
# no root). rikkiti-settings invokes this when the accent changes; `reset`
# removes the override theme. Idempotent, ~instant.
set -eu
c="${1:-}"
src=/usr/share/icons/Papirus
dst="${XDG_DATA_HOME:-$HOME/.local/share}/icons/Papirus-Rikkiti"
[ -n "$c" ] || { echo "usage: rikkiti-papirus-accent <colour>|reset" >&2; exit 2; }
[ -d "$src" ] || exit 0 # Papirus not installed — nothing to do
# appdb rasterizes SVG icons into ~/.cache/rikkiti/icons/<name>.png keyed by
# NAME only — a folder rendered under the old colour stays stale forever unless
# we drop it. Clear the folder/user entries on every colour change (and reset).
clear_raster_cache() {
	rm -f "${XDG_CACHE_HOME:-$HOME/.cache}/rikkiti/icons/folder"*.png \
	      "${XDG_CACHE_HOME:-$HOME/.cache}/rikkiti/icons/user-"*.png 2>/dev/null || true
}
if [ "$c" = reset ]; then rm -rf "$dst"; clear_raster_cache; exit 0; fi
clear_raster_cache

tmp="$dst.tmp.$$"
rm -rf "$tmp"; mkdir -p "$tmp"
dirs=""
for d in "$src"/*/places; do
	[ -d "$d" ] || continue
	size_dir=$(basename "$(dirname "$d")")    # e.g. 64x64
	case "$size_dir" in *x*) ;; *) continue ;; esac
	out="$tmp/$size_dir/places"
	found=0
	for f in "$d"/folder-"$c".svg "$d"/folder-"$c"-*.svg "$d"/user-"$c"-*.svg; do
		[ -e "$f" ] || continue
		[ $found = 0 ] && mkdir -p "$out"
		found=1
		base=$(basename "$f")
		gen=$(printf '%s' "$base" | sed "s/-$c//")  # folder-green-music → folder-music
		ln -sf "$f" "$out/$gen"
	done
	# Stock folder-publicshare.svg is a colourless hardlink (people-badge folder,
	# blue) with no -publicshare colour variant — but folder-<c>-public.svg IS the
	# same motif in colour. Map it so the Public dir follows the accent too.
	if [ $found = 1 ] && [ -e "$d/folder-$c-public.svg" ]; then
		ln -sf "$d/folder-$c-public.svg" "$out/folder-publicshare.svg"
	fi
	[ $found = 1 ] && dirs="$dirs$size_dir/places,"
done
dirs="${dirs%,}"
[ -n "$dirs" ] || { rm -rf "$tmp"; exit 0; } # unknown colour — leave stock

{
	echo "[Icon Theme]"
	echo "Name=Papirus-Rikkiti"
	echo "Comment=Papirus with Rikkiti accent-coloured folders"
	echo "Inherits=Papirus"
	echo "Directories=$dirs"
	for d in $(printf '%s' "$dirs" | tr , ' '); do
		echo ""
		echo "[$d]"
		echo "Size=${d%%x*}"
		case "$d" in *@2x*) echo "Scale=2" ;; esac
		echo "Context=Places"
		echo "Type=Fixed"
	done
} > "$tmp/index.theme"

rm -rf "$dst"
mv "$tmp" "$dst"

# Build the GTK icon-theme.cache so GTK apps (nm-applet, blueman, file dialogs) mmap a
# shared read-only index instead of loading the whole icon tree into each app's PRIVATE
# heap. Without it, every GTK app pays ~80MB privately for Papirus (this theme Inherits it)
# — measured on real hw. gtk-update-icon-cache's dpkg trigger only UPDATES an existing
# cache, never creates one, and Papirus ships none → we must build it explicitly. Quiet,
# best-effort (never fail an accent change over it).
command -v gtk-update-icon-cache >/dev/null 2>&1 && gtk-update-icon-cache -q -f -t "$dst" 2>/dev/null || true
