#!/bin/bash
# Sample script for fscd.
# Copy or link this script to rotate-normal and rotate-tablet.
#
# Copyright (C) 2008 Robert Gerlach <khnz@users.sourceforge.net>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2.
################################################################################

oskb_bin=/usr/bin/onboard 
[ "$oskb_bin" ] || oskb_bin="`type -p cellwriter`"
[ "$oskb_bin" ] || oskb_bin="`type -p onboard`"
[ "$oskb_bin" ] || oskb_bin="`type -p xvkbd`"
[ "$oskb_bin" ] || exit 0

killbyname () {
  local killpid="$( ps xuw | grep ^$USER | grep $1 | awk '{print "kill -TERM "$2";"}' )"
  if [ "$killpid" ]; then
    eval $killpid
  fi
}

case "$MODE" in

  ccw-tablet)
    if test "$ACTION" = "rotated"; then
      killbyname $oskb_bin
      "$oskb_bin" &
      KBD_PID=$!
      sleep 2
      KBD_WIN=$(wmctrl -l -p| awk '{if($3=='$KBD_PID') print $1}')
      wmctrl -i -r $KBD_WIN -b add,sticky
      killbyname "enable_touchscreen"
      enable_touchscreen -ccw &
    fi
    ;;
  tablet)
    if test "$ACTION" = "rotated"; then
      killbyname $oskb_bin
      "$oskb_bin" &
      KBD_PID=$!
      sleep 2
      KBD_WIN=$(wmctrl -l -p| awk '{if($3=='$KBD_PID') print $1}')
      wmctrl -i -r $KBD_WIN -b add,sticky
      killbyname "enable_touchscreen"
      enable_touchscreen -cw &
    fi
    ;;

  normal)
    if test "$ACTION" = "rotating"; then
      killbyname $oskb_bin
      killbyname "enable_touchscreen"
      enable_touchscreen &
    fi
    ;;
  inv-tablet)
    if test "$ACTION" = "rotated"; then
      killbyname $oskb_bin
      "$oskb_bin" &
      KBD_PID=$!
      sleep 2
      KBD_WIN=$(wmctrl -l -p| awk '{if($3=='$KBD_PID') print $1}')
      wmctrl -i -r $KBD_WIN -b add,sticky
      killbyname "enable_touchscreen"
      enable_touchscreen -inv &
    fi
    ;;

esac

exit 0
