#!/bin/sh
#
# Author: Aaron Voisine <aaron@voisine.org>

CWD="`dirname \"$0\"`"

# System version: 3 for Panther, 4 for Tiger, 5 for Leopard
export VERSION=`/usr/bin/sw_vers | grep ProductVersion | cut -f2 -d'.'`

# On Leopard, X11.app is installed by default, and will be started
# automatically via launchd.  On older systems, we need to start
# X11 ourself.

# For Panther and Tiger, start X11
if [[ $VERSION -le 4 ]]; then

        ps -wx -ocommand | grep -e '[X]11' > /dev/null
        if [ "$?" != "0" -a ! -f ~/.xinitrc ]; then
                echo "rm -f ~/.xinitrc" > ~/.xinitrc
                sed 's/xterm/# xterm/' /usr/X11R6/lib/X11/xinit/xinitrc >> ~/.xinitrc
        fi
       
        cp -f "$CWD/bin/getdisplay.sh" /tmp/
        rm -f /tmp/display.$UID
        open-x11 /tmp/getdisplay.sh || \
        open -a XDarwin /tmp/getdisplay.sh || \
        echo ":0" > /tmp/display.$UID
       
        while [ "$?" == "0" -a ! -f /tmp/display.$UID ]; do
                sleep 1
        done
        export "DISPLAY=`cat /tmp/display.$UID`"
       
        ps -wx -ocommand | grep -e '[X]11' > /dev/null || exit 11

fi

cd ~/
exec "$CWD/bin/gimp" "$@"
