#!/bin/sh
# Enable 'splash' only on UEFI desktop installs, and only if Plymouth is present.
# Runs before 10update-initramfs, so we only call update-grub here.
#

set -e

# UEFI only in installer env
[ -d /sys/firmware/efi ] || exit 0

# Desktop detection (target): Trisquel desktop metas OR common DMs
if in-target dpkg -s trisquel-desktop-common >/dev/null 2>&1 \
   || in-target dpkg -s triskel >/dev/null 2>&1 \
   || in-target dpkg -s trisquel-gnome >/dev/null 2>&1 \
   || in-target dpkg -s trisquel-mini  >/dev/null 2>&1 \
   || in-target dpkg -s lightdm >/dev/null 2>&1 \
   || in-target dpkg -s gdm3   >/dev/null 2>&1 \
   || in-target dpkg -s sddm   >/dev/null 2>&1; then
    logger -t trisquel-uefi-splash "desktop detected (packages)"
else
    logger -t trisquel-uefi-splash "no desktop; skip"
    exit 0
fi

# Plymouth present in target?
in-target dpkg -s plymouth >/dev/null 2>&1 || { logger -t trisquel-uefi-splash "no plymouth; skip"; exit 0; }

# Edit /etc/default/grub inside target: ensure GRUB_CMDLINE_LINUX_DEFAULT includes 'splash'
in-target sh -s <<'INCHROOT'
set -e
CFG="/etc/default/grub"
[ -f "$CFG" ] || touch "$CFG"

awk -v key="GRUB_CMDLINE_LINUX_DEFAULT" '
BEGIN{done=0}
# Match the variable (with or without quotes around value)
($0 ~ "^"key"=") {
  line = $0
  val = $0
  sub("^"key"=","",val)
  # strip surrounding double quotes if present
  if (val ~ /^"/) sub(/^"/,"",val)
  if (val ~ /"$/) sub(/"$/,"",val)
  # normalize trailing spaces
  gsub(/[[:space:]]+$/, "", val)
  # check presence of splash
  found=0
  n=split(val, tok, /[[:space:]]+/)
  for (i=1;i<=n;i++) if (tok[i]=="splash") { found=1; break }
  if (!found) {
    if (val == "") val = "quiet splash"; else val = val " splash"
  }
  print key "=\"" val "\""
  done=1
  next
}
{ print }
END{
  if (!done) print key "=\"quiet splash\""
}
' "$CFG" > "$CFG.tmp" && mv "$CFG.tmp" "$CFG"

update-grub || true
INCHROOT

logger -t trisquel-uefi-splash "splash ensured; update-grub called"
exit 0
