#!/bin/sh
#
# Quick handler for chkhinge26 and X. 
#

killproc() {            # kill the named process(es)
        pid=`/bin/ps -e x |
             /bin/grep $1 |
             /bin/grep -v grep |
             /bin/sed -e 's/^  *//' -e 's/ .*//'`
        [ "$pid" != "" ] && kill $pid
}

export DISPLAY=:0

if [ -z "$1" ]; then
   echo "Usage: hinge-handler <state> ( 3 = closed, 0 = landscape, 2 = portrait )"
   exit 1
fi

STATE=$1

if [ $STATE = "3" ]; then
  #echo "sleeping"
  if [ -x /usr/bin/apm ]; then
    apm -s
  fi
  exit 0
fi

if [ $STATE = "0" ]; then
  #echo "lanscape"
  killproc /usr/bin/mbinputmgr
  # urg mbinputmgr should kill below
  killproc /usr/bin/matchbox-keyboard
  killproc /usr/bin/matchbox-stroke
  if [ -x /usr/bin/xrandr ]; then
    xrandr -o normal
  fi
  exit 0
fi

if [ $STATE = "2" ]; then
  #echo "portrait"
  if [ -x /usr/bin/xrandr ]; then
    /usr/bin/xrandr -o left 
  fi
  # just to be extra safe
  sleep 1
  if [ -x /usr/bin/mbinputmgr ]; then
    /usr/bin/mbinputmgr &
  fi
  exit 0
fi
