#! /bin/sh

# This script runs the xvesa x server

# Setting PATH and LIBRARY_PATH
PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin"
LD_LIBRARY_PATH="/lib:/lib/tls:/usr/lib:/usr/X11R6/lib:/opt/gnome/lib:/usr/local/lib:/usr/share/nero:/usr/sbin"
export PATH LD_LIBRARY_PATH

# Setting Locale
SET_LOCALE="POSIX"
if [ -e "/etc/vesalang.txt" ]; then
	SET_LOCALE="`cat /etc/vesalang.txt | grep LOCALE | cut -d'=' -f2`"
	if [ -z $SET_LOCALE ]; then	# Checking empty values.
		SET_LOCALE="POSIX"
	fi
fi

export LC_ALL=$SET_LOCALE
export LANG=$SET_LOCALE

# Keyboard Layout Setting

kboardlayout ()
{
 KEYBOARD=""
 FONTMAP=""
 CODEPAGE=""

 KEYBOARD="`cat /etc/vesalang.txt | grep KEYBOARD | cut -d'=' -f2`"
 if [ -z $KEYBOARD ]; then		# Checking empty values.
	 KEYBOARD="us.map"
 fi

 case $KEYBOARD in
  cz-lat2.map|pl.map|sk-qwerty.map|hu.map|slovene.map)
  FONTMAP=/usr/share/kbd/consolefonts/lat2-12.psfu
  CODEPAGE=ISO-8859-2
  break
 ;;
 *)
  FONTMAP=/usr/share/kbd/consolefonts/lat1-12.psfu
  CODEPAGE=ISO-8859-1
  break
 ;;
 esac
 echo -n "$KEYBOARD" > /etc/keyboard.txt
 return 0
}

if [ -e "/etc/vesalang.txt" ]; then
	kboardlayout 				# This function is above.
	if [ $? -eq 0 ];then
		KMAP="$KEYBOARD"
	else 						# Cancel, error exit.
		KMAP="us.map"
	fi
	echo -n "$KMAP" > /etc/keymap

	if [ "$FONTMAP" ];then
		echo -n "$FONTMAP" > /etc/fontmap
	fi
	if [ "$CODEPAGE" ];then
		echo -n "$CODEPAGE" > /etc/codepage
	fi

	cat /etc/keymap | grep ".map" > /dev/null 2>&1
	if [ $? -eq 0 ];then		# = 0 found.
		KMAP="`echo -n "$KMAP" | cut -f 1 -d '.'`"
		loadkeys $KMAP
	fi
fi

# Keyboard Layout End

# Mouse Handling
rm -f /dev/mouse
ln -sf /dev/psaux /dev/mouse
MOUSEBUTTONS=3

# Setting background
icewmbg

# Starting the Xserver
xinit /root/.xinitrc -- -screen 800x600x16 -mouse /dev/mouse,$MOUSEBUTTONS
