#!/bin/bash

GEOMETRY="/tmp/.cocon.geometry"
CNFFILE="/tmp/.cocon.cnf"

if [ -e $CNFFILE ];
then
  . $CNFFILE
fi


# first, copy settings from skel.
cp -R /etc/skel/.config/ /home/xuser
cp /etc/skel/.profile /home/xuser
cp /etc/skel/.xinitrc /home/xuser 


# Force Framebuffer driver if boot option contains "forcefb*"
if [ "$COCON_FORCEFB" = "1" ];
then
  ln -s /usr/share/cocon/card-fbdev.conf /etc/X11/xorg.conf.d/card-fbdev.conf
fi

# Force 800x600 16bit color if contains "forcesvga"
if [ "$COCON_FORCESVGA" = "1" ];
then
  ln -s /usr/share/cocon/defaultdepth.conf /etc/X11/xorg.conf.d/defaultdepth.conf
fi

# Workaround for graphics chipset bugs
# ( Userspace: not root )

# Workaround chips for broken X.org driver
#  (Currently ATI, Savage. Almost these chips can display 24-bit color depth.)
if [ "` lsmod | grep -E 'radeon|savage|atyfb'`" ];
then
  X_OPTION="-- -depth 24"
fi

if [ `echo $COCON_DEBUG` ];
then
  echo "DEBUG: before X. X_OPTION is $X_OPTION ."
  /bin/sh
fi

# opencocon userland startx
startx $X_OPTION
retr=$?

echo "X.org return: $retr"

if [ $retr -ne 0 ];
then
  # Error on wakeup X
  dialog --msgbox "Xorg initalize was failed. please try 'forcefb1', 'forcefb2' and 'forcefb3' option on boot loader." 20 60
  echo "To reboot, input 'exit'."
  /bin/sh
  exit 1
fi

exit 0
