patina-firefox-theme (5859B)
1 #!/bin/sh 2 # Puts Patina's Firefox theme into the user's Firefox profiles (ADR-0018): 3 # 4 # patina-firefox-theme 5 # 6 # The theme is userChrome CSS, which Firefox reads from a profile's chrome/ folder: the system's 7 # copy, /usr/share/patina-os/firefox/patina-default.css (from patina-browser-themes), is copied there 8 # and imported at the top of chrome/userChrome.css, where anything of the user's stays. Every profile 9 # in Firefox's profiles.ini gets it. Safe to run again, and a no-op without a profile. Run as the 10 # user, by systemd: at login, and when Firefox first writes its profiles.ini. 11 # (The pref that has Firefox read userChrome.css is set for every profile by the policy in 12 # /usr/lib64/firefox/distribution/policies.json.) 13 set -eu 14 15 # GTK draws the frame of Firefox's window (its outline and the transparent corners) from its own 16 # theme, whose corners are 8 px; the theme's CSS rounds the page at Patina's 15 px, so the outline 17 # runs inside the page's corners and shows as a dark wedge. GTK3 reads a CSS file only from the 18 # user's own config folder, so a block in ~/.config/gtk-3.0/gtk.css sets the frame to 15 px too. 19 # Not for a window that is maximized, fullscreen or tiled (those are square). A file that has the 20 # line /* patina: do not manage */ is left alone. 21 gtk=${XDG_CONFIG_HOME:-$HOME/.config}/gtk-3.0/gtk.css 22 begin='/* >>> patina: window corners (managed by patina-firefox-theme) */' 23 end='/* <<< patina */' 24 if ! grep -qF '/* patina: do not manage */' "$gtk" 2>/dev/null; then 25 block=$(cat <<EOF 26 $begin 27 window.csd:not(.maximized):not(.fullscreen):not(.tiled):not(.tiled-top):not(.tiled-bottom):not(.tiled-left):not(.tiled-right) decoration { 28 border-radius: 15px; 29 } 30 $end 31 EOF 32 ) 33 mkdir -p "$(dirname "$gtk")" 34 touch "$gtk" 35 # The rest of the file, without an earlier copy of the block. 36 rest=$(awk -v b="$begin" -v e="$end" '$0 == b { skip = 1; next } $0 == e { skip = 0; next } !skip' "$gtk") 37 new=$block 38 [ -z "$rest" ] || new=$block$(printf '\n\n%s' "$rest") 39 [ "$(cat "$gtk")" = "$new" ] || printf '%s\n' "$new" > "$gtk" 40 fi 41 42 # A machine with no Firefox profile yet gets one made for it, with a few of the new tab page's 43 # settings already the user's own (ADR-0018). Why not defaults: Firefox works out the new tab 44 # widgets' defaults itself after policies and pref files have run, so a default cannot keep them off 45 # (and the only other way, locking them, would take the choice away). A profile that starts with 46 # the values in it is exactly what a person who switches them off in Customize ends up with, and 47 # from then on the values are theirs: this runs only when there is no profiles.ini at all, so it never 48 # touches a profile that exists, and an update never changes one. 49 seed_new_profile() { 50 config=${XDG_CONFIG_HOME:-$HOME/.config} 51 root=$config/mozilla/firefox 52 [ -e "$root/profiles.ini" ] || [ -e "$HOME/.mozilla/firefox/profiles.ini" ] && return 0 53 # Firefox running with no profiles.ini cannot happen, but never write under one that is starting. 54 ! pgrep -u "$(id -u)" -x firefox >/dev/null 2>&1 || return 0 55 dir=patina.default-release 56 mkdir -p "$root/$dir" 57 cat > "$root/$dir/prefs.js" <<'EOF' 58 // Mozilla User Preferences 59 // Seeded once by patina-firefox-theme when this profile was made (ADR-0018). These are the user's 60 // own now: change them in Firefox's Customize or Settings, and they stay as changed. 61 user_pref("browser.newtabpage.activity-stream.widgets.enabled", false); 62 user_pref("browser.newtabpage.activity-stream.widgets.clocks.enabled", false); 63 user_pref("browser.newtabpage.activity-stream.widgets.focusTimer.enabled", false); 64 user_pref("browser.newtabpage.activity-stream.widgets.lists.enabled", false); 65 user_pref("browser.newtabpage.activity-stream.widgets.sportsWidget.enabled", false); 66 user_pref("browser.newtabpage.activity-stream.widgets.weather.enabled", false); 67 user_pref("browser.newtabpage.activity-stream.showWeather", false); 68 EOF 69 # Firefox picks its profile through installs.ini, which maps this installation to one; the key is 70 # a hash of the install path, the same on every Fedora machine (Firefox writes this one itself). 71 # Without it Firefox would not use this profile and would make another. The files are written the 72 # way Firefox writes them, profiles.ini last: it is what says the profile exists. 73 install=11457493C5A56847 74 printf '[%s]\nDefault=%s\nLocked=1\n' "$install" "$dir" > "$root/installs.ini" 75 printf '[Install%s]\nDefault=%s\nLocked=1\n\n[Profile0]\nName=default-release\nIsRelative=1\nPath=%s\n\n[General]\nStartWithLastProfile=1\nVersion=2\n' "$install" "$dir" "$dir" > "$root/profiles.ini.new" 76 mv "$root/profiles.ini.new" "$root/profiles.ini" 77 } 78 seed_new_profile 79 80 theme=patina-default 81 src=/usr/share/patina-os/firefox/$theme.css 82 [ -f "$src" ] || exit 0 83 import="@import url(\"$theme.css\");" 84 85 for root in "${XDG_CONFIG_HOME:-$HOME/.config}/mozilla/firefox" "$HOME/.mozilla/firefox"; do 86 ini=$root/profiles.ini 87 [ -f "$ini" ] || continue 88 # Each profile's Path, and whether it is relative to the folder profiles.ini is in. 89 awk -F= ' 90 /^\[/ { if (path != "") print rel "\t" path; path = ""; rel = 1; next } 91 $1 == "Path" { path = $2 } 92 $1 == "IsRelative" { rel = $2 } 93 END { if (path != "") print rel "\t" path }' "$ini" | 94 while IFS=" " read -r rel path; do 95 if [ "$rel" = 1 ]; then dir=$root/$path; else dir=$path; fi 96 [ -d "$dir" ] || continue 97 mkdir -p "$dir/chrome" 98 cp -f "$src" "$dir/chrome/$theme.css" 99 css=$dir/chrome/userChrome.css 100 touch "$css" 101 # Already first? Then nothing more to do (and nothing rewritten). 102 [ "$(head -n 1 "$css")" = "$import" ] && continue 103 { echo "$import"; grep -vxF "$import" "$css" || true; } > "$css.new" 104 mv "$css.new" "$css" 105 done 106 done