#!/bin/sh

### BEGIN INIT INFO
# Provides:          fll-gnome-desktop
# Required-Start:    $local_fs $remote_fs fll-adduser
# Required-Stop:
# X-Start-Before:    fll-common-desktop
# Default-Start:     S
# Default-Stop:
# Short-Description: prepare live user for gnome desktop
# Description:       The purpose of this script is to preseed the live users
#                    settings for the gnome desktop.
### END INIT INFO

###
# F.U.L.L.S.T.O.R.Y init script
#
# Copyright: (C) 2007-2008 Kel Modderman <kel@otaku42.de>
# Copyright: (C) 2008-2016 Stefan Lippers-Hollmann <s.l-h@gmx.de>
# License:   GPLv2
#
# F.U.L.L.S.T.O.R.Y Project Homepage:
# https://github.com/fullstory
###

PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME="fll-gnome-desktop"

###
# source distro-defaults, no-op unless in live mode
###
FLL_DISTRO_MODE="installed"

if [ -r /etc/default/distro ]; then
	. /etc/default/distro
fi

if [ "${FLL_DISTRO_MODE}" != "live" ]; then
	exit 0
fi

###
# source lsb functions
###
. /lib/lsb/init-functions

###
# source fll functions
###
. /lib/init/fll

###
# cheatcode handling
###
for param in $(cat /proc/cmdline); do
	case "${param}" in
		flldebug=*)
			if [ "${param#flldebug=}" = "${NAME#fll-}" ] || [ "${param#flldebug=}" = "all" ]; then
                    		fll_redirect
			fi
			;;
	esac
done

change_gnome_session()
{
	if [ ! -d /etc/glib-2.0/schemas ]; then
		mkdir -p /etc/glib-2.0/schemas
	fi

	mount --bind /usr/share/glib-2.0/schemas /etc/glib-2.0/schemas
	sed -i "s@session-name=.*@session-name='${GNOME_SESSION_TYPE}'@" /etc/glib-2.0/schemas/live.org.gnome.desktop.session.gschema.override

	# we rebuild schema
	glib-compile-schemas /etc/glib-2.0/schemas
	umount /etc/glib-2.0/schemas
} 

do_start()
{
	if [ -n ${GNOME_SESSION_TYPE} ] ; then
		change_gnome_session
	fi

	su "${FLL_LIVE_USER}" -c /usr/share/desktop-defaults-gnome/preseed-user-home-gnome
}

case "${1}" in
	start)
		log_daemon_msg "${NAME}"
		log_action_begin_msg " preparing ${FLL_LIVE_USER} for gnome"
		do_start
		log_end_msg 0
		;;
	stop)
		;;
	restart|force-reload)
		echo "Error: argument '${1}' not supported" >&2
		exit 3
		;;
	status)
		log_success_msg "${0} is no dæmon."
		exit 0
		;;
	*)
		echo "Usage: ${NAME} {start|stop}" >&2
		exit 3
		;;
esac

:
