#!/bin/sh


# Don't care errors here
set +e


load_nsu_stuff()
{
    rmmod g_file_storage
    modprobe g_nokia use_mtp=0

    start infoserver
    sleep 3

    start csd
    start sscd

    start mce
    /sbin/dsmetool --start-dbus


    # We do not need phonet stuff
    sleep 2
    /usr/bin/killall phonet-at-phonebook
    /usr/bin/killall acm

}

unload_nsu_stuff()
{
   stop mce

   stop sscd
   stop csd
   stop infoserver


   # Leave g_nokia so we can charge
}



show_malf()
{
  echo "System is in MALF state: $*"

  load_nsu_stuff

  image_path="/usr/share/themes/blanco/meegotouch/images/system-services/"
  image_prefix="malf_"
  ok="OK"

  reason=$1
  component=$2
  if [ "x$component" = "x" ]; then
     component="unknown"
  fi

  component=`echo $component | sed 's/\://g'`
  reboot_needed=0

  case $1 in
  SOFTWARE)
    reason="Software problem ($component):"
    ;;
  SECURITY)
    reason="Security problem ($component):"
    reboot_needed=1
    ;;
  HARDWARE)
    reason="Hardware problem ($component):"
    ;;
  *)
    reason="Unidentified problem ($component):"
    ;;
  esac

  shift 2
  details=$*


  bg_color=0x000000
  color=0xffffff
  line_height=50
  line_height_2=50
  x=0


  # Sleep a while to get gconf up
  sleep 5
  lang=`gconftool -g /meegotouch/i18n/language`
  # .png if prefered .bmp is backup
  malf_image="$image_path$image_prefix$lang.png"
  malf_image2="$image_path$image_prefix$lang.bmp"
  echo "Trying to use image $malf_image"

  if [ ! -f $malf_image -a ! -f $malf_image2 ]; then
    # Language version image not available, try to speak English
    lang=en
  fi
  malf_image="$image_path$image_prefix$lang.png"
  malf_image2="$image_path$image_prefix$lang.bmp"

  #
  # Show (or try to) nice MALF graphics
  #
  dbus-monitor --system 'type=signal,interface=com.nokia.mce.signal,member=display_status_ind,path=/com/nokia/mce/signal,arg0=on' | \
  ( while read i; do
    if [ -f $malf_image ]; then
      /usr/bin/show_png $malf_image
      y=700
    elif [ -f $malf_image2 ]; then
      /usr/sbin/show_bmp -i $malf_image2
      y=700
    else
      echo "$malf_image not found, using home-made text screen"
      y=10
      text2screen -c -x 0 -y 0 -w 854 -h 480 -B $bg_color
      text2screen -p -t "CONTACT SERVICE" -s 3 -x $x -y $y -B $bg_color -T $color
      y=$(expr $y + $line_height)
    fi

    #
    # Always dump "blue screen" stuff
    #

    text2screen -p -t "$reason" -s 2 -x $x -y $y -B $bg_color -T $color

    if [ "x$details" != "x" ]; then
       y=$(expr $y + $line_height_2)
       text2screen -p -t "$details" -s 2 -x $x -y $y -B $bg_color -T $color
    fi

    if [ ! -f $malf_image -a ! -f $malf_image2 ]; then
        y=$(expr $y + $line_height)
        text2screen -p -t "Tap touchscreen to continue" -s 2 -x $x -y $y -B $bg_color -T $color
    fi;
  done ) &
  drawerpid=$!

  #
  # We did not show image that showed instructions, do it now and wait ack
  #

  if [ ! -f $malf_image -a ! -f $malf_image2 ]; then
      y=$(expr $y + $line_height)
      text2screen -p -t "Tap touchscreen to continue" -s 2 -x $x -y $y -B $bg_color -T $color
      /usr/sbin/waittouch /dev/input/ts 0 854 0 480
  else
    if  [ "x$ok" = "xNOK" ]; then
      /usr/sbin/waittouch /dev/input/ts 0 0 0 0
    else
      /usr/sbin/waittouch /dev/input/ts 600 653 80 400
    fi
  fi

  kill $drawerpid


  text2screen -c -x 0 -y 0 -w 854 -h 480 -B $bg_color

  unload_nsu_stuff

  if  [ "x$ok" = "xOK" ]; then
    echo "MALF_CHECK_OK (accepted)"
    rm /var/malf
    if [ $reboot_needed -eq 1 ]; then
      sync
      echo "SECURITY MALF, need to reboot"
      /sbin/reboot
      sleep 30
      /sbin/reboot -f
    fi
  else
    echo "MALF shuts down"
    sleep 3
    /sbin/poweroff -f
    sleep 100
  fi

}

show_malf $*
