#!/bin/sh

. /etc/init.d/functions

for x in $(cat /proc/cmdline); do
        case $x in
        x11=false)
		echo "X Server disabled" 
		exit 0;
                ;;
        esac
done

case "$1" in
  start)
  
    if [ -f /etc/smart/user_channels.bak ]; then
		echo "Install in progress... File /etc/smart/user_channels.bak detected"
		exit 0
	fi
	
    # We don't want this script to block the rest of the boot process
    if [ "$2" != "background" ]; then
      $0 $1 background &
    else
       # work around from /etc/X11/Xinit
       export USER=root
       export HOME=/home/root
       if [ ! -d $HOME ] && [ -d /root ]; then
         HOME=/root
       fi

       . /etc/profile

       echo "Starting Xserver"
       . /etc/X11/xserver-common
       xinit /etc/X11/Xsession -- `which $XSERVER` $ARGS >/var/log/Xsession.log 2>&1
    fi 
  ;;

  stop)
        echo "Stopping XServer"
		if [ "${RUNLEVEL}" = "0" -o "${RUNLEVEL}" = "6" ]; then #if we are shutting down or rebooting then hide the terminal
			if [ -f /sys/class/graphics/fb0/blank ]; then
				echo 1 > /sys/class/graphics/fb0/blank
			fi
		fi
	
        killproc xinit
  ;;

  restart)
	$0 stop
        sleep 1
        $0 start
  ;;

  *)
        echo "usage: $0 { start | stop | restart }"
  ;;
esac

exit 0
