#!/bin/bash

# omarchy:summary=Set Chromium browser profiles that never chose a frame to use the system title bar
# omarchy:hidden=true

set -euo pipefail

# A Chromium browser on the system title bar drops its own frame and gets
# Omarchy's titlebar when it floats. A browser with no profile yet is given one
# that starts that way, and a profile that already exists and never chose is
# switched. One that chose, either way, is left alone.
#
# A running browser holds its Preferences in memory and writes them back when
# it exits, which would undo the change. So a browser that is running is
# skipped: its profile directory is printed and the command fails, for the
# caller to ask for it to be closed and run this again.

config_home="${XDG_CONFIG_HOME:-$HOME/.config}"

browser_dirs=(
  "$config_home/chromium"
  "$config_home/google-chrome"
  "$config_home/google-chrome-beta"
  "$config_home/google-chrome-unstable"
  "$config_home/BraveSoftware/Brave-Browser"
  "$config_home/BraveSoftware/Brave-Browser-Beta"
  "$config_home/BraveSoftware/Brave-Browser-Nightly"
  "$config_home/BraveSoftware/Brave-Origin"
  "$config_home/BraveSoftware/Brave-Origin-Beta"
  "$config_home/BraveSoftware/Brave-Origin-Nightly"
  "$config_home/microsoft-edge"
  "$config_home/microsoft-edge-dev"
)

running=()

# A browser attached to a profile directory holds a SingletonLock in it: a
# symlink to <host>-<pid>, which dangles by design. One left by a browser that
# died without cleaning up names a process that is gone, so it is only held
# while that process lives. A lock from another host, on a shared home, counts
# as held.
browser_running() {
  local holder
  holder=$(readlink "$1/SingletonLock") || return 1
  if [[ $holder == "$HOSTNAME-"* ]]; then
    kill -0 "${holder##*-}" 2>/dev/null
  fi
}

for dir in "${browser_dirs[@]}"; do
  # Registering the native messaging hosts makes each browser's directory, so
  # one without a profile is a browser that has yet to run, or to be installed.
  # It keeps a Preferences file that is there before its first launch, and one
  # on that launch right now is about to write its own.
  if [[ -d $dir && ! -e $dir/Default/Preferences ]]; then
    if browser_running "$dir"; then
      running+=("$dir")
      continue
    fi

    # Without --no-first-run in its flags, as on an install from before they
    # had it, the browser's first-run setup would copy its system defaults over
    # this file. The marker tells it that setup is done.
    mkdir -p "$dir/Default"
    printf '{"browser":{"custom_chrome_frame":false}}\n' >"$dir/Default/Preferences"
    : >"$dir/First Run"
  fi

  for preferences in "$dir"/*/Preferences; do
    # A profile that chose, a file that is not valid JSON, and the unmatched
    # glob of a browser that is not installed all fail this and are passed over.
    jq -e '(.browser // {}) | has("custom_chrome_frame") | not' "$preferences" >/dev/null 2>&1 || continue

    if browser_running "$dir"; then
      running+=("$dir")
      break
    fi

    updated=$(mktemp "$preferences.XXXXXX")
    jq -c '.browser.custom_chrome_frame = false' "$preferences" >"$updated"
    chmod --reference="$preferences" "$updated"
    mv "$updated" "$preferences"
  done
done

if (( ${#running[@]} )); then
  printf '%s\n' "${running[@]}"
  exit 1
fi
