#!/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 &
lxpanel &
connman-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 
  if [ "$COCON_AUTOCONNECT" = "rdp" ];
  then
      lxterminal -e /usr/bin/cocon-freerdp-launch
  fi

  # Poweroff Flag
  if [ "$COCON_POWEROFF_AFTER_AUTOCONNECT" = "1" ];
  then
    touch $PWROFF
    exit 0
  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!
