#!/bin/bash

# .xinitrc : setup routine after initalize X.org

PWROFF="/tmp/.cocon.poweroff"


# opencocon-local startup
if [ -e /tmp/.cocon.cnf ];
then
  . /tmp/.cocon.cnf
fi

if [ -e /tmp/.cocon.cnf.part ];
then
  . /tmp/.cocon.cnf.part
fi


if [ -e $PWROFF ];
then
  rm $PWROFF
fi

# Default running on matchbox window manager
matchbox-window-manager -theme blondie -use_desktop_mode plain &
lxpanel &
nm-applet &


# Read auto-connection setting and run (TODO)



# Keymap
if [ "$COCON_KBD_X_MODEL" ];
then
  SETXKB=" -model $COCON_KBD_X_MODEL "
fi

if [ "$COCON_KBD_X_LAYOUT" ];
then
  SETXKB="$SETXKB -layout $COCON_KBD_X_LAYOUT "
fi

if [ "$COCON_KBD_X_VARIANT" ];
then
  SETXKB="$SETXKB -variant $COCON_KBD_X_VARIANT "
fi

if [ "$SETXKB" ];
then
  setxkbmap $SETXKB
fi


# Auto connection
if [ "$COCON_AUTOCONNECT" ];
then
  case $COCON_AUTOCONNECT in
  rdp)
    lxterminal -e /usr/bin/cocon-freerdp-launch
    ;;
  vnc)
    cocon-vnc-launch 
    ;;
  spice)
    cocon-spice-launch
    ;;
  www)
    cocon-caravan-launch
    ;;
  x)
    cocon-xephyr-launch
    ;;
  esac
  
  # Poweroff Flag
  if [ "$COCON_POWEROFF_AFTER_AUTOCONNECT" = "1" ];
  then
    touch $PWROFF
    exit 0
  else
    # TODO : Disable Autoconnect settings!
    sleep 5
  fi 
fi


  # New mode menu
while :
do
  if [ -e $PWROFF ];
  then
    break
  fi
  lxterminal -e /usr/bin/cocon-menu-launch
done

# Power off runs on .profile!
