#!/bin/sh

# Quick hack to make h-a-m work without icd.
mkdir -p ~/.osso && grep 'assume-connection 1' ~/.osso/hildon-application-manager || echo 'assume-connection 1' >> ~/.osso/hildon-application-manager

# notice: system dbus must be running
validate_theme() # $1 = theme name
{
  THEMES_DIR=/usr/share/themes
  if test -d ${THEMES_DIR}/$1; then
    if test -f ${THEMES_DIR}/$1/gtk-2.0/gtkrc; then
      if test -f ${THEMES_DIR}/$1/gtk-2.0/gtkrc.maemo_af_desktop; then
        if test -f ${THEMES_DIR}/$1/matchbox/theme.xml; then
          return 0
        fi
      fi
    fi
  fi
  return 1
}
                                                      
if [ -e $HOME/.osso/current-locale ]; then
	LANG=`cat $HOME/.osso/current-locale`
fi

if test -z "${LANG}"; then
  LANG=en_GB.utf8
fi
if test ! -d /usr/lib/locale/${LANG}; then
  LANG=en_GB.utf8
fi
export LANG

# for testing set display to something sane
if [ "x$DISPLAY" = "x" ]; then
	export DISPLAY=:0
fi

export MB_HUNG_APP_HANDLER='/usr/bin/hd-hung-app-handler'

DEFAULTMBTHEME=default
DEFAULTGTKTHEME=default

CURRENT_THEME_DIR=$HOME/.osso
CURRENT_THEME_FILE=$CURRENT_THEME_DIR/current-gtk-theme
CURRENT_KEY_THEME_FILE=$CURRENT_THEME_DIR/current-gtk-key-theme
if [ ! -f $CURRENT_THEME_FILE -a "x$DONOTCREATE" = "x" ]; then

  mkdir -p $CURRENT_THEME_DIR
  echo 'include "/usr/share/themes/default/gtk-2.0/gtkrc"' > $CURRENT_THEME_FILE
  echo "include \"/usr/share/themes/default/gtk-2.0/gtkrc.maemo_af_desktop\"" \
        > $CURRENT_THEME_DIR/current-gtk-theme.maemo_af_desktop
else
  # investigate if theme exists
  THEMEFILE=$(cat $CURRENT_THEME_FILE | sed -e "s/include\ \"\/usr\/share\/themes\///" -e 's/\/gtk\-2\.0\/gtkrc\"//'|tr -d '\n')
  DEFAULTMBTHEME=$THEMEFILE

  # if theme directory does not exist, use 'default' theme
  if ! validate_theme $THEMEFILE; then

    if [ "x$DONOTCREATE" = "x" ]; then
      # set GTK theme
      echo 'include "/usr/share/themes/default/gtk-2.0/gtkrc"' > $CURRENT_THEME_FILE

      # set MAD theme
      echo "include \"/usr/share/themes/default/gtk-2.0/gtkrc.maemo_af_desktop\"" \
        > $CURRENT_THEME_DIR/current-gtk-theme.maemo_af_desktop
    fi

    # set MBOX theme
    DEFAULTMBTHEME=default
  fi
fi
    
export DEFAULTMBTHEME=$DEFAULTMBTHEME
export DEFAULTGTKTHEME=$DEFAULTGTKTHEME

export GTK2_RC_FILES=$CURRENT_THEME_FILE:$CURRENT_THEME_DIR/current-gtk-theme.maemo_af_desktop

# Display the wallpaper early to show we're doing something.
if [ -f $HOME/.osso/hildon-desktop/home-background.conf ]; then
	WALLPAPER=`grep BackgroundImage= $HOME/.osso/hildon-desktop/home-background.conf | cut -d "=" -f 2`
else
	WALLPAPER=`grep File= /usr/share/backgrounds/$DEFAULTMBTHEME.desktop | cut -d "=" -f 2`
fi

hsetroot -center $WALLPAPER

mkdir -p $HOME/MyDocs/.images
mkdir -p $HOME/MyDocs/.videos
mkdir -p $HOME/MyDocs/.documents
mkdir -p $HOME/MyDocs/.sounds
mkdir -p $HOME/MyDocs/.games

export `dbus-launch --exit-with-session`
# shut up the keyring daemon
export `echo | gnome-keyring-daemon -l -d`

/usr/bin/maemo-launcher --daemon

# This is the new gconf line used for Maemo/Mer's ligconf2-6
/usr/lib/gconf2/gconfd-2 &
# This is the old gconf line used for Ubuntu's libgconf2-4
#/usr/lib/libgconf2-4/gconfd-2 &

/usr/lib/sapwood/sapwood-server &

MBTHEME=$DEFAULTMBTHEME

if [ -f $HOME/.osso/mbtheme ]; then
    MBTHEME=`cat $HOME/.osso/mbtheme`
fi

/usr/bin/clipboard-manager &

# OMGWTF FIXME!
if ! lshal | grep -i touchscreen ; then
	SHOW_CURSOR=yes
else
	SHOW_CURSOR=no
fi

/usr/bin/matchbox-window-manager \
      -theme $MBTHEME \
      -use_titlebar yes \
      -use_desktop_mode plain \
      -use_lowlight no \
      -use_cursor $SHOW_CURSOR \
      -use_dialog_mode static \
      -use_super_modal yes &

if /usr/bin/lshal | grep info.product | grep -i keyboard ; then
        /usr/bin/gconftool-2 -s -t bool /apps/osso/inputmethod/keyboard_available true 
fi

/usr/bin/hildon-input-method &

/usr/bin/nm-applet &

/usr/bin/hildon-desktop

#/usr/bin/nm-applet &

