#!/bin/sh
#
# Copyright (C) 2004-2005 Nokia Corporation.
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA


DIR=/etc/osso-af-init

AF_DEFS=$DIR/af-defines.sh
source $AF_DEFS || exit 2

case "$1" in
start)  START=TRUE
        ;;
stop)   START=FALSE
        ;;
restart)
        echo "$0: not implemented"
        exit 1
        ;;
force-reload)
        echo "$0: not implemented"
        exit 1
        ;;
*)      echo "Usage: $0 {start|stop}"
        exit 1
        ;;
esac


if [ $START = TRUE ]; then
  # this is used by the input methods for disabling features in startup
  touch /tmp/im_disable_tools_menu

  echo "Running xset770 -- the future of xset!"
  # disable keyrepeat for select, menu, plus, minus, escape
  xset770 -r 36 0 -r 70 0 -r 73 0 -r 74 0 -r 9 0

  # block here if device lock is on
  if [ -x /sbin/devlock-blocker ]; then
    /sbin/devlock-blocker
  fi

  # wait for the session bus
  /usr/sbin/waitdbus session
  if [ $? -gt 0 ]; then
    echo "$0: Error, the D-BUS session bus did not start"
    exit 2
  fi

  if [ -x /etc/osso-af-init/maemo-launcher ]; then
    /etc/osso-af-init/maemo-launcher start
  fi

  if [ -x $DIR/startup-greeting.sh ]; then
    source $DIR/startup-greeting.sh start
    sleep 2
  fi
  # ke-recv, Wizards and Connectivity use Gtk
  source $DIR/osso-gtk.defs
  if [ -f $DIR/ke-recv.sh ]; then
    source $DIR/ke-recv.sh start
  fi

  if [ -e $HOME/first-boot-flag ]; then
    CONNECTIVITY_UI_SCRIPT=/usr/bin/osso-connectivity-ui.sh
    if [ -x $CONNECTIVITY_UI_SCRIPT ]; then
      source $CONNECTIVITY_UI_SCRIPT start
    fi
    if [ -x $DIR/startup-wizard.sh ]; then
      source $DIR/startup-wizard.sh start
      source $DIR/af-defines.sh ;# re-read locale variables
      # restart processes because the locale might have changed
      source $DIR/after-suw-process-restart.sh
    fi
    echo "LC_ALL='$LC_ALL' LANG='$LANG' LC_MESSAGES='$LC_MESSAGES'"
    if [ -x $DIR/osso-connectivity-ui-gwwizard-startup.sh \
         -a ! -f /tmp/suw-user-cancelled.tmp ]; then
      $DIR/osso-connectivity-ui-gwwizard-startup.sh start
    fi
    if [ -f $TMPDIR/launch-operator-wizard.tmp \
         -a ! -f /tmp/suw-user-cancelled.tmp ]; then
      if [ -x $DIR/operator-wizard.sh ]; then
        $DIR/operator-wizard.sh start
      fi
      rm -f $TMPDIR/launch-operator-wizard.tmp
    fi
    rm $HOME/first-boot-flag
    rm -f /tmp/suw-user-cancelled.tmp
  else
    if [ -x $DIR/startup-wizard.sh ]; then
      source $DIR/startup-wizard.sh start
    fi
  fi

  rm -f /tmp/im_disable_tools_menu
else
  if [ -x $DIR/osso-media-server.sh ]; then
    $DIR/osso-media-server.sh stop
  fi
fi
