#!/bin/bash
#
# Copyright (C) 2007-2009  OLPC
# Author: Bernardo Innocenti <bernie@codewiz.org>
# Author: John (J5) Palmieri <johnp@redhat.com>
# Author: Daniel Drake <dsd@laptop.org>
# Author: Martin Langhoff <martin@laptop.org>
#
# 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, see <http://www.gnu.org/licenses/>.
#

export SUGAR_SCALING=100

# We need to read this early (dlo #9543)
[ -f "$HOME/.sugar/debug" ] && . "$HOME/.sugar/debug"

# enable python optimized bytecode if the files are present (#8431)
# do this early too, so that the environment is set up for the dbus session
if [ -e /usr/lib/python2.?/subprocess.pyo ]; then
	export PYTHONOPTIMIZE=2
fi

# Run Xephyr before keyboard setup
# so that it loads in the Xephyr server
if [ -e /dev/sisusbvga0 ]; then

    # Give the root X server a bit of time
    # to come up.
    # Avoids screen/cursor corruption discussed
    # in dlo#10210
    sleep 1
    
    # We have a USB2VGA external display - use 
    # Xephyr to "shrink" our desktop.
    # -noxv makes sure video is visible over sisusbvga
    #       devices which don't support it
    # -dpi 133 gives reasonable font sizing
    /usr/bin/Xephyr :1 -screen 1024x768 -keybd ephyr,,,xkbmodel=evdev -noxv -dpi 133 &
    DISPLAY=:1
    USING_XEPHYR=Yes

    # Prevent idle suspend, it switches off
    # power to USB2VGA connector.
    touch /var/run/powerd-inhibit-suspend/$$

fi

# equivalent of /etc/X11/xinit/xinitrc-common without xmodmap or xrdb nonsense
[ -r /etc/profile.d/lang.sh ] && . /etc/profile.d/lang.sh
for file in /etc/X11/xinit/xinitrc.d/* ; do
	. $file
done

# provide some defaults
LANG=${LANG:-en_US.UTF-8}
XKB_LAYOUT=${XKB_LAYOUT:-us}

# We need to stop loading of certain GTK Input Modules (d.l.o #6945)
# Normally we would load the "gtk-im-context-simple" module, but 
# some keyboards require to use Compose tables, in which case we need 
# to use the "xim" module
# In reality, the Compose tables get loaded if the locale is set 
# appropriately, so we use the LANG variable below
if [ $LANG = "am_ET.UTF-8" -o $LANG = "ne_NP.UTF-8" ]; then
	GTK_IM_MODULE="xim"
	export GTK_IM_MODULE
elif [ $LANG = "zh_CN.UTF-8" ]; then
	GTK_IM_MODULE="scim"
	XMODIFIERS="@im=SCIM"
	XIM_PROGRAM="scim -d"
	USE_XOPENIM=t
	export GTK_IM_MODULE
	export XMODIFIERS
	export XIM_PROGRAM
	export USE_XOPENIM
    #FIXME: Why do I have to do this manually ? --sayamindu 
    scim --no-socket -f x11 -d &
else
	GTK_IM_MODULE="gtk-im-context-simple"
	export GTK_IM_MODULE
fi

# load keyboard settings
[ -f "/etc/sysconfig/keyboard" ] && . "/etc/sysconfig/keyboard"
[ -f "$HOME/.kbd" ] && . "$HOME/.kbd"


if [ -e "/ofw/banner-name" ]; then
	name=$(</sys/class/dmi/id/product_name)
	version=$(</sys/class/dmi/id/product_version)
	if [ "${name}-${version}" == "XO-1" ]; then
		# XO-1 only: set mouse & keyboard speed
		xset m 7/4 1
		xset r rate 500 30
	fi
fi

[ -n "$XKB_OPTION" ] && XKB_OPTION=`echo "$XKB_OPTION" |
	sed -e 's/\b/-option /g'`

# set keyboard layout
if [ "$XKB_MODEL" = "olpc2" ]   # no such model value
then
	# early mfg units of 1.5HS were programmed with KM and KV
	# both set to "olpc2".  they should have been "olpcm"/"olpc"
	XKB_MODEL="olpcm"
	test "$XKB_VARIANT" = "olpc2" && XKB_VARIANT="olpc"
fi

setxkbmap \
	${XKB_MODEL:+ -model $XKB_MODEL} \
	${XKB_LAYOUT:+ -layout $XKB_LAYOUT} \
	${XKB_VARIANT:+ -variant $XKB_VARIANT} \
	$XKB_OPTION -display :0

if [ "$USING_XEPHYR" = "Yes" ]; then
    setxkbmap \
	${XKB_MODEL:+ -model $XKB_MODEL} \
	${XKB_LAYOUT:+ -layout $XKB_LAYOUT} \
	${XKB_VARIANT:+ -variant $XKB_VARIANT} \
	-keycodes evdev -display :1 \
	$XKB_OPTION
    
fi

# disable repeat on several keys
xset -r 9 -r 220  -r 67 -r 68 -r 69 -r 70 -r 71 -r 72 -r 73 -r 74 -r 79 -r \
	81 -r 87 -r 89 -r 95 -r 96 -r 224 -r 147 -r 49 -r 10 -r 11 -r 12 -r 13 -r 14 -r \
	15 -r 16 -r 17 -r 18 -r 19 -r 20 -r 21 -r 23 -r 24 -r 25 -r 26 -r 27 -r 28 -r \
	29 -r 30 -r 31 -r 32 -r 33 -r 34 -r 35 -r 36 -r 37 -r 38 -r 39 -r 40 -r 41 -r 42 -r \
	43 -r 44 -r 45 -r 46 -r 47 -r 48 -r 51 -r 52 -r 53 -r 54 -r 55 -r 56 -r 57 -r \
	58 -r 59 -r 60 -r 61 -r 62 -r 219 -r 112 -r 110 -r 117 -r 115 -r 96  -r 221 -r \
	225 -r 236 -r 217 -r 218 -r 219 -r 220 -r 221 -r 191 -r 192 -r 193 -r 222 -r \
	223 -r 224 -r 225 -r 194 -r 195 -r 196 -r 226 -r 227 -r 228 -r 229 -r 197 -r \
	198 -r 199 -r 156 -r 235


# put Switch-to-sugar icon in place
install_switch_desktop_icon()
{
	local desktopdir=$(xdg-user-dir DESKTOP)
	[ ! -e "/usr/share/applications/olpc-switch-to-sugar.desktop" ] || \
		[ -e "${desktopdir}/olpc-switch-to-sugar.desktop" ] && return
	cp -f /usr/share/applications/olpc-switch-to-sugar.desktop \
		"${desktopdir}"
	chmod 755 "${desktopdir}"/olpc-switch-to-sugar.desktop
}


# Desktop selection
desktop=sugar
if [ -e "$HOME/.olpc-active-desktop" ]; then
	active=$(<$HOME/.olpc-active-desktop)
	if [ "${active}" == "gnome" ]; then
		desktop=gnome-session
		install_switch_desktop_icon
	fi
fi

# source custom user session, if present
[ -f "$HOME/.xsession" ] && . "$HOME/.xsession"

# useful for performance work
mv $HOME/.boot_time $HOME/.boot_time.prev 2>/dev/null
cat /proc/uptime >$HOME/.boot_time

# unfreeze the DCON for non-sugar environments
# (sugar unfreezes the dcon itself at an appropriate time)
# note: --print-reply should not be necessary, but works around a long-term bug
# where dbus-send can occasionally lose messages:
# http://marc.info/?l=freedesktop-dbus&m=120662577224073&w=2
if [ "$desktop" != "sugar" ]
then
    dbus-send --print-reply --system --dest=org.freedesktop.ohm \
    	/org/freedesktop/ohm/Keystore org.freedesktop.ohm.Keystore.SetKey \
	string:display.dcon_freeze int32:0 &
fi

exec $desktop 
