#!/bin/bash

# Configure the Plymouth boot theme with a custom background color, text color, and logo.
# Stages the change in a temp dir, then commits the staged files to /usr/share and
# rebuilds the initramfs.

if [[ $# -ne 3 ]]; then
  echo "Usage: omarchy-plymouth-set <background-hex> <text-hex> <path-to-logo.png>" >&2
  exit 1
fi

bg_hex="${1#\#}"
text_hex="${2#\#}"
logo_path="$3"

if ! [[ $bg_hex =~ ^[0-9a-fA-F]{6}$ ]]; then
  echo "Invalid background color: $1 (expected #RRGGBB)" >&2
  exit 1
fi

if ! [[ $text_hex =~ ^[0-9a-fA-F]{6}$ ]]; then
  echo "Invalid text color: $2 (expected #RRGGBB)" >&2
  exit 1
fi

if [[ ! -f $logo_path ]]; then
  echo "Logo file not found: $logo_path" >&2
  exit 1
fi

bg_r=$(awk -v n=$((16#${bg_hex:0:2})) 'BEGIN{printf "%.3f", n/255}')
bg_g=$(awk -v n=$((16#${bg_hex:2:2})) 'BEGIN{printf "%.3f", n/255}')
bg_b=$(awk -v n=$((16#${bg_hex:4:2})) 'BEGIN{printf "%.3f", n/255}')

theme_dir="/usr/share/plymouth/themes/omarchy"
staging_dir=$(mktemp -d)
trap 'rm -rf "$staging_dir"' EXIT

find ~/.local/share/omarchy/default/plymouth -maxdepth 1 -type f -exec cp -t "$staging_dir/" {} +
cp "$logo_path" "$staging_dir/logo.png"

sed -i \
  -e "s/^Window.SetBackgroundTopColor.*/Window.SetBackgroundTopColor($bg_r, $bg_g, $bg_b);/" \
  -e "s/^Window.SetBackgroundBottomColor.*/Window.SetBackgroundBottomColor($bg_r, $bg_g, $bg_b);/" \
  "$staging_dir/omarchy.script"

for asset in bullet.png entry.png lock.png progress_bar.png; do
  magick "$staging_dir/$asset" -channel RGB +level-colors "#$text_hex","#$text_hex" "$staging_dir/$asset"
done

sudo cp -a "$staging_dir/." "$theme_dir/"
sudo plymouth-set-default-theme omarchy

if omarchy-cmd-present limine-mkinitcpio; then
  sudo limine-mkinitcpio
else
  sudo mkinitcpio -P
fi
