#!/bin/bash
# Fixes the "ml." icon on the right of the status bar in three parts:
#
#  1. Makes its idle color follow the active color scheme instead of ML4W's
#     fixed brand cyan (#19cddb), the same way every other bar icon already
#     does.
#  2. Fixes its hover state, which part 1 alone broke: it would turn into an
#     unreadable dark blob instead of showing bright "ml." text.
#  3. Fixes its idle state, which parts 1+2 still left broken: the glyph was
#     nearly invisible (dark-on-dark) until hovered.
#
# This CachyOS/ML4W install has waybar DISABLED (see
# ~/.config/ml4w/settings/waybar-disabled) and uses ML4W's newer Quickshell
# ("qs") status bar instead (~/.mydotfiles/com.ml4w.dotfiles.stable/.config/
# quickshell). Every bar icon is a BarButton
# (.config/quickshell/StatusbarApp/BarButton.qml), which colorizes its icon
# image via a MultiEffect: Theme.primary when idle, Theme.background when
# hovered/active -- Theme.primary/background are loaded live from
# ~/.config/ml4w/colors/colors.json (matugen output, regenerated on each
# wallpaper switch, see 06-switch-wallpaper.sh).
#
# Ml4wLogoModule.qml originally opted out of all that with `colorize: false`
# (intentionally, per a comment in BarButton.qml, to keep ML4W's own brand
# colors) -- that's part 1's root cause. But its source image
# (~/.config/ml4w/assets/ml4w.svg) is a two-tone badge (a cyan background
# square plus a separately-colored dark glyph), not the flat single-tone
# icon BarButton's colorize logic assumes. Turning colorize back on makes
# BOTH tones get mapped onto the SAME target color, which initially looked
# fine at idle but broke hover -- part 2's root cause -- fixed by swapping
# in a glyph-only icon (background square removed, transparent instead).
#
# That glyph-only SVG kept the original glyph fill (#242424, near-black) --
# wrong, and part 3's root cause. Qt's MultiEffect colorize works by
# multiplying the target color by each source pixel's own grayscale
# luminance (confirmed by sampling actual rendered pixels: idle glyph ~14%
# as bright as Theme.primary, matching #242424's ~14% luminance). Every
# OTHER working bar icon (shared/icons/launcher.svg, power.svg, ...) is
# authored with fill="white"/stroke="white" for exactly this reason: white
# has luminance 1.0, so it colorizes to the FULL, undimmed target color.
# Fixed by changing the glyph's fill to white, matching that convention --
# idle now renders at full Theme.primary brightness, hover at full
# Theme.background darkness (still legible against the bright hover circle,
# as already confirmed).
#
# The shared ml4w.svg badge itself is left untouched since WelcomeWindow.qml
# also uses it.
#
# ~/.mydotfiles/com.ml4w.dotfiles.stable is NOT a git repo and has no
# custom-override layer -- re-running 04-install-ml4w-theme.sh
# (ml4w-dotfiles-installer --install ...) backs up and replaces that whole
# profile directory (see backup_existing_profile() in
# ~/.local/share/ml4w-dotfiles-installer/utils.sh), which would silently
# undo both fixes. So, like 05-set-default-session-hyprland.sh, this script
# is idempotent and meant to be re-run after any future ML4W
# (re)install/update.
#
# Usage: 07-fix-ml4w-icon-color.sh [--vm|--local]
#   --vm (default): act on the libvirt VM over SSH.
#   --local: act on the local machine directly (an already-themed
#            laptop with the same ML4W dotfiles profile).
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"
source ./00-config.sh

parse_target_flag "$@"
set -- "${PARSED_ARGS[@]+"${PARSED_ARGS[@]}"}"

ASSETS_DIR='$HOME/.mydotfiles/com.ml4w.dotfiles.stable/.config/ml4w/assets'
GLYPH_SVG="${ASSETS_DIR}/ml4w-icon-glyph.svg"
MODULE_QML='$HOME/.mydotfiles/com.ml4w.dotfiles.stable/.config/quickshell/StatusbarApp/Ml4wLogoModule.qml'

echo ">>> Target: ${TARGET}"
echo ">>> [1/3] Writing glyph-only ml4w icon SVG (white fill, transparent"
echo "    background -- white so BarButton's luminance-based colorize"
echo "    renders it at full brightness, like every other bar icon)"
run "cat > ${GLYPH_SVG}" <<'SVG'
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   width="528.13916"
   height="528.13916"
   viewBox="0 0 528.13916 528.13916"
   version="1.1"
   xmlns="http://www.w3.org/2000/svg"><g
     transform="translate(-6283.7168,-2683.1846)"><path
       id="ellipse1"
       style="fill:#ffffff;stroke-width:8.10061;stroke-opacity:0.929412"
       d="m 6757.0157,3034.1301 a 24.915628,24.915602 0 0 1 -24.9156,24.9156 24.915628,24.915602 0 0 1 -24.9156,-24.9156 24.915628,24.915602 0 0 1 24.9156,-24.9156 24.915628,24.915602 0 0 1 24.9156,24.9156 z" /><path
       d="m 6660.1889,3056.7258 q -26.8789,0 -38.9075,-9.8011 -11.8802,-9.9496 -11.8802,-32.6704 v -178.7964 h 43.8081 v 174.6383 q 0,7.2766 2.97,10.2467 2.9701,2.8215 10.2467,2.8215 1.485,0 3.267,0 1.782,0 3.1186,0 v 33.5614 q -3.1186,0 -6.3856,0 -3.1186,0 -6.2371,0 z"
       id="text1"
       style="font-size:304.132px;fill:#ffffff;fill-rule:evenodd;stroke-width:62.8543;stroke-linecap:square;stroke-linejoin:round"
       aria-label="l" /><path
       d="m 5031.9074,3850.4758 v -199.2161 h 40.6094 v 21.454 q 1.9156,-4.9804 4.9804,-9.5777 3.448,-4.9804 7.6622,-8.4284 4.5973,-3.4479 9.9608,-4.9804 5.3635,-1.5324 11.1101,-1.5324 6.5129,0 13.0257,2.2987 6.5128,2.2986 11.1101,7.279 4.9804,4.5973 8.0453,10.727 3.0649,6.1298 4.9804,12.6426 1.5324,-6.5128 4.2142,-12.6426 3.0648,-6.1297 7.6621,-10.727 4.9804,-4.9804 11.1102,-7.279 6.5128,-2.2987 13.4088,-2.2987 6.5128,0 12.6425,2.2987 6.5129,2.2986 11.1102,6.8959 4.9804,4.2142 8.0452,10.3439 3.0649,5.7466 4.5973,12.2595 1.9156,6.1297 2.6818,13.0256 0.7662,6.5129 0.7662,13.0257 v 144.4317 h -40.6095 v -144.4317 q 0,-4.2142 -0.7662,-8.0453 -0.3831,-4.2141 -2.6817,-7.6621 -1.9156,-3.448 -5.3635,-5.7466 -3.448,-2.2987 -7.6622,-2.2987 -4.2142,0 -7.6622,2.2987 -3.4479,2.2986 -5.7466,5.7466 -1.9155,3.448 -2.6817,7.6621 -0.3831,3.8311 -0.3831,8.0453 v 144.4317 h -40.6095 v -144.4317 q 0,-4.2142 -0.7662,-8.0453 -0.3831,-4.2141 -2.6818,-7.6621 -1.9155,-3.448 -5.3635,-5.7466 -3.448,-2.2987 -7.6621,-2.2987 -4.2142,0 -7.6622,2.2987 -3.448,2.2986 -5.7466,5.7466 -1.9156,3.448 -2.6818,7.6621 -0.3831,3.8311 -0.3831,8.0453 v 144.4317 z"
       id="text2"
       style="font-weight:bold;font-size:383.108px;fill:#ffffff;fill-rule:evenodd;stroke-width:79.1758;stroke-linecap:square;stroke-linejoin:round"
       transform="scale(1.2596724,0.7938572)"
       aria-label="m" /></g></svg>
SVG

echo ">>> [2/3] Patching Ml4wLogoModule.qml (idempotent): drop 'colorize:"
echo "    false' and point iconSrc at the new glyph-only SVG"
run "sed -i \
    -e '/^\s*colorize:\s*false\s*\$/d' \
    -e 's#\(iconSrc:.*\)/.config/ml4w/assets/ml4w\(-icon-glyph\)\?\.svg\"#\1/.config/ml4w/assets/ml4w-icon-glyph.svg\"#' \
    ${MODULE_QML}"

echo ">>> Verifying:"
run "cat ${MODULE_QML}"

echo ">>> [3/3] Reloading the live Quickshell theme (re-reads colors.json"
echo "    and re-applies tinting to every bar icon, including this one)"
run_script <<'REMOTE'
set -euo pipefail
RUNTIME_DIR="/run/user/$(id -u)"
SIG=$(ls "$RUNTIME_DIR/hypr" 2>/dev/null | head -n1)
SOCK=$(ls "$RUNTIME_DIR" 2>/dev/null | grep -E '^wayland-[0-9]+$' | head -n1)
if [ -z "$SIG" ] || [ -z "$SOCK" ]; then
    echo "No running Hyprland session found -- the changes are saved and" >&2
    echo "will take effect next login/qs restart." >&2
    exit 0
fi
export XDG_RUNTIME_DIR="$RUNTIME_DIR"
export WAYLAND_DISPLAY="$SOCK"
export HYPRLAND_INSTANCE_SIGNATURE="$SIG"
qs ipc call theme-manager reload
REMOTE

echo ">>> Done."
