#!/bin/bash
set -euxo pipefail

BACKGROUND_WALLPAPER_URI="file://$1"
shift

function _dconf {
    command='eval $(dbus-launch --sh-syntax); dconf '"$@"'; export a=$?; kill "$DBUS_SESSION_BUS_PID"; exit $a'
    sudo -H -u zoox bash -c "$command"
}

function _gsettings {
    command='eval $(dbus-launch --sh-syntax); gsettings '"$@"'; set a=$?; kill "$DBUS_SESSION_BUS_PID"; exit $a'
    sudo -H -u zoox bash -c "$command"
}

# Background wallpaper
_dconf write "/org/gnome/desktop/background/picture-uri" "\'$BACKGROUND_WALLPAPER_URI\'"
_dconf write "/org/gnome/desktop/background/picture-options" "\'stretched\'"

terminal_profile=$(_gsettings get org.gnome.Terminal.ProfilesList default | sed "s/'//g")
_dconf write "/org/gnome/terminal/legacy/profiles:/:${terminal_profile}/scrollback-unlimited" true
_dconf write "/org/gnome/terminal/legacy/profiles:/:${terminal_profile}/use-system-font" false

# Terminal background
_dconf write "/org/gnome/terminal/legacy/profiles:/:${terminal_profile}/use-theme-colors" false
_dconf write "/org/gnome/terminal/legacy/profiles:/:${terminal_profile}/foreground-color" "\'rgb\(255,255,255\)\'"
_dconf write "/org/gnome/terminal/legacy/profiles:/:${terminal_profile}/background-color" "\'rgb\(0,0,0\)\'"

_dconf write "/org/gnome/desktop/input-sources/xkb-options" "[\'caps:ctrl_modifier\']"

_dconf write "/org/gnome/desktop/peripherals/touchpad/click-method" "\'fingers\'"

# Sleep timeout
_dconf write "/org/gnome/desktop/session/idle-delay" \"uint32 0\"
_dconf write "/org/gnome/settings-daemon/plugins/power/idle-dim" "\'false\'"

_dconf write "/org/gnome/settings-daemon/plugins/power/sleep-inactive-ac-type" "\'suspend\'"
_dconf write "/org/gnome/settings-daemon/plugins/power/sleep-inactive-battery-type" "\'suspend\'"

_dconf write "/org/gnome/settings-daemon/plugins/power/sleep-inactive-ac-timeout" 3600
_dconf write "/org/gnome/settings-daemon/plugins/power/sleep-inactive-battery-timeout" 3600

# Trackpad
_gsettings set org.gnome.desktop.peripherals.touchpad tap-to-click false
