#!/bin/bash

# gettext support
. gettext.sh
export TEXTDOMAIN=lernstick_autostart

show_dialog ()
{
	TEXT="${1}"

	# set the "always on top" property to our dialog so that it doesn't
	# get overlooked when other windows are opened very fast after login
	if [ "$KDE_FULL_SESSION" ]
	then
		DIALOG_WM_CLASS="kdialog"
	else
		DIALOG_WM_CLASS="zenity"
	fi
	(while true; do wmctrl -x -r $DIALOG_WM_CLASS -b add,above && exit; sleep 0.3; done) &

	if [ "$KDE_FULL_SESSION" ]
	then
		kdialog --msgbox "${TEXT}"
	else
		# zenity doesn't understand HTML but Pango Markup Language, see:
		# https://developer.gnome.org/pygtk/stable/pango-markup-language.html
		# Therefore we need to replace "<br>" with "\\n".
		ZENITY_TEXT="${TEXT//<br>/\\n}"
		echo "ZENITY_TEXT=\"${ZENITY_TEXT}\""
		zenity --no-wrap --info --text "${ZENITY_TEXT}"
	fi

}

run_desktop_file()
{
	DESKTOP_FILE="${1}"

	if [ -e "${DESKTOP_FILE}" ]
	then
		if [ "$KDE_FULL_SESSION" ]
		then
			kioclient5 exec "${DESKTOP_FILE}"
		else
			BASE_NAME="$(basename "${DESKTOP_FILE}")"
			SHORT_NAME="${BASE_NAME%.desktop}"
			gtk-launch "${SHORT_NAME}"
		fi
	else
		echo "desktop file ${DESKTOP_FILE} does not exist!"
	fi
}

disable_software_upgrades()
{
	gsettings set org.gnome.software download-updates false
}

CONFIG_FILE="/etc/lernstickWelcome"

# start installer or welcome program
COW=$(grep -e "/lib/live/mount/persistence" -e "/run/live/persistence/" /proc/mounts | grep -v ^tmpfs | grep -v ^aufs | grep -v ^overlay)
echo "COW: \"${COW}\""

if echo "${COW}" | grep -q "rw,"
then
	echo "data partition is in read-write mode -> try starting lernstickWelcome..."
	if [ -e ${CONFIG_FILE} ]
	then
		if grep -q "ShowWelcome=true" ${CONFIG_FILE}
		then
			echo "user selected lernstickWelcome startup"
			START="true"
		elif grep -q "ShowWelcome=false" ${CONFIG_FILE}
		then
			echo "user disabled lernstickWelcome start in config file"
		else
			echo "no ShowWelcome key in lernstickWelcome config file -> default start"
			START="true"
		fi
	else
		echo "no lernstickWelcome config file -> default start"
		START="true"
	fi

	if [ -n "${START}" ]
	then
		run_desktop_file /usr/share/applications/ch.fhnw.lernstickwelcome.controller.welcomeapplication.desktop
	fi

elif echo "${COW}" | grep -q "ro,"
then
	disable_software_upgrades

	echo "data partition is in read-only mode -> try showing information dialog..."
	if [ -e ${CONFIG_FILE} ]
	then
		if grep -q "ShowReadOnlyInfo=true" ${CONFIG_FILE}
		then
			echo "user selected information dialog startup"
			START="true"
		fi
	else
		echo "no lernstickWelcome config file -> default start"
		START="true"
	fi

	if [ -n "${START}" ]
	then
		TEXT="$(gettext "The data partition is in <b>read-only mode</b>.<br><br>This means that all modifications you might have done previously<br>are visible but all modifications you will do now are <b>lost</b><br>when you shut down or restart the system.<br><br>(If you now change files and want to keep them, you need to<br>save them on the exchange partition or other <b>external media</b>.)")"
		show_dialog "${TEXT}"
	fi

else
	# data partition is not used, we can NOT install additional programs
	# but we can copy the system

	disable_software_upgrades

	echo "data partition is NOT used"
	if [ -e ${CONFIG_FILE} ]
	then
		if grep -q "ShowNotUsedInfo=true" ${CONFIG_FILE}
		then
			# show information dialog before starting copy program
			TEXT="$(gettext "There is <b>no data partition in use</b>.<br><br>This means that all modifications you might have done previously<br>are now <b>invisible</b> and all modifications you will do now are <b>lost</b><br>when you shut down or restart the system.<br><br>(If you now change files and want to keep them, you need to<br>save them on the exchange partition or other <b>external media</b>.)")"
			show_dialog "${TEXT}"
		else
			echo "info dialog not configured in config file"
		fi

		if grep -q "AutoStartInstaller=true" ${CONFIG_FILE}
		then
			echo "persistency disabled -> try starting dlcopy..."
			run_desktop_file /usr/share/applications/ch-fhnw-dlcopy-dlcopy.desktop
		else
			echo "installer autostart not configured in config file"
		fi
	else
		echo "no lernstickWelcome config file -> no dialog and autostart"
	fi
fi
