#!/bin/sh

# support for i18n and L10n
. gettext.sh
export TEXTDOMAIN=install-software-upgrades

# Playing with locks and calling dconf update while a GNOME session is running
# leads to all kinds of strange behaviour and crashes (seen in Debian 8 with
# GNOME 3.14). Therefore we leave the code in place but comment the actual
# calls to it.

unlock_dconf()
{
	# try unlocking the desktop background
	if grep -q "/org/gnome/desktop/background/" /etc/dconf/db/local.d/locks/*
	then
		LOCK_TMP_DIR="$(mktemp -d)"
		echo "LOCK_TMP_DIR=\"${LOCK_TMP_DIR}\""
		for i in $(grep -l "/org/gnome/desktop/background/" /etc/dconf/db/local.d/locks/*)
		do
			echo "temporarily disabling $i"
			sudo mv "$i" ${LOCK_TMP_DIR}/
		done
		sudo dconf update
	fi
}

lock_dconf()
{
	if [ -n "${LOCK_TMP_DIR}" ]
	then
		sudo mv "${LOCK_TMP_DIR}"/* /etc/dconf/db/local.d/locks/
		rmdir "${LOCK_TMP_DIR}"
		sudo dconf update
	fi
}

lock_desktop()
{
	if [ "${XDG_CURRENT_DESKTOP}" = "GNOME" ]
	then
		# remember current GNOME settings
		PICTURE_OPTIONS="$(gsettings get org.gnome.desktop.background picture-options)"
		echo "PICTURE_OPTIONS=\"${PICTURE_OPTIONS}\""
		PICTURE_URI="$(gsettings get org.gnome.desktop.background picture-uri)"
		echo "PICTURE_URI=\"${PICTURE_URI}\""
		PRIMARY_COLOR="$(gsettings get org.gnome.desktop.background primary-color)"
		echo "PRIMARY_COLOR=\"${PRIMARY_COLOR}\""
		DISABLE_LOCK_SCREEN="$(gsettings get org.gnome.desktop.lockdown disable-lock-screen)"
		echo "DISABLE_LOCK_SCREEN=\"${DISABLE_LOCK_SCREEN}\""
		DISABLE_LOG_OUT="$(gsettings get org.gnome.desktop.lockdown disable-log-out)"
		echo "DISABLE_LOG_OUT=\"${DISABLE_LOG_OUT}\""
		IDLE_DELAY="$(gsettings get org.gnome.desktop.session idle-delay)"
		echo "IDLE_DELAY=\"${IDLE_DELAY}\""
		BUTTON_LAYOUT="$(gsettings get org.gnome.desktop.wm.preferences button-layout)"
		echo "BUTTON_LAYOUT=\"${BUTTON_LAYOUT}\""
		IDLE_DIM="$(gsettings get org.gnome.settings-daemon.plugins.power idle-dim)"
		echo "IDLE_DIM=\"${IDLE_DIM}\""

		# TODO: uncomment in later version if GNOME handles calls to "dconf update" at
		# runtime without crashes
		#unlock_dconf

		# change GNOME settings to "kiosk mode"
		gsettings set org.gnome.desktop.background picture-options "'wallpaper'"
		gsettings set org.gnome.desktop.background picture-uri "'file:////usr/share/gnome-control-center/pixmaps/noise-texture-light.png'"
		gsettings set org.gnome.desktop.background primary-color "'#FF0000'"
		gsettings set org.gnome.desktop.lockdown disable-lock-screen true
		gsettings set org.gnome.desktop.lockdown disable-log-out true
		gsettings set org.gnome.desktop.session idle-delay "uint32 0"
		gsettings set org.gnome.desktop.wm.preferences button-layout "''"
		gsettings set org.gnome.settings-daemon.plugins.power idle-dim false

		# Locking the dconf database here in between is not useful as it would
		# instantly fall back to the (default) keys in the system database instead of
		# using the values in the user's database we just set with all the gsettings
		# calls above.
	fi
}

unlock_desktop()
{
	if [ "${XDG_CURRENT_DESKTOP}" = "GNOME" ]
	then
		# restore previous GNOME settings
		gsettings set org.gnome.desktop.background picture-options "${PICTURE_OPTIONS}"
		gsettings set org.gnome.desktop.background picture-uri "${PICTURE_URI}"
		gsettings set org.gnome.desktop.background primary-color "${PRIMARY_COLOR}"
		gsettings set org.gnome.desktop.lockdown disable-lock-screen "${DISABLE_LOCK_SCREEN}"
		gsettings set org.gnome.desktop.lockdown disable-log-out "${DISABLE_LOG_OUT}"
		gsettings set org.gnome.desktop.session idle-delay "${IDLE_DELAY}"
		gsettings set org.gnome.desktop.wm.preferences button-layout "${BUTTON_LAYOUT}"
		gsettings set org.gnome.settings-daemon.plugins.power idle-dim "${IDLE_DIM}"

		# TODO: uncomment in later version if GNOME handles calls to "dconf update" at
		# runtime without crashes
		#lock_dconf
	fi
}
#!/bin/sh

system_is_plugged_in() {
	for i in /sys/class/power_supply/*
	do
		if [ "$(cat $i/type)" = "Mains" -a "$(cat $i/online 2>/dev/null)" = "1" ]
		then
			# we found a power supply that is *NOT* a battery
			# and it is "online" (aka plugged in)
			return 0
		fi
	done
	return 1
}

if system_is_plugged_in
then
	POWER_MESSAGE="$(gettext "Don't unplug the power supply of your computer!")"
else
	POWER_MESSAGE="$(gettext "Connect a power supply to your computer!")"
fi

# TODO: remove --no-wrap option once zenity is fixed, see
# https://bugs.launchpad.net/ubuntu/+source/zenity/+bug/1206760
zenity \
	--no-wrap \
	--warning \
	--text="$(gettext "The upgrades will be running in a terminal window.\nDon't close this window!")\n${POWER_MESSAGE}\n$(gettext "Don't turn off the computer!")"

case $DESKTOP_SESSION in

  cinnamon|gnome*)
    TERMINAL_COMMAND="gnome-terminal --maximize --wait --"
    ;;

  enlightenment)
    TERMINAL_COMMAND="terminology -e"
    ;;

  LXDE)
    TERMINAL_COMMAND="lxterminal -e"
    ;;

  mate)
    TERMINAL_COMMAND="mate-terminal -e"
    ;;

  plasma)
    TERMINAL_COMMAND="konsole -e"
    ;;

  xfce)
    TERMINAL_COMMAND="xfce4-terminal -e"
    ;;

  *)
    TERMINAL_COMMAND="xterm -e"
    ;;
esac

# If the system crashes after we lock the desktop most users are unable to
# return to the normal settings. Therefore we are let the lock/unlock feature
# disabled for now...
#lock_desktop
systemd-inhibit \
	--what="shutdown:sleep:idle:handle-power-key:handle-suspend-key:handle-hibernate-key:handle-lid-switch" \
	--who="upgrade installation script" \
	--why="installing upgrades" \
	pkexec package-management-wrapper_upgrades "$TERMINAL_COMMAND"
#unlock_desktop

# TODO: remove --no-wrap option once zenity is fixed, see
# https://bugs.launchpad.net/ubuntu/+source/zenity/+bug/1206760
zenity \
	--no-wrap \
	--info \
	--text="$(gettext "The upgrade process finished.")"
