#!/bin/sh
# Single Xwindow/KDE startup script
# (C) Klaus Knopper Jun 2001

USER=knoppix

PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin:."
export PATH

umask 022

CRE="
[K"
BLUE="[1;34m"
NORMAL="[1;39m"

RUNLEVEL="$(runlevel)"

[ -f /etc/sysconfig/xserver ] && . /etc/sysconfig/xserver
[ -f /etc/sysconfig/knoppix ] && . /etc/sysconfig/knoppix

rm -rf /tmp/k*

[ -n "$BACKGROUND" -a -e "$BACKGROUND" ] || BACKGROUND=/usr/local/lib/knoppix.gif

# See how we were called.
case "$1" in
  start)
# Don't start X if brltty is running!
BPID="$(pidof brltty 2>/dev/null)"
[ "$?" = "0" ] && exit 0

echo "${CRE}${BLUE}Executing $0 $1:${NORMAL}"
        # Start daemons.
REALMEM="$(cat /proc/meminfo | awk '/MemTotal/{print $2}')"
if [ -n "$REALMEM" -a "$REALMEM" -lt 14000 ]
then
echo "${BLUE}$0: ${RED}Sorry, You need at least 16 Megs of RAM to start the X-Window system.${NORMAL}"
echo "${BLUE}$0: ${RED}Exiting now.${NORMAL}"
exit 1
fi


echo ""
echo "	${NORMAL}Starting single X-Window session for User $USER."
echo "	Please stand by a few seconds while the optimal configuration"
echo "	is being determined."
if [ "$RUNLEVEL" = "N 5" ]
then
echo ""
echo "After this session terminates, the system will automatically shut down."
fi
echo ""
echo -n "${BLUE}Starting X11...${NORMAL}"

ACCEL=""
XOPTIONS=""
NORESET=""
[ -n "$XSERVER" -a -x "/usr/X11R6/bin/$XSERVER" ] && ACCEL="$XSERVER"
[ -L /etc/X11/X ] && ACCEL="$ACCEL /etc/X11/X"

for X in $ACCEL vesa fbdev
do
# Use customized version of XF86Config
XCONFIG="/etc/X11/XF86Config"
BPP="-bpp"
case "$X" in
*XFree86*) XCONFIG="/etc/X11/XF86Config-4"; BPP="-depth"; NORESET="-noreset"; ;;
vesa|fbdev)
	for i in xserver knoppix; do echo 'XSERVER="XFree86"' >> /etc/sysconfig/$i; echo 'XMODULE="'"$X"'"' >> /etc/sysconfig/$i; done
	echo "${NORMAL}"
	echo -n "${BLUE}Retrying with Server ${GREEN}XFree86(${YELLOW}$X${GREEN})${NORMAL} "
	rm -f /etc/X11/XF86Config-4
	mkxf86config >/dev/null 2>&1
	X=XFree86 ; XCONFIG="/etc/X11/XF86Config-4"; BPP="-depth"; NORESET="-noreset"; XOPTIONS=""
	;;
esac
# Try hwsetup-generated flags first, if present
$X $NORESET -xf86config "$XCONFIG" $XOPTIONS  :0 2>/dev/null && break
# Mostly failsafe probe fallbacks
# Some cards prefer 16 bit, especially when low on mem or not SVGA,
# so try this first.
if $X $NORESET -xf86config "$XCONFIG" $BPP 16 :0 2>/dev/null || \
   $X $NORESET -xf86config "$XCONFIG" $BPP 24 :0 2>/dev/null || \
   $X $NORESET -xf86config "$XCONFIG" $BPP 32 :0 2>/dev/null || \
   $X $NORESET -xf86config "$XCONFIG" $BPP  8 :0 2>/dev/null; then break; fi
echo -n "${BLUE}.${NORMAL}"
done &
sleep 2
# Wait for X-Server startup
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
do
found="true"
# Startup Logo
# DISPLAY=:0 xloadimage -center -onroot -border black \
DISPLAY=:0 xloadimage -fullscreen -onroot -border black \
 -quiet "$BACKGROUND" >/dev/null 2>&1 && break
found="false"
sleep 1
done
echo ""

if [ "$found" = "false" ]
then
echo "${RED}Error: No suitable X-Server found for your card.${NORMAL}"
exit 1
fi

su -c "DISPLAY=:0 exec /etc/X11/xinit/xinitrc" - $USER >/dev/null 2>&1
RC="$?"

# After xsession exits, end X-Server
for i in $ACCEL $X $XSERVER XFree86 XF86_SVGA; do
killall -TERM $i 2>/dev/null && break
done
echo ""
echo -n "${CRE}${BLUE}X-Window session terminated "
if [ "$RC" = "0" ]
then
echo "without errors."
if [ "$RUNLEVEL" = "N 5" ]
then
echo "Shutting down.${NORMAL}"
# Play informational sound if soundcore module present
# (checking /dev/sndstat is unreliable)
OGGPLAY=/usr/bin/ogg123
PLAY=/usr/bin/wavp
[ -x "$PLAY" ] || PLAY=/usr/bin/play-sample
[ -x "$PLAY" ] || PLAY=/usr/bin/play
if [ -x "$OGGPLAY" -a -f /usr/share/sounds/shutdown.ogg ]; then
case "$(lsmod)" in *sound*) { $OGGPLAY -p 64 /usr/share/sounds/shutdown.ogg >/dev/null 2>&1 & } ; sleep 7 ;; esac
elif [ -f /usr/share/sounds/shutdown.wav -a -x "$PLAY" ]; then
case "$(lsmod)" in *sound*) { $PLAY /usr/share/sounds/shutdown.wav >/dev/null 2>&1 & } ; sleep 7 ;; esac
fi
init 0
fi
else
echo "with errors."
echo "${RED}Please check $XCONFIG and retry ${CYAN}'$0 start'${RED}.${NORMAL}"
fi
        ;;
  stop)
        # Stop daemons.
	for i in $XSERVER XF86_SVGA; do
        killall -TERM $i 2>/dev/null && echo "${BLUE}X-Server shut down.${NORMAL}" && break
	done
        ;;
  *)
        echo "${RED}Usage: xsession {start|stop}${NORMAL}"
        exit 1
esac

exit 0
