#!/bin/bash
#
# FIXME: This is so far just a sketch until there is a proper mechanism for customizing GNOME
# during installation. This script collects all necessary customizations in one place.
# Currently, this is run from ~/.config/autostart/reform-gnome-config.desktop
# To only run once, it could remove this .desktop file at the end.
#
# original lists generated by:
# gsettings list-recursively | grep -E "(shell\.keybindings|wm\.keybindings|wayland\.keybindings)

usage() {
  echo "Reset parts of GNOME configuration (like keyboard shortcuts and some window behavior) to the defaults customized for MNT Reform Series devices." >&2
}

if [ "$#" -gt 0 ] && [ "$1" = "--help" ]; then
  usage
  exit 0
fi

# disable "Automatic Screen Blank"
gsettings set org.gnome.desktop.session idle-delay 0
# Make EURkey layout and compose work
gsettings set org.gnome.desktop.input-sources show-all-sources true
gsettings set org.gnome.desktop.input-sources xkb-options "['compose:rctrl', 'lv3:ralt_switch']"
# Don't attach modal windows, it's annoying when some windows can't be moved individually
gsettings set org.gnome.mutter attach-modal-dialogs false
# this is hogging Super+Escape by default
gsettings set org.gnome.mutter.wayland.keybindings restore-shortcuts "@as []"
gsettings set org.gnome.settings-daemon.plugins.media-keys help "@as []"
# these are hogging super+numbers / super+shift numbers by default
gsettings set org.gnome.shell.keybindings switch-to-application-1 "@as []"
gsettings set org.gnome.shell.keybindings switch-to-application-2 "@as []"
gsettings set org.gnome.shell.keybindings switch-to-application-3 "@as []"
gsettings set org.gnome.shell.keybindings switch-to-application-4 "@as []"
gsettings set org.gnome.shell.keybindings switch-to-application-5 "@as []"
gsettings set org.gnome.shell.keybindings switch-to-application-6 "@as []"
gsettings set org.gnome.shell.keybindings switch-to-application-7 "@as []"
gsettings set org.gnome.shell.keybindings switch-to-application-8 "@as []"
gsettings set org.gnome.shell.keybindings switch-to-application-9 "@as []"
gsettings set org.gnome.shell.keybindings open-new-window-application-1 "@as []"
gsettings set org.gnome.shell.keybindings open-new-window-application-2 "@as []"
gsettings set org.gnome.shell.keybindings open-new-window-application-3 "@as []"
gsettings set org.gnome.shell.keybindings open-new-window-application-4 "@as []"
gsettings set org.gnome.shell.keybindings open-new-window-application-5 "@as []"
gsettings set org.gnome.shell.keybindings open-new-window-application-6 "@as []"
gsettings set org.gnome.shell.keybindings open-new-window-application-7 "@as []"
gsettings set org.gnome.shell.keybindings open-new-window-application-8 "@as []"
gsettings set org.gnome.shell.keybindings open-new-window-application-9 "@as []"

# MNT Reform customizations
## window actions
gsettings set org.gnome.desktop.wm.keybindings toggle-fullscreen "['<Super>f']"
gsettings set org.gnome.desktop.wm.keybindings close "['<Super>Escape']"
gsettings set org.gnome.desktop.wm.keybindings maximize "['<Super>Up']"
gsettings set org.gnome.desktop.wm.keybindings unmaximize "['<Super>Down']"
## window switcher
gsettings set org.gnome.desktop.wm.keybindings switch-applications "['<Super>Tab']"
gsettings set org.gnome.desktop.wm.keybindings switch-applications-backward "['<Super><Shift>Tab']"
gsettings set org.gnome.desktop.wm.keybindings switch-group "['<Alt>Tab']"
gsettings set org.gnome.desktop.wm.keybindings switch-group-backward "['<Alt><Shift>Tab']"
## workspaces
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-1 "['<Super>1']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-10 "['<Super>0']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-2 "['<Super>2']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-3 "['<Super>3']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-4 "['<Super>4']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-5 "['<Super>5']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-6 "['<Super>6']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-7 "['<Super>7']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-8 "['<Super>8']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-9 "['<Super>9']"
gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-1 "['<Super><Shift>1']"
gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-10 "['<Super><Shift>0']"
gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-2 "['<Super><Shift>2']"
gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-3 "['<Super><Shift>3']"
gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-4 "['<Super><Shift>4']"
gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-5 "['<Super><Shift>5']"
gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-6 "['<Super><Shift>6']"
gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-7 "['<Super><Shift>7']"
gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-8 "['<Super><Shift>8']"
gsettings set org.gnome.desktop.wm.keybindings move-to-workspace-9 "['<Super><Shift>9']"
## screenshots
gsettings set org.gnome.shell.keybindings screenshot "['<Shift><Super>s']"
gsettings set org.gnome.shell.keybindings show-screenshot-ui "['<Shift><Super>x']"
gsettings set org.gnome.shell.keybindings show-screen-recording-ui "['<Shift><Super>r']"
## media keys
gsettings set org.gnome.settings-daemon.plugins.media-keys keyboard-brightness-down-static "@as []"
gsettings set org.gnome.settings-daemon.plugins.media-keys keyboard-brightness-up-static "@as []"
gsettings set org.gnome.shell.keybindings screen-brightness-down "['XF86MonBrightnessDown', '<Super>F1']"
gsettings set org.gnome.shell.keybindings screen-brightness-up "['XF86MonBrightnessUp', '<Super>F2']"
## launchers
dconf load /org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/ <<EOF
[custom0]
binding='<Super>Return'
command='xdg-terminal-exec'
name='Launch Terminal'
EOF
gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/']"
## window tweaks (more buttons, move+resize with super+drag)
gsettings set org.gnome.desktop.wm.preferences action-double-click-titlebar 'toggle-maximize'
gsettings set org.gnome.desktop.wm.preferences button-layout 'appmenu:minimize,maximize,close'
gsettings set org.gnome.desktop.wm.preferences mouse-button-modifier '<Super>'
gsettings set org.gnome.desktop.wm.preferences resize-with-right-button true
## extensions
gsettings set org.gnome.shell enabled-extensions "['dash-to-dock@micxgx.gmail.com']"
gsettings set org.gnome.shell.extensions.dash-to-dock hot-keys false
gsettings set org.gnome.shell.extensions.dash-to-dock disable-overview-on-startup true
## disable standby (the GDM part is set in /etc/gdm3/greeter.dconf-defaults)
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing'
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type 'nothing'
gsettings set org.gnome.settings-daemon.plugins.power power-button-action 'interactive'
gsettings set org.gnome.desktop.interface show-battery-percentage true
## fonts
gsettings set org.gnome.desktop.interface monospace-font-name 'JetBrains Mono 12'
gsettings set org.gnome.desktop.interface gtk-theme 'Adwaita'
gsettings set org.gnome.desktop.interface icon-theme 'Adwaita'
gsettings set org.gnome.desktop.background picture-uri 'file:///usr/share/backgrounds/mnt-reform-next-y2k.jpg'
gsettings set org.gnome.desktop.background picture-uri-dark 'file:///usr/share/backgrounds/mnt-reform-next-y2k.jpg'
## disable automatic downloads consuming bandwidth and noisy notifications about updates
gsettings set org.gnome.software allow-updates false
gsettings set org.gnome.software download-updates false
gsettings set org.gnome.software download-updates-notify false
