#!/bin/bash

# make firefox with host-individual profile as default (important for gac-bernina account)
xdg-settings set default-web-browser bernifox.desktop

#clear some default keyborad shortcuts for later reassignment of important actions
gsettings set org.gnome.settings-daemon.plugins.media-keys calculator []
gsettings set org.gnome.settings-daemon.plugins.media-keys email []
gsettings set org.gnome.settings-daemon.plugins.media-keys home []
gsettings set org.gnome.settings-daemon.plugins.media-keys screenshot []
gsettings set org.gnome.settings-daemon.plugins.media-keys help []
# org.gnome.shell.overrides is the key gnome-shell actually reads (its
# schema documents itself as overriding the mutter key "when running GNOME
# Shell", confirmed via `gsettings describe`), but org.gnome.mutter has its
# own separate copy of the same setting that stays at its default (false)
# and was found sitting there unmanaged/inconsistent. Set both explicitly
# so there's nothing left ambiguous, in case that ever changes across a
# GNOME version.
gsettings set org.gnome.shell.overrides workspaces-only-on-primary true
gsettings set org.gnome.mutter workspaces-only-on-primary true

# use Caps Lock as a compose key, so accented/special characters (and
# Greek letters, see below) can be typed as Compose + <sequence>
gsettings set org.gnome.desktop.input-sources xkb-options "['compose:caps']"

# Greek letters via the compose key: CapsLock, Space, <mnemonic letter>
# (either order) -- e.g. CapsLock Space p -> pi, CapsLock Space Shift+P
# -> Pi. Conflicting first letters are resolved as: h=theta, y=eta,
# c=xi, x=chi, q=psi, w=omega, v=final sigma, giving every Latin letter
# a-z (except j) a unique Greek target. Rewritten every login so it
# self-heals if missing or accidentally edited.
cat > "$HOME/.XCompose" <<'EOF'
include "%L"

# --- Greek letters (Compose Space <letter>, either order) ---
<Multi_key> <space> <a> : "α" U03B1 GREEK SMALL LETTER ALPHA
<Multi_key> <a> <space> : "α" U03B1 GREEK SMALL LETTER ALPHA
<Multi_key> <space> <A> : "Α" U0391 GREEK CAPITAL LETTER ALPHA
<Multi_key> <A> <space> : "Α" U0391 GREEK CAPITAL LETTER ALPHA
<Multi_key> <space> <b> : "β" U03B2 GREEK SMALL LETTER BETA
<Multi_key> <b> <space> : "β" U03B2 GREEK SMALL LETTER BETA
<Multi_key> <space> <B> : "Β" U0392 GREEK CAPITAL LETTER BETA
<Multi_key> <B> <space> : "Β" U0392 GREEK CAPITAL LETTER BETA
<Multi_key> <space> <c> : "ξ" U03BE GREEK SMALL LETTER XI
<Multi_key> <c> <space> : "ξ" U03BE GREEK SMALL LETTER XI
<Multi_key> <space> <C> : "Ξ" U039E GREEK CAPITAL LETTER XI
<Multi_key> <C> <space> : "Ξ" U039E GREEK CAPITAL LETTER XI
<Multi_key> <space> <d> : "δ" U03B4 GREEK SMALL LETTER DELTA
<Multi_key> <d> <space> : "δ" U03B4 GREEK SMALL LETTER DELTA
<Multi_key> <space> <D> : "Δ" U0394 GREEK CAPITAL LETTER DELTA
<Multi_key> <D> <space> : "Δ" U0394 GREEK CAPITAL LETTER DELTA
<Multi_key> <space> <e> : "ε" U03B5 GREEK SMALL LETTER EPSILON
<Multi_key> <e> <space> : "ε" U03B5 GREEK SMALL LETTER EPSILON
<Multi_key> <space> <E> : "Ε" U0395 GREEK CAPITAL LETTER EPSILON
<Multi_key> <E> <space> : "Ε" U0395 GREEK CAPITAL LETTER EPSILON
<Multi_key> <space> <f> : "φ" U03C6 GREEK SMALL LETTER PHI
<Multi_key> <f> <space> : "φ" U03C6 GREEK SMALL LETTER PHI
<Multi_key> <space> <F> : "Φ" U03A6 GREEK CAPITAL LETTER PHI
<Multi_key> <F> <space> : "Φ" U03A6 GREEK CAPITAL LETTER PHI
<Multi_key> <space> <g> : "γ" U03B3 GREEK SMALL LETTER GAMMA
<Multi_key> <g> <space> : "γ" U03B3 GREEK SMALL LETTER GAMMA
<Multi_key> <space> <G> : "Γ" U0393 GREEK CAPITAL LETTER GAMMA
<Multi_key> <G> <space> : "Γ" U0393 GREEK CAPITAL LETTER GAMMA
<Multi_key> <space> <h> : "θ" U03B8 GREEK SMALL LETTER THETA
<Multi_key> <h> <space> : "θ" U03B8 GREEK SMALL LETTER THETA
<Multi_key> <space> <H> : "Θ" U0398 GREEK CAPITAL LETTER THETA
<Multi_key> <H> <space> : "Θ" U0398 GREEK CAPITAL LETTER THETA
<Multi_key> <space> <i> : "ι" U03B9 GREEK SMALL LETTER IOTA
<Multi_key> <i> <space> : "ι" U03B9 GREEK SMALL LETTER IOTA
<Multi_key> <space> <I> : "Ι" U0399 GREEK CAPITAL LETTER IOTA
<Multi_key> <I> <space> : "Ι" U0399 GREEK CAPITAL LETTER IOTA
<Multi_key> <space> <k> : "κ" U03BA GREEK SMALL LETTER KAPPA
<Multi_key> <k> <space> : "κ" U03BA GREEK SMALL LETTER KAPPA
<Multi_key> <space> <K> : "Κ" U039A GREEK CAPITAL LETTER KAPPA
<Multi_key> <K> <space> : "Κ" U039A GREEK CAPITAL LETTER KAPPA
<Multi_key> <space> <l> : "λ" U03BB GREEK SMALL LETTER LAMBDA
<Multi_key> <l> <space> : "λ" U03BB GREEK SMALL LETTER LAMBDA
<Multi_key> <space> <L> : "Λ" U039B GREEK CAPITAL LETTER LAMBDA
<Multi_key> <L> <space> : "Λ" U039B GREEK CAPITAL LETTER LAMBDA
<Multi_key> <space> <m> : "μ" U03BC GREEK SMALL LETTER MU
<Multi_key> <m> <space> : "μ" U03BC GREEK SMALL LETTER MU
<Multi_key> <space> <M> : "Μ" U039C GREEK CAPITAL LETTER MU
<Multi_key> <M> <space> : "Μ" U039C GREEK CAPITAL LETTER MU
<Multi_key> <space> <n> : "ν" U03BD GREEK SMALL LETTER NU
<Multi_key> <n> <space> : "ν" U03BD GREEK SMALL LETTER NU
<Multi_key> <space> <N> : "Ν" U039D GREEK CAPITAL LETTER NU
<Multi_key> <N> <space> : "Ν" U039D GREEK CAPITAL LETTER NU
<Multi_key> <space> <o> : "ο" U03BF GREEK SMALL LETTER OMICRON
<Multi_key> <o> <space> : "ο" U03BF GREEK SMALL LETTER OMICRON
<Multi_key> <space> <O> : "Ο" U039F GREEK CAPITAL LETTER OMICRON
<Multi_key> <O> <space> : "Ο" U039F GREEK CAPITAL LETTER OMICRON
<Multi_key> <space> <p> : "π" U03C0 GREEK SMALL LETTER PI
<Multi_key> <p> <space> : "π" U03C0 GREEK SMALL LETTER PI
<Multi_key> <space> <P> : "Π" U03A0 GREEK CAPITAL LETTER PI
<Multi_key> <P> <space> : "Π" U03A0 GREEK CAPITAL LETTER PI
<Multi_key> <space> <q> : "ψ" U03C8 GREEK SMALL LETTER PSI
<Multi_key> <q> <space> : "ψ" U03C8 GREEK SMALL LETTER PSI
<Multi_key> <space> <Q> : "Ψ" U03A8 GREEK CAPITAL LETTER PSI
<Multi_key> <Q> <space> : "Ψ" U03A8 GREEK CAPITAL LETTER PSI
<Multi_key> <space> <r> : "ρ" U03C1 GREEK SMALL LETTER RHO
<Multi_key> <r> <space> : "ρ" U03C1 GREEK SMALL LETTER RHO
<Multi_key> <space> <R> : "Ρ" U03A1 GREEK CAPITAL LETTER RHO
<Multi_key> <R> <space> : "Ρ" U03A1 GREEK CAPITAL LETTER RHO
<Multi_key> <space> <s> : "σ" U03C3 GREEK SMALL LETTER SIGMA
<Multi_key> <s> <space> : "σ" U03C3 GREEK SMALL LETTER SIGMA
<Multi_key> <space> <S> : "Σ" U03A3 GREEK CAPITAL LETTER SIGMA
<Multi_key> <S> <space> : "Σ" U03A3 GREEK CAPITAL LETTER SIGMA
<Multi_key> <space> <t> : "τ" U03C4 GREEK SMALL LETTER TAU
<Multi_key> <t> <space> : "τ" U03C4 GREEK SMALL LETTER TAU
<Multi_key> <space> <T> : "Τ" U03A4 GREEK CAPITAL LETTER TAU
<Multi_key> <T> <space> : "Τ" U03A4 GREEK CAPITAL LETTER TAU
<Multi_key> <space> <u> : "υ" U03C5 GREEK SMALL LETTER UPSILON
<Multi_key> <u> <space> : "υ" U03C5 GREEK SMALL LETTER UPSILON
<Multi_key> <space> <U> : "Υ" U03A5 GREEK CAPITAL LETTER UPSILON
<Multi_key> <U> <space> : "Υ" U03A5 GREEK CAPITAL LETTER UPSILON
<Multi_key> <space> <v> : "ς" U03C2 GREEK SMALL LETTER FINAL SIGMA
<Multi_key> <v> <space> : "ς" U03C2 GREEK SMALL LETTER FINAL SIGMA
<Multi_key> <space> <w> : "ω" U03C9 GREEK SMALL LETTER OMEGA
<Multi_key> <w> <space> : "ω" U03C9 GREEK SMALL LETTER OMEGA
<Multi_key> <space> <W> : "Ω" U03A9 GREEK CAPITAL LETTER OMEGA
<Multi_key> <W> <space> : "Ω" U03A9 GREEK CAPITAL LETTER OMEGA
<Multi_key> <space> <x> : "χ" U03C7 GREEK SMALL LETTER CHI
<Multi_key> <x> <space> : "χ" U03C7 GREEK SMALL LETTER CHI
<Multi_key> <space> <X> : "Χ" U03A7 GREEK CAPITAL LETTER CHI
<Multi_key> <X> <space> : "Χ" U03A7 GREEK CAPITAL LETTER CHI
<Multi_key> <space> <y> : "η" U03B7 GREEK SMALL LETTER ETA
<Multi_key> <y> <space> : "η" U03B7 GREEK SMALL LETTER ETA
<Multi_key> <space> <Y> : "Η" U0397 GREEK CAPITAL LETTER ETA
<Multi_key> <Y> <space> : "Η" U0397 GREEK CAPITAL LETTER ETA
<Multi_key> <space> <z> : "ζ" U03B6 GREEK SMALL LETTER ZETA
<Multi_key> <z> <space> : "ζ" U03B6 GREEK SMALL LETTER ZETA
<Multi_key> <space> <Z> : "Ζ" U0396 GREEK CAPITAL LETTER ZETA
<Multi_key> <Z> <space> : "Ζ" U0396 GREEK CAPITAL LETTER ZETA
EOF

# Defensive cleanup: this script used to install and enable the "Shortcuts"
# gnome-shell extension (Shortcuts@kyle.aims.ac.za) for the cheat sheet
# overlay, bound to its own internal Super+F1 toggle, before switching to
# the current standalone cheatsheet_overlay script. Extension enabled-state
# is per-host/per-login and isn't touched by this script's own updates, so
# any host gac-bernina logged into during that period is still silently
# running it, intercepting Super+F1 before the custom-keybinding below ever
# gets a chance (confirmed on saresb-cons-05: the extension was still
# enabled and still bound, showing its own old-style overlay with no title
# bar, while ps showed no matching process at all -- because it renders
# inside gnome-shell itself, not as a separate process). Safe/idempotent if
# already disabled or never installed.
if gnome-extensions list --enabled 2>/dev/null | grep -qx "Shortcuts@kyle.aims.ac.za"; then
    gnome-extensions disable "Shortcuts@kyle.aims.ac.za"
    echo "Disabled leftover Shortcuts@kyle.aims.ac.za gnome-shell extension (superseded by cheatsheet_overlay)"
fi
rm -rf "$HOME/.local/share/gnome-shell/extensions/Shortcuts@kyle.aims.ac.za"
# The extension's own settings subtree is a separate relocatable schema and
# survives disabling it or deleting its directory -- reset it too so it
# doesn't linger as orphaned dconf state (found still present on hosts that
# ran the extension long after it was disabled above).
dconf reset -f /org/gnome/shell/extensions/shortcuts/

#custom bernina keyboard shortcuts
# Full subtree wipe, not just the list: set_gnome_keyboard_shortcut always
# appends to the next free customN slot and never reuses/cleans up old
# ones, so a `gsettings reset ... custom-keybindings` here only clears which
# paths are active -- the individual customN/{name,command,binding} nodes
# below are a relocatable schema and persist forever once written. Any
# future change to the number/order of the set_gnome_keyboard_shortcut
# calls below would otherwise orphan whatever slots existed under the old
# call count (found on saresb-cons-05: 11 stray custom18-custom28 nodes
# from a since-removed shortcut).
dconf reset -f /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/
if [ "$(hostname)" = "saresb-cons-03.psi.ch" ] || [ "$(hostname)" = "saresb-cons-04.psi.ch" ] || [ "$(hostname)" = "saresb-cons-05.psi.ch" ] || [ "$(hostname)" = "saresb-cons-08.psi.ch" ] || [ "$(hostname)" = "saresb-cons-09.psi.ch" ] || [ "$(hostname)" = "saresb-cons-10.psi.ch" ]; then
    echo "This is a primary controls terminal"
    set_gnome_keyboard_shortcut 'stop_fel' 'feloff' 'Calculator'
    set_gnome_keyboard_shortcut 'open_fel' 'felon' '<Super>Calculator'
    set_gnome_keyboard_shortcut 'stop_laser' 'laseroff' 'Mail'
    set_gnome_keyboard_shortcut 'open_laser' 'laseron' '<Super>Mail'
    echo "done"
  else
    echo "no primary controls terminal"
fi
set_gnome_keyboard_shortcut 'eco' 'gnome-terminal -- eco' '<Alt><Control>j'
set_gnome_keyboard_shortcut 'eco' 'gnome-terminal -- eco' '<Super>HomePage'
set_gnome_keyboard_shortcut 'launcher' 'launcher' 'HomePage'
set_gnome_keyboard_shortcut 'browser' 'firefox' '<Alt><Control>b'
set_gnome_keyboard_shortcut 'aramis_synoptiic' 'caqtdm -noMsg  -stylefile sfop.qss   Aramis.ui' '<Alt><Control>a'
set_gnome_keyboard_shortcut 'swissfel_op_overview' 'caqtdm -noMsg  -stylefile sfop.qss -macro chargelim=15  S_OP_overview_for_photonics.ui' '<Alt><Control>s'
set_gnome_keyboard_shortcut 'terminal' 'gnome-terminal' '<Alt><Control>t'
set_gnome_keyboard_shortcut 'browser-as' 'firefox_as' '<Alt><Control><Shift>b'
set_gnome_keyboard_shortcut 'terminal-as' 'gnome-terminal_as' '<Alt><Control><Shift>t'
set_gnome_keyboard_shortcut 'winterm' 'winterm' '<Alt><Control><Shift>w'
set_gnome_keyboard_shortcut 'cheatsheet' '/sf/bernina/bin/cheatsheet_overlay' '<Super>F1'
set_gnome_keyboard_shortcut 'Flameshot' 'flameshot gui' 'Print'
set_gnome_keyboard_shortcut 'dark screens' 'xset dpms force off' '<Ctrl><Alt>d'

# gTile and Tactile are served straight from the shared, read-only
# /sf/bernina/applications/gnome-shell/extensions/ tree (real dirs there,
# symlinked in from ../../gTile@vibou and ../../tactile@lundal.io) via
# XDG_DATA_DIRS -- see the environment.d block below. No per-account copy
# step needed: confirmed (2026-09-09, saresb-cons-04, Looking Glass) that
# gnome-shell's own startup scan of ~/.local/share/gnome-shell/extensions
# is broken on this desktop image -- Main.extensionManager.lookup() stayed
# undefined for a perfectly valid, correctly-listed-in-enabled-extensions
# copy there, confirmed via strings on libgnome-shell.so that extension
# directory resolution is GLib.get_system_data_dirs() (i.e. XDG_DATA_DIRS)
# gated by an `includeUserDir` flag that's evidently false for extensions
# on this RHEL9 build (still true for e.g. themes) -- almost certainly a
# deliberate enterprise hardening choice, not a bug to work around per se.
# Pointing XDG_DATA_DIRS at the shared tree instead sidesteps this
# entirely and lets gnome-shell's normal, working system-extension scan
# pick both up, exactly like the stock dash-to-panel/dash-to-dock/etc.
#
# GNOME Shell's own extensions.gnome.org auto-update check doesn't know or
# care that these UUIDs are pinned/managed via that shared tree -- it
# silently fetched and swapped in a newer upstream gTile build once
# already (confirmed: v52 replacing our pinned v44, with a different
# settings/keybinding schema, breaking every gTile shortcut on
# saresb-cons-05). allow-extension-installation gates that whole
# background fetch/stage/swap mechanism; there's no reason for it to be on
# when every extension this desktop uses is already managed by hand.
gsettings set org.gnome.shell allow-extension-installation false

# Ensure XDG_DATA_DIRS includes the shared extensions tree for *future*
# logins -- systemd only reads ~/.config/environment.d/*.conf at session
# start, so a change made here (this script runs after the session's
# already up) never affects the *current* login, only the next one. Safe
# to run every time: only rewrites the file if its content would actually
# change, so it doesn't touch its mtime or spam anything on every login.
ext_env_dir="$HOME/.config/environment.d"
ext_env_file="$ext_env_dir/98-bernina-extensions.conf"
ext_base_dirs="${XDG_DATA_DIRS:-/usr/local/share/:/usr/share/}"
case ":${ext_base_dirs}:" in
    *:/sf/bernina/applications:*) ext_data_dirs="$ext_base_dirs" ;;      # already present -- don't re-prepend and grow forever
    *) ext_data_dirs="/sf/bernina/applications:${ext_base_dirs}" ;;
esac
ext_env_line="XDG_DATA_DIRS=$ext_data_dirs"
mkdir -p "$ext_env_dir"
if [ ! -f "$ext_env_file" ] || [ "$(cat "$ext_env_file")" != "$ext_env_line" ]; then
    printf '%s\n' "$ext_env_line" > "$ext_env_file"
    echo "startup_bernina_terminal: updated $ext_env_file -- log out/in (or reboot) once for it to take effect"
fi

dconf write /org/gnome/shell/extensions/gtile/resize21 "'3x3 0:2 0:2, 0:1 0:2, 0:2 1:2, 0:1 1:2'"
dconf write /org/gnome/shell/extensions/gtile/resize22 "'3x3 1:2 1:2, 1:1 1:2, 0:2 2:2, 0:1 2:2'"
dconf write /org/gnome/shell/extensions/gtile/resize23 "'3x3 2:2 2:2, 2:1 2:2, 1:2 2:2, 1:1 2:2'"
dconf write /org/gnome/shell/extensions/gtile/resize24 "'3x3 0:1 0:1, 0:0 0:2, 0:1 1:1, 0:0 1:2'"
dconf write /org/gnome/shell/extensions/gtile/resize25 "'3x3 1:1 1:1, 1:0 1:2, 0:1 2:1, 0:0 2:2'"
dconf write /org/gnome/shell/extensions/gtile/resize26 "'3x3 2:1 2:1, 2:0 2:2, 1:1 2:1, 1:0 2:2'"
dconf write /org/gnome/shell/extensions/gtile/resize27 "'3x3 0:0 0:0, 0:0 0:1, 0:0 1:0, 0:0 1:1'"
dconf write /org/gnome/shell/extensions/gtile/resize28 "'3x3 1:0 1:0, 1:0 1:1, 0:0 2:0, 0:0 2:1'"
dconf write /org/gnome/shell/extensions/gtile/resize29 "'3x3 2:0 2:0, 2:0 2:1, 1:0 2:0, 1:0 2:1'"
# preset-resize-21 (numpad-1's keybinding for the resize21 preset above)
# had a stray override missing its Shift modifier on saresb-cons-04 (bound
# to plain <Super>KP_1 instead of gTile's own default <Super><Shift>KP_1;
# every other preset-resize-2N sibling was untouched and still works fine)
# -- reset it back to gTile's own default rather than hardcoding a value
# here, so this self-heals regardless of what that default actually is.
# NOTE: `dconf reset`, not `gsettings reset` -- gTile's schema is a
# relocatable one bundled inside the extension itself and never registered
# system-wide, so plain `gsettings` (run outside gnome-shell's own process,
# which loads it internally) can't see it at all ("No such schema",
# confirmed by hand -- this line was silently failing every single run).
# `dconf reset` only touches the raw key/value store, no schema needed --
# gnome-shell's own GSettings wrapper then falls back to the compiled-in
# default the next time it reads the now-unset key.
dconf reset /org/gnome/shell/extensions/gtile/preset-resize-21

# Second gTile preset set: a 4x4 grid on <Super><Shift> + the right-hand
# key block, laid out to spatially match the keyboard (row of keys top to
# bottom -> row of screen quarters top to bottom, left to right likewise):
#   7 8 9 0
#   u i o p
#   j k l ;
#   m , . /
# gTile's schema only has 30 preset-resize-N/resizeN slots total (1-30);
# 21-29 are already used by the 3x3 numpad set above, so this reuses
# 1-16, which otherwise just sit at gTile's own factory defaults
# (<Super><Alt>KP_N / <Super><Control>KP_N, several already disabled by an
# old manual override found blank in dconf on both machines) -- nothing
# else here was using them.
#
# Each key cycles through 4 stages on repeated presses (gTile's own
# built-in cycling, `max-timeout` apart): its exact 4x4 cell, the 2x2
# quadrant it belongs to, the left or right half of the screen, then the
# full screen. There's no fifth stage for "two keys held together span a
# window" (e.g. 7 + , for the left half): mutter's global keybinding grab
# only ever matches one non-modifier key plus modifiers, so gTile (like
# any other shortcut here) cannot bind two simultaneously-held regular
# keys as one trigger -- the half-screen cycle stage above is the closest
# equivalent, reachable in one extra press of the same key.
dconf write /org/gnome/shell/extensions/gtile/resize1  "'4x4 0:0 0:0, 0:1 0:1, 0:1 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize2  "'4x4 1:1 0:0, 0:1 0:1, 0:1 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize3  "'4x4 2:2 0:0, 2:3 0:1, 2:3 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize4  "'4x4 3:3 0:0, 2:3 0:1, 2:3 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize5  "'4x4 0:0 1:1, 0:1 0:1, 0:1 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize6  "'4x4 1:1 1:1, 0:1 0:1, 0:1 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize7  "'4x4 2:2 1:1, 2:3 0:1, 2:3 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize8  "'4x4 3:3 1:1, 2:3 0:1, 2:3 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize9  "'4x4 0:0 2:2, 0:1 2:3, 0:1 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize10 "'4x4 1:1 2:2, 0:1 2:3, 0:1 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize11 "'4x4 2:2 2:2, 2:3 2:3, 2:3 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize12 "'4x4 3:3 2:2, 2:3 2:3, 2:3 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize13 "'4x4 0:0 3:3, 0:1 2:3, 0:1 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize14 "'4x4 1:1 3:3, 0:1 2:3, 0:1 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize15 "'4x4 2:2 3:3, 2:3 2:3, 2:3 0:3, 0:3 0:3'"
dconf write /org/gnome/shell/extensions/gtile/resize16 "'4x4 3:3 3:3, 2:3 2:3, 2:3 0:3, 0:3 0:3'"
# NOTE: `dconf write`, not `gsettings set` -- gTile's schema is bundled
# inside the extension and never registered system-wide, so plain
# `gsettings` (run outside gnome-shell's own process) can't see it at all
# ("No such schema", confirmed by hand). These 16 lines used to be
# `gsettings set` and were therefore silently failing every single run --
# the resize1-16 grid layouts above were being written fine (dconf write
# never needed the schema), but the keybindings meant to trigger them never
# actually got assigned, leaving preset-resize-1..16 at gTile's factory
# defaults (<Super><Alt>KP_N / <Super><Control>KP_N) instead of this
# right-hand block. This is almost certainly why this second preset set
# looked like it "didn't work" while the numpad set above (which only ever
# needed dconf write, no keybinding reassignment) did.
dconf write /org/gnome/shell/extensions/gtile/preset-resize-1  "['<Super><Shift>7']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-2  "['<Super><Shift>8']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-3  "['<Super><Shift>9']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-4  "['<Super><Shift>0']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-5  "['<Super><Shift>u']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-6  "['<Super><Shift>i']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-7  "['<Super><Shift>o']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-8  "['<Super><Shift>p']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-9  "['<Super><Shift>j']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-10 "['<Super><Shift>k']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-11 "['<Super><Shift>l']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-12 "['<Super><Shift>semicolon']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-13 "['<Super><Shift>m']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-14 "['<Super><Shift>comma']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-15 "['<Super><Shift>period']"
dconf write /org/gnome/shell/extensions/gtile/preset-resize-16 "['<Super><Shift>slash']"

# Tactile (a second, simpler grid-tiling extension alongside gTile above)
# is served the same way -- see the shared-tree/XDG_DATA_DIRS note near
# allow-extension-installation above. Pin its keyboard shortcuts to its
# own shipped defaults so they self-heal against the same kind of
# stray-override drift documented for gTile's preset-resize-21.
# `dconf reset`, not `gsettings reset` -- see the note on gTile's
# preset-resize-21 above: tactile's schema is bundled inside the extension
# and never registered system-wide, so plain `gsettings` can't see it.
dconf reset /org/gnome/shell/extensions/tactile/show-tiles
dconf reset /org/gnome/shell/extensions/tactile/hide-tiles
dconf reset /org/gnome/shell/extensions/tactile/show-settings
dconf reset /org/gnome/shell/extensions/tactile/next-monitor
dconf reset /org/gnome/shell/extensions/tactile/prev-monitor
dconf reset /org/gnome/shell/extensions/tactile/layout-1
dconf reset /org/gnome/shell/extensions/tactile/layout-2
dconf reset /org/gnome/shell/extensions/tactile/layout-3
dconf reset /org/gnome/shell/extensions/tactile/layout-4

# Tactile grid layout: 4 rows (grid-cols is already 4 by default, so this
# makes a 4x4 grid) with maximize-on-select turned off, and the tile
# activation keys remapped from Tactile's own qwerty-block defaults to the
# same "row of keys top to bottom -> row of screen quarters top to bottom"
# spatial layout already used for gTile's right-hand-block preset set
# above:
#   7 8 9 0
#   u i o p
#   j k l ;
#   m , . /
# tile-4-Y/5-Y/6-Y (columns beyond this 4x4) are left at their defaults
# (unbound) since nothing here uses them. layout-4-row-3 is bumped to 1
# (from its default 0) so the "layout-4" preset -- one of Tactile's four
# selectable layouts, cycled via the layout-1..4 keys -- also spans all 4
# rows of this now-4-row grid instead of just the first 3.
dconf write /org/gnome/shell/extensions/tactile/grid-rows 4
dconf write /org/gnome/shell/extensions/tactile/layout-4-row-3 1
dconf write /org/gnome/shell/extensions/tactile/maximize false
dconf write /org/gnome/shell/extensions/tactile/tile-0-0 "['7']"
dconf write /org/gnome/shell/extensions/tactile/tile-1-0 "['8']"
dconf write /org/gnome/shell/extensions/tactile/tile-2-0 "['9']"
dconf write /org/gnome/shell/extensions/tactile/tile-3-0 "['0']"
dconf write /org/gnome/shell/extensions/tactile/tile-0-1 "['u']"
dconf write /org/gnome/shell/extensions/tactile/tile-1-1 "['i']"
dconf write /org/gnome/shell/extensions/tactile/tile-2-1 "['o']"
dconf write /org/gnome/shell/extensions/tactile/tile-3-1 "['p']"
dconf write /org/gnome/shell/extensions/tactile/tile-0-2 "['j']"
dconf write /org/gnome/shell/extensions/tactile/tile-1-2 "['k']"
dconf write /org/gnome/shell/extensions/tactile/tile-2-2 "['l']"
dconf write /org/gnome/shell/extensions/tactile/tile-3-2 "['semicolon']"
dconf write /org/gnome/shell/extensions/tactile/tile-0-3 "['m']"
dconf write /org/gnome/shell/extensions/tactile/tile-1-3 "['comma']"
dconf write /org/gnome/shell/extensions/tactile/tile-2-3 "['period']"
dconf write /org/gnome/shell/extensions/tactile/tile-3-3 "['slash']"

# Which of the 4 layouts show-tiles (Super+T) displays is per-monitor
# state (monitor-N-layout, one key per physical monitor, default 1) that
# Tactile itself overwrites the moment you press layout-1..4 to switch --
# so this only sets the *initial* default before anyone's ever switched on
# a given monitor. Set to 4 here (our now-4-row grid above) rather than
# leaving Tactile's own default of 1 (its original, un-customized layout).
# Tactile's schema goes up to monitor-5-layout (6 monitors); set all of
# them rather than guessing how many are actually connected on a given
# console -- harmless no-op for any index beyond what's physically there.
dconf write /org/gnome/shell/extensions/tactile/monitor-0-layout 4
dconf write /org/gnome/shell/extensions/tactile/monitor-1-layout 4
dconf write /org/gnome/shell/extensions/tactile/monitor-2-layout 4
dconf write /org/gnome/shell/extensions/tactile/monitor-3-layout 4
dconf write /org/gnome/shell/extensions/tactile/monitor-4-layout 4
dconf write /org/gnome/shell/extensions/tactile/monitor-5-layout 4

# gnome terminal color profiles: install Solarized Dark/Light and Spacedust
# (plus a "1-Spacedust" copy that sorts to the top of the profile menu) if
# not already present, and set Spacedust as the default profile. Idempotent
# -- safe to run every login, only ever adds what's missing.
install_terminal_profiles

#desktop background default
dconf write /org/gnome/desktop/background/color-shading-type "'solid'"
dconf write /org/gnome/desktop/background/picture-options "'wallpaper'"
dconf write /org/gnome/desktop/background/picture-uri "'file:////usr/share/gnome-control-center/pixmaps/noise-texture-light.png'"
dconf write /org/gnome/desktop/background/primary-color "'#425265'"
dconf write /org/gnome/desktop/background/secondary-color "'#425265'"

# lock screen behavior
if [ "$(hostname)" = "saresb-cons-08.psi.ch" ]; then
    dconf write /org/gnome/desktop/screensaver/lock-enabled true
    dconf write /org/gnome/desktop/screensaver/lock-delay 'uint32 180'
  else
    dconf write /org/gnome/desktop/screensaver/lock-enabled false
    dconf write /org/gnome/desktop/session/idle-delay 'uint32 0'
fi

# Add cryo-gw to CA adress list for recovery air content
export EPICS_CA_ADDR_LIST="cryo-cagw:5062 "$EPICS_CA_ADDR_LIST
# Propagate it into the session's D-Bus activation environment too, so
# gnome-terminal windows opened later (which get their env from there,
# not from this script's own process) also see it -- needed since this
# script now runs once at login via autostart rather than being sourced
# into every interactive shell.
dbus-update-activation-environment --systemd EPICS_CA_ADDR_LIST

# Keep the Kerberos ticket (default: gac-bernina) alive for this login
# session; renews automatically once less than 24h validity remains, or
# prompts for the renew password if a silent renewal isn't possible.
# Safe to call repeatedly -- it locks itself to a single running instance.
nohup /sf/bernina/bin/kerberos_ticket_renew >> "$HOME/.kerberos_ticket_renew.log" 2>&1 &
disown

# Keep a local copy of the shared bernina pixi python env in ~/bpy in
# sync: installs it on first run (asking permission + checking disk space
# first), then re-syncs it whenever the shared env changes, checking every
# minute for as long as this terminal session stays open. Safe to call
# repeatedly -- it locks itself to a single running instance per host.
nohup /sf/bernina/bin/pixi_local_sync >> "$HOME/.pixi_local_sync.log" 2>&1 &
disown

# Start flameshot as a background tray daemon so it's already running
# before the first PrintScreen press (see the Flameshot custom-keybinding
# below, which just calls `flameshot gui` against this same running
# instance) -- its tray icon needs the appindicatorsupport extension
# enabled at the bottom of this script to actually show up.
nohup /sf/bernina/bin/flameshot >> "$HOME/.flameshot.log" 2>&1 &
disown

# Let the user know startup actually finished, since everything above ran
# silently in a terminal window they likely aren't watching.
command -v notify-send >/dev/null 2>&1 && notify-send -u low "Bernina Terminal" "Initialized Bernina control terminal successfully."

# make rhel9 text larger and rely on default dash, no extra cluttering dock.
dconf write /org/gnome/desktop/interface/text-scaling-factor 1.25

# dock individualism
# lock screen behavior
if [ "$(hostname)" = "saresb-cons-03.psi.ch" ]; then
    dconf write /org/gnome/shell/enabled-extensions "['appindicatorsupport@rgcjonas.gmail.com', 'gTile@vibou', 'tactile@lundal.io', 'dash-to-panel@gnome-shell-extensions.gcampax.github.com']"
    dconf write /org/gnome/shell/disabled-extensions "['dash-to-dock@gnome-shell-extensions.gcampax.github.com']"
  else
    dconf write /org/gnome/shell/enabled-extensions "['appindicatorsupport@rgcjonas.gmail.com', 'gTile@vibou', 'tactile@lundal.io']"
    dconf write /org/gnome/shell/disabled-extensions "['dash-to-dock@gnome-shell-extensions.gcampax.github.com']"
fi
