#!/bin/sh

#
# Usage: testx <option> [name]
#
# help (displays help)
# start (starts X server using current configuration)
# stop (stops X server)
# rootless (prepares X server to start in rootless mode)
# fullscreen (prepares X server to start in fullscreen mode)
# use <name> (resets window manager or sets window manager to <name>)
#
# testx without any parameters will return the status of the X server (running/not running).
# testx 3.0 (c) GPL Andrew J. Brehm
#

if test -z $1 2> /dev/null

    then
    ps axc | grep X11 > /dev/null && xrunning=yes || xrunning=no
    
    if test $xrunning = yes

	then
	echo testx: X is running.
	exit 0
	
    fi

    echo testx: X is NOT running or has been killed.
    exit 1

fi

if test $1 = help 2> /dev/null

    then
    grep \# /usr/X11R6/bin/testx | grep -v "#!" | sed s/#//g
    exit 0
    
fi

if test $1 = start 2> /dev/null

    then
    /usr/X11R6/bin/testx > /dev/null && /usr/X11R6/bin/testx restart && exit 0
    cd /Applications/Utilities
    echo testx: Starting X11...
    open X11.app || (echo X11.app not present\? && exit 1)
    sleep 5
    /usr/X11R6/bin/testx && exit 0 || exit 1

fi

if test $1 = rootless 2> /dev/null

    then
    echo testx: Preparing X11 to start in rootless mode...
    defaults delete com.apple.x11 rootless
    defaults write com.apple.x11 rootless Yes
    echo testx: Done.
    exit 0

fi

if test $1 = fullscreen 2> /dev/null

    then
    echo testx: Preparing X11 to start in fullscreen mode...
    defaults delete com.apple.x11 rootless
    defaults write com.apple.x11 rootless No
    echo testx: Done.
    echo testx: Press meta-alt-a to switch to X desktop when X11 is active.
    exit 0

fi

if test $1 = stop 2> /dev/null

    then
    echo testx: Killing X11...
    killall X11
    sleep 1
    /usr/X11R6/bin/testx && exit 1 || exit 0

fi

if test $1 = use 2> /dev/null

    then
    cd

    if test -z $2 2> /dev/null

	then
	echo testx: Setting window manager back to quartz-wm...
	echo exec quartz-wm > .xinitrc && echo testx: Attempt succeeded. || exit 1
	exit 0

    fi

    echo testx: Attempting to configure X11 for window manager $2...
    echo exec quartz-wm --only-proxy \& > .xinitrc
    echo export KDEWM=kwin >> .xinitrc
    echo exec $2 >> .xinitrc && echo testx: Attempt succeeded. || exit 1
    exit 0

fi


