#!/bin/sh
# -*- mode: shell-script; coding: utf-8 -*-
#
# elho-gnome-config
#
# Script to set some sane GNOME GConf settings
#
# Copyright (C) 2010-2019 Elmar Hoffmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

set -e

HOMEPAGE_ELHO='http://www.elho.net/'
HOMEPAGE_LUSER='http://www.google.com/'

. /lib/elho/shell-tools

if which gconftool-2 > /dev/null; then
    gconf_set ()
    {
	local key="$1"
	local type="$2"
	local value="$3"

	case "${type}" in
	    list-*)
		type=$(echo "${type}" | sed --quiet --expression='s/^list-//p')
		gconftool-2 --set "${key}" --type 'list' --list-type="${type}" "${value}"
		;;
	    *)
		gconftool-2 --set "${key}" --type "${type}" "${value}"
		;;
	esac
    }
else
    warn "gconftool-2 not installed, not setting gconf values!"
    gconf_set ()
    {
	:
    }
fi

usage ()
{
    echo "Usage: $(basename -- "$0") [--shared] [--luser]"
}

set +e
commandline=$(getopt --name "$(basename -- "$0"): Error" \
    --options='?h' --longoptions='help,usage,shared,luser' -- "$@")
if [ $? -ne 0 ]; then
        usage
        exit 2
    exit
fi
set -e
eval set -- "${commandline}"
shared=0
luser=0
while true; do
    case "$1" in
        -\?|-h|--help|--usage)
            usage
            exit
            ;;
        --shared)
            shared=1
            ;;
        --luser)
            luser=1
            ;;
        --)
            shift
            break
            ;;
        *)
            error "Invalid getopt(1) output."
            exit 1
            ;;
    esac
    shift
done
if [ $# -ne 0 ]; then
        usage
        exit 2
    exit
fi

if [ "${luser}" -eq 0 ]; then
    homepage="${HOMEPAGE_ELHO}"
else
    homepage="${HOMEPAGE_LUSER}"
fi

YRESOLUTION=''
if [ -n "${DISPLAY}" ]; then
    YRESOLUTION=$(xdpyinfo | sed --quiet --expression='s/^[[:space:]]\+dimensions:[[:space:]]\+[[:digit:]]\+x\([[:digit:]]\+\) pixels.*$/\1/p')
fi
if [ -z "${YRESOLUTION}" ]; then
    warn 'Unable to determine vertical display resolution!'
fi

# GNOME settings
if [ "${XDG_CURRENT_DESKTOP}" = 'GNOME' ]; then
    gsettings set org.gnome.desktop.interface cursor-blink false
    gsettings set org.gnome.desktop.interface gtk-key-theme "'Emacs'"

    gsettings set org.gnome.desktop.interface cursor-theme "'redglass'"

    gsettings set org.gnome.desktop.interface gtk-theme "'Adwaita'"
    gsettings set org.gnome.desktop.wm.preferences theme "'Adwaita'"
    gsettings set org.gnome.desktop.interface icon-theme "'gnome'"

    # accessibility (universal access) settings
    gsettings set org.gnome.desktop.a11y.magnifier caret-tracking "'push'"
    gsettings set org.gnome.desktop.a11y.magnifier mouse-tracking "'push'"
    gsettings set org.gnome.settings-daemon.plugins.media-keys magnifier "'<Super>z'"
    gsettings set org.gnome.settings-daemon.plugins.media-keys magnifier-zoom-in "'<Super>equal'"
    gsettings set org.gnome.settings-daemon.plugins.media-keys magnifier-zoom-out "'<Super>minus'"
    
    # panel settings
    gsettings set org.gnome.desktop.interface clock-format "'24h'"
    gsettings set org.gnome.desktop.interface clock-show-date true
    gsettings set org.gnome.desktop.interface clock-show-seconds true

    gsettings set org.gnome.shell.extensions.openweather unit "'celsius'"
    gsettings set org.gnome.shell.extensions.openweather wind-speed-unit "'kph'"
    gsettings set org.gnome.shell.extensions.openweather pressure-unit "'hPa'"

    # pgp settings
    gsettings set org.gnome.crypto.pgp ascii-armor true
    gsettings set org.gnome.crypto.pgp encrypt-to-self true
    gsettings set org.gnome.crypto.pgp keyservers "['hkp://pool.sks-keyservers.net','hkp://keys.gnupg.net','hkp://subkeys.pgp.net','hkp://pgp.mit.edu','ldap://keyserver.pgp.com']"

    # Terminal settings
    gsettings set org.gnome.Terminal.Legacy.Settings menu-accelerator-enabled false
    gsettings set org.gnome.Terminal.Legacy.Settings mnemonics-enabled false

    profile="$(gsettings get org.gnome.Terminal.ProfilesList default | sed --expression="s/^'//;s/'$//")"
    gsettings set "org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:${profile}/" use-theme-colors true
    gsettings set "org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:${profile}/" scrollback-lines 8192

    gsettings set 'org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/' help "'disabled'"
    gsettings set 'org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/' switch-to-tab-1 "'disabled'"
    gsettings set 'org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/' switch-to-tab-2 "'disabled'"
    gsettings set 'org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/' switch-to-tab-3 "'disabled'"
    gsettings set 'org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/' switch-to-tab-4 "'disabled'"
    gsettings set 'org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/' switch-to-tab-5 "'disabled'"
    gsettings set 'org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/' switch-to-tab-6 "'disabled'"
    gsettings set 'org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/' switch-to-tab-7 "'disabled'"
    gsettings set 'org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/' switch-to-tab-8 "'disabled'"
    gsettings set 'org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/' switch-to-tab-9 "'disabled'"
    gsettings set 'org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/' switch-to-tab-10 "'disabled'"

    # Nautilus settings
    if [ "${luser}" -eq 0 ]; then
	gsettings set org.gnome.nautilus.preferences show-delete-permanently true
    else
	gsettings set org.gnome.nautilus.preferences show-delete-permanently false
    fi
    gsettings set org.gnome.desktop.media-handling automount-open false

# MATE settings
elif [ "${XDG_CURRENT_DESKTOP}" = 'MATE' ]; then
    gsettings set org.mate.interface cursor-blink false
    gsettings set org.mate.interface gtk-key-theme "'Emacs'"

    gsettings set org.mate.peripherals-mouse cursor-theme "'redglass'"

    gsettings set org.mate.NotificationDaemon theme "'standard'"

    gsettings set org.gnome.desktop.interface gtk-theme "'Adwaita'"
    gsettings set org.mate.interface gtk-theme "'Adwaita'"
    gsettings set org.gnome.desktop.wm.preferences theme "'TraditionalOk'"
    gsettings set org.gnome.desktop.interface icon-theme "'mate'"
    gsettings set org.mate.interface icon-theme "'gnome'"

    if [ "${luser}" -eq 0 ]; then
	gsettings set org.mate.background picture-filename "''"
    else
	gsettings set org.mate.background picture-filename "'/usr/share/images/desktop-base/desktop-background'"
	gsettings set org.mate.background picture-options "'stretched'"
    fi
    gsettings set org.mate.background color-shading-type "'vertical-gradient'"
    gsettings set org.mate.background primary-color "'#39374B'"
    gsettings set org.mate.background secondary-color "'#42528F'"

    gsettings set org.mate.peripherals-keyboard-xkb.general group-per-window false

    # panel settings
    gsettings set org.mate.panel.toplevel:/org/mate/panel/toplevels/bottom/ size 24
    gsettings set org.mate.panel.applet.clock:/org/mate/panel/objects/clock/prefs/ format "'24-hour'"
    gsettings set org.mate.panel.applet.clock:/org/mate/panel/objects/clock/prefs/ show-date true
    gsettings set org.mate.panel.applet.clock:/org/mate/panel/objects/clock/prefs/ show-seconds true
    gsettings set org.mate.panel.applet.clock:/org/mate/panel/objects/clock/prefs/ speed-unit "'km/h'"
    gsettings set org.mate.panel.applet.clock:/org/mate/panel/objects/clock/prefs/ temperature-unit "'Centigrade'"

    # Terminal settings
    gsettings set org.mate.terminal.global use-menu-accelerators false
    gsettings set org.mate.terminal.global use-mnemonics false

    profile="$(gsettings get org.mate.terminal.global default-profile | sed --expression="s/^'//;s/'$//")"
    
    gsettings set "org.mate.terminal.profile:/org/mate/terminal/profiles/${profile}/" background-color "'#DCDAD5'"
    gsettings set "org.mate.terminal.profile:/org/mate/terminal/profiles/${profile}/" use-theme-colors false
    gsettings set "org.mate.terminal.profile:/org/mate/terminal/profiles/${profile}/" palette "'#000000:#AA0000:#008200:#AA5500:#0000A0:#AA00AA:#00AAAA:#AAAAAA:#555555:#FF5555:#42C842:#FFFF55:#4C4CE6:#FF55FF:#55FFFF:#FFFFFF'"
    gsettings set "org.mate.terminal.profile:/org/mate/terminal/profiles/${profile}/" scrollback-lines 8192

    gsettings set org.mate.terminal.keybindings help "'disabled'"
    gsettings set org.mate.terminal.keybindings switch-to-tab-1 "'disabled'"
    gsettings set org.mate.terminal.keybindings switch-to-tab-2 "'disabled'"
    gsettings set org.mate.terminal.keybindings switch-to-tab-3 "'disabled'"
    gsettings set org.mate.terminal.keybindings switch-to-tab-4 "'disabled'"
    gsettings set org.mate.terminal.keybindings switch-to-tab-5 "'disabled'"
    gsettings set org.mate.terminal.keybindings switch-to-tab-6 "'disabled'"
    gsettings set org.mate.terminal.keybindings switch-to-tab-7 "'disabled'"
    gsettings set org.mate.terminal.keybindings switch-to-tab-8 "'disabled'"
    gsettings set org.mate.terminal.keybindings switch-to-tab-9 "'disabled'"
    gsettings set org.mate.terminal.keybindings switch-to-tab-10 "'disabled'"

    # Marco settings
    for theme in 'TraditionalOk' 'Industrial' 'Metabox'; do
	if [ -d "/usr/share/themes/${theme}/metacity-1" ]; then
	    gsettings set org.mate.Marco.general theme "'${theme}'"
	    break
	fi
    done

    # XScreensaver
    xdg-desktop-menu uninstall --mode user /usr/share/applications/xscreensaver-properties.desktop

    # Screensaver settings
    gsettings set org.mate.screensaver lock-delay 5
    gsettings set org.mate.screensaver cycle-delay 2147483647
    gsettings set org.mate.screensaver mode "'single'"
    if [ "${luser}" -eq 0 ]; then
	if [ -n "${YRESOLUTION}" ]; then
	    if [ "${YRESOLUTION}" -gt 854 ]; then
	        gsettings set org.mate.screensaver themes "['screensavers-phosphor-small']"
	    else
	        gsettings set org.mate.screensaver themes "['screensavers-phosphor-tiny']"
	    fi
	fi
    else
        gsettings set org.mate.screensaver themes "['screensavers-footlogo-floaters']"
    fi
    if [ "${shared}" -eq 0 ]; then
        gsettings set org.mate.screensaver user-switch-enabled false
    else
        gsettings set org.mate.screensaver user-switch-enabled true
    fi

    # Caja settings
    if [ "${luser}" -eq 0 ]; then
	gsettings set org.mate.caja.preferences enable-delete true
    else
	gsettings set org.mate.caja.preferences enable-delete false
    fi
    gsettings set org.mate.media-handling automount-open false

else
    warn 'Warning: Environment variable XDG_CURRENT_DESKTOP not set, unable to determine desktop evnivronment!'
fi

/lib/elho/add-gnome-keyboard-shortcut 'Save screen' 'xdg-screensaver activate' '<Control><Alt>s'

# Legacy GNOME settings
gconf_set /desktop/gnome/interface/cursor_blink bool false
gconf_set /desktop/gnome/interface/gtk_key_theme string 'Emacs'

gconf_set /desktop/gnome/peripherals/mouse/cursor_theme string 'redglass'

gconf_set /desktop/gnome/interface/gtk_theme string 'Raleigh'
gconf_set /desktop/gnome/interface/icon_theme string 'gnome'

# gksu settings
gconf_set /apps/gksu/save-to-keyring bool false
gconf_set /apps/gksu/force-grab bool true
gconf_set /apps/gksu/sudo-mode bool true

# Revelation settings
gconf_set /apps/revelation/clipboard/chain_username bool true
gconf_set /apps/revelation/file/autoload bool true
gconf_set /apps/revelation/file/autoload_file string '/home/elho/logins'
gconf_set /apps/revelation/file/autolock bool false
if [ "${luser}" -eq 0 ]; then
    gconf_set /apps/revelation/launcher/ftp string 'gnome-terminal -x lftp %(-u %u%(,%p%)%) %h%(:%o%)'
fi
gconf_set /apps/revelation/passwordgen/length int 10
gconf_set /apps/revelation/view/passwords bool false

# Totem settings
gsettings set org.gnome.totem autoload-subtitles true
gsettings set org.gnome.totem repeat true
gsettings set org.gnome.totem subtitle-font "'Verdana Bold 20'"
