#!/bin/bash
#
# This is not the original gnome2-session but a startscript
# to install some preconfiguration for GNOME2 
# and some variables to check.
#
# For feedback on this script please see
# http://www.suse.de/feedback
#
# set a background until the gnome-session becomes visible
#xsetroot -cursor_name watch -solid "#6a83be"
#                 
# Help GNOME2 guessing where KDE resides on the filesystem
export KDEDIR=/opt/kde3
#
# We use a cascaded KDE configuration while in GNOME,
# to preconfigure some utilities better suited for GNOME
export KDEDIRS=/opt/gnome/share/dist/kde-preconf
#
#
# Manage GTK1 themes with gnome-theme-manager
export GTK_RC_FILES=/etc/opt/gnome/gtk/gtkrc:$HOME/.gtkrc-1.2-gnome2
#
# Set STYLE variable for other applications.
STYLE="$(gconftool-2 --get /desktop/gnome/interface/gtk_theme 2>/dev/null)"
export STYLE="${STYLE:-Default}"
#
#
# If gconfd 1 is still running, shut it down now
if [ -e /opt/gnome/bin/gconftool-1 ] 
	then 
	/opt/gnome/bin/gconftool-1 --shutdown
fi
#
#
# make sure .skel is available
mkdir -p $HOME/.skel


#
# use_gnome2_default_no_settings:  Same as use_gnome2_default without
# the wallpaper and fonts
function use_gnome2_default_no_settings {
# do this only one time
[ -e $HOME/.skel/gnome2-run ] && return

# run possibly available addon-scripts
if [ -d /opt/gnome/share/dist/addon-scripts ] ; then 
for scr in /opt/gnome/share/dist/addon-scripts/*; do
 [ -r "$scr" ] && . "$scr"
done
fi


#
#  add compatibility and default settings for GNOME1
#  (set help browsers to epiphany & nautilus)
if [ ! -d ~/.gnome ] ; then 
    mkdir -p ~/.gnome
    cp /opt/gnome/share/dist/gnome1/* ~/.gnome/
fi
if [ ! -e ~/.gtkrc ] ; then
    cp /opt/gnome/share/dist/gnome1/gtkrc ~/.gtkrc
fi



# we are done, set the flag
touch ~/.skel/gnome2-run
}

#
#
# use_gnome2_default: if the default preconfiguration is used,
# some modifications need to be done to setup correct wallpapers
#
function use_gnome2_default {
# do this only one time
[ -e $HOME/.skel/gnome2-run ] && return
# and only if no wallpaper is already set (or other background
# option)
[ -e $HOME/.gconf/desktop/gnome/background/%gconf.xml ] && return 
mkdir -p ~/.gconf/desktop/gnome/background
# determine X-Resolution
X=$(xdpyinfo |grep dimensions:|head -1|cut -d " " -f 7 |cut -d "x" -f 1)
Y=$(xdpyinfo |grep dimensions:|head -1|cut -d " " -f 7 |cut -d "x" -f 2)
# set default wallpaper to 1600x1200
GNOME_WALLPAPER="default-1600x1200.jpg" 
[ $((($X * 10) / $Y)) -ge 15 ] && GNOME_WALLPAPER="default-1920x1200.jpg"
GNOME_WALLPAPER="/usr/share/wallpapers/"$GNOME_WALLPAPER
# check if a wallpaper is actually available
if [ -e $GNOME_WALLPAPER ] ; then 
gconftool-2 -s /desktop/gnome/background/picture_filename --type string $GNOME_WALLPAPER
gconftool-2 -s /desktop/gnome/background/picture_options --type string stretched
fi


# Adjust DPI for better font size rendering
DPY="`LANG=C xdpyinfo | sed -n -e '/resolution/ s,.*resolution: *\([^x]*\)x\([^ ]*\) .*,\1 + \2,p'|head -1`"
[ "$DPY" == " + " ] && DPY="75" \
                    || DPY="$(( ( $DPY ) / 2 ))"
gconftool-2 -s /desktop/gnome/font_rendering/dpi --type float $DPY

# run possibly available addon-scripts
if [ -d /opt/gnome/share/dist/addon-scripts ] ; then 
for scr in /opt/gnome/share/dist/addon-scripts/*; do
 [ -r "$scr" ] && . "$scr"
done
fi


#
#  add compatibility and default settings for GNOME1
#  (set help browsers to epiphany & nautilus)
if [ ! -d ~/.gnome ] ; then 
    mkdir -p ~/.gnome
    cp /opt/gnome/share/dist/gnome1/* ~/.gnome/
fi
if [ ! -e ~/.gtkrc ] ; then
    cp /opt/gnome/share/dist/gnome1/gtkrc ~/.gtkrc
fi



# we are done, set the flag
touch ~/.skel/gnome2-run
}

function fix_wallpapers_on_update {

# we do that only the first time
[ -e $HOME/.skel/gnome2-run-9.2 ] && return

	# we change to the new distribution wallpapers
        # if the 9.1 generation wallpapers are no longer
	# available
	WALLPAPER=$(gconftool-2 -g /desktop/gnome/background/picture_filename)
	WALLPAPER=$(echo $WALLPAPER | cut -b -30)
	if   [ $WALLPAPER="/usr/share/wallpapers/suse9.1-" ] ; then
		# determine X-Resolution
		X=$(xdpyinfo |grep dimensions:|cut -d " " -f 7 |cut -d "x" -f 1)
		# if no config fits, use 1024x768
		GNOME_WALLPAPER="suse10-1027x768.jpg"
		[ $X -gt 799 ] && GNOME_WALLPAPER="suse10-800x600.jpg"
		[ $X -gt 1023 ] && GNOME_WALLPAPER="suse10-1024x768.jpg"
		[ $X -gt 1279 ] && GNOME_WALLPAPER="suse10-1280x1024.jpg"
		[ $X -gt 1399 ] && GNOME_WALLPAPER="suse10-1400x1050.jpg"
		[ $X -gt 1599 ] && GNOME_WALLPAPER="suse10-1600x1200.jpg"
		GNOME_WALLPAPER="/usr/share/wallpapers/"$GNOME_WALLPAPER
		# check if a wallpaper is actually available
		if [ -e $GNOME_WALLPAPER ] ; then
		gconftool-2 -s /desktop/gnome/background/picture_filename --type string $GNOME_WALLPAPER
		gconftool-2 -s /desktop/gnome/background/picture_options --type string stretched
		fi
	fi

# done, set the flag
touch ~/.skel/gnome2-run-9.2

}
		
function use_sled10_default {
# do this only for SLED 10
([ -e /etc/SuSE-release ] && grep "SUSE Linux Enterprise Desktop 10" /etc/SuSE-release) || return 

# do this only one time
[ -e $HOME/.skel/sled10-run ] && return

# dump the existing config for backup
PANEL_BACKUP=panel-settings-backup-`date '+%m%d%y%H%M%S'`.xml
gconftool-2 --dump /apps/panel > $PANEL_BACKUP

# unset the current panel configuration
gconftool-2 --recursive-unset /apps/panel/default_setup
gconftool-2 --unset /apps/panel/general/toplevel_id_list
gconftool-2 --unset /apps/panel/general/applet_id_list
gconftool-2 --unset /apps/panel/general/object_id_list
gconftool-2 --recursive-unset /apps/panel/profiles/default
gconftool-2 --recursive-unset /apps/panel/toplevels
gconftool-2 --unset /apps/metacity/general/num_workspaces

# we are done, set the flag
touch ~/.skel/sled10-run
}


# set up an arbitrary icon path for nautilus, accessing YaST Icons and, if installed, common KDE icons
KDE_CRYSTAL_32=/opt/kde3/share/icons/crystalsvg/32x32
KDE_CRYSTAL_48=/opt/kde3/share/icons/crystalsvg/48x48
GNOME_DEFAULT_32=/opt/gnome/share/icons/gnome/32x32
GNOME_DEFAULT_48=/opt/gnome/share/icons/gnome/48x48
YAST_ICONS=/usr/share/YaST2/theme/current/icons/48x48

export GNOME_ICON_PATH=/usr/share/images:$YAST_ICONS/apps:$KDE_CRYSTAL_32/apps:$KDE_CRYSTAL_32/devices:$KDE_CRYSTAL_32/mimetypes:$KDE_CRYSTAL_32/filesystems:$KDE_CRYSTAL_32/actions:$KDE_CRYSTAL_48/apps:$KDE_CRYSTAL_48/devices:$KDE_CRYSTAL_48/mimetypes:$KDE_CRYSTAL_48/filesystems:$KDE_CRYSTAL_48/actions:$GNOME_DEFAULT_48/apps:$GNOME_DEFAULT_32/apps:/opt/gnome/share/pixmaps


# start up the gnome2 session

# reset the SLED 10 UI only if upgrading from NLD9/SLES9, anything newer we 
# ignore (like SL 9.3), the logic here is redundant but its low risk
TRY_SLED10=1
if [ ! -e $HOME/.skel/gnome2-run ] || [ -e $HOME/.skel/gnome2-run-9.2 ] ; then
  TRY_SLED10=0
fi

fix_wallpapers_on_update
use_gnome2_default
if [ $TRY_SLED10 -gt 0 ] ; then
 use_sled10_default
else
 touch ~/.skel/sled10-run
fi



exec /opt/gnome/bin/gnome-session

