#!/bin/bash

export DISPLAY=:0.0

#
# openbox
#

if [ -e "/opt/bin/openbox/tray_show_hide.sh" ]; then
  # tray is visible
  if [ -e "/tmp/.tray_visible" ]; then
    /opt/bin/openbox/tray_show_hide.sh

    if [ -e "/opt/bin/keyboard.sh" ]; then
      /opt/bin/keyboard.sh 1
    else
      ps xa | grep matchbox-keyboard | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
    fi

    rm -f /tmp/.tray_visible
  else
  # tray is hidden
    touch /tmp/.tray_visible
    /opt/bin/openbox/tray_show_hide.sh
    sleep 0.5

    if [ -e "/opt/bin/keyboard.sh" ]; then
      /opt/bin/keyboard.sh 1
    else
      ps xa | grep matchbox-keyboard | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
    fi

    maxw=`xdpyinfo  | grep 'dimensions:' | sed -e 's/^[ \t]*//'  | cut -f5 -d' ' | cut -f1 -dx`
    maxw2=`expr $maxw - 145`

    if [ -e "/opt/bin/keyboard.sh" ]; then
      /opt/bin/keyboard.sh
    else
      matchbox-keyboard &
      sleep 1
      wmctrl -r "Keyboard" -e 1,0,370,$maxw2,350
    fi

    xdotool mousemove $maxw2 5 click 3
  fi

  exit
fi

#
# newer TIBs
#

if [ -e "/home/pos/.fluxbox/init.17.visible" ]; then
  FLUXBOX="/home/pos/.fluxbox"

  # tray is visible
  if [ -e "/home/pos/.fluxbox/is_visible" ]; then
    cp /home/pos/.fluxbox/init.17.hidden /home/pos/.fluxbox/init

    ps xa | grep nm-applet | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null

    if [ -e "/opt/bin/keyboard.sh" ]; then
      /opt/bin/keyboard.sh 1
    else
      ps xa | grep matchbox-keyboard | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
    fi

    # fluxbox restart
    ps xa | grep fluxbox | grep -v grep | awk '{ print $1 }' | xargs kill -SIGUSR2 2> /dev/null

    rm -f /home/pos/.fluxbox/is_visible

  # tray is hidden
  else
    cp /home/pos/.fluxbox/init.17.visible /home/pos/.fluxbox/init

    # fluxbox restart
    ps xa | grep fluxbox | grep -v grep | awk '{ print $1 }' | xargs kill -SIGUSR2 2> /dev/null
    ps xa | grep nm-applet | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null

    if [ -e "/opt/bin/keyboard.sh" ]; then
      /opt/bin/keyboard.sh 1
    else
      ps xa | grep matchbox-keyboard | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
    fi

    nm-applet &
    sleep 1

    maxw=`xdpyinfo  | grep 'dimensions:' | sed -e 's/^[ \t]*//'  | cut -f5 -d' ' | cut -f1 -dx`
    maxw2=`expr $maxw - 10`

    if [ -e "/opt/bin/keyboard.sh" ]; then
      /opt/bin/keyboard.sh
    else
      matchbox-keyboard &
      sleep 1
      wmctrl -r "Keyboard" -e 1,0,370,$maxw2,350
    fi

    xdotool mousemove $maxw2 1 click 3

    touch /home/pos/.fluxbox/is_visible
  fi

#
# older TIBs
#

else
  FLUXBOX="/root/.fluxbox"

  # tray is hidden, show it
  if [ -e "$FLUXBOX/init.hidden" ]; then
    mv $FLUXBOX/init.hidden $FLUXBOX/init

    # fluxbox restart
    ps xa | grep fluxbox | grep -v grep | awk '{ print $1 }' | xargs kill -SIGUSR2 2> /dev/null
    ps xa | grep nm-applet | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null

    if [ -e "/opt/bin/keyboard.sh" ]; then
      /opt/bin/keyboard.sh 1
    else
      ps xa | grep matchbox-keyboard | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
    fi
  # tray is visible, hide it
  else
    mv $FLUXBOX/init $FLUXBOX/init.hidden

    echo "session.menuFile:	~/.fluxbox/menu" > $FLUXBOX/init
    echo "session.keyFile: ~/.fluxbox/keys" >> $FLUXBOX/init
    echo "session.styleFile: /usr/share/fluxbox/styles/Ubuntu-light" >> $FLUXBOX/init
    echo "session.configVersion:	13" >> $FLUXBOX/init
    echo "session.screen0.toolbar.widthPercent: 100" >> $FLUXBOX/init
    echo "session.screen0.toolbar.placement: TopCenter" >> $FLUXBOX/init
    echo "session.screen0.strftimeFormat: %d %b, %a %02k:%M:%S" >> $FLUXBOX/init
    echo "session.screen0.toolbar.tools: prevworkspace, workspacename, nextworkspace, clock, prevwindow, nextwindow, iconbar, systemtray" >> $FLUXBOX/init

    # fluxbox restart
    ps xa | grep fluxbox | grep -v grep | awk '{ print $1 }' | xargs kill -SIGUSR2 2> /dev/null
    ps xa | grep nm-applet | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null

    if [ -e "/opt/bin/keyboard.sh" ]; then
      /opt/bin/keyboard.sh 1
    else
      ps xa | grep matchbox-keyboard | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
    fi

    sleep 2
    nm-applet &
    sleep 1

    maxw=`xdpyinfo  | grep 'dimensions:' | sed -e 's/^[ \t]*//'  | cut -f5 -d' ' | cut -f1 -dx`
    maxw2=`expr $maxw - 10`

    if [ -e "/opt/bin/keyboard.sh" ]; then
      /opt/bin/keyboard.sh
    else
      matchbox-keyboard &
      sleep 1
      wmctrl -r "Keyboard" -e 1,0,370,$maxw2,350
    fi

    xdotool mousemove $maxw2 1 click 3
  fi
fi

