#!/bin/sh
#
# PyChess startup script
#


################################################################################
# Find path function is grabbed from googleearth                               #
################################################################################

FindPath()
{
    fullpath="`echo $1 | grep /`"
    if [ "$fullpath" = "" ]; then
        oIFS="$IFS"
        IFS=:
        for path in $PATH
        do if [ -x "$path/$1" ]; then
               if [ "$path" = "" ]; then
                   path="."
               fi
               fullpath="$path/$1"
               break
           fi
        done
        IFS="$oIFS"
    fi
    if [ "$fullpath" = "" ]; then
        fullpath="$1"
    fi

    # Is the sed/ls magic portable?
    if [ -L "$fullpath" ]; then
        fullpath=`ls -l "$fullpath" |sed -e 's/.* -> //' |sed -e 's/\*//'`
    fi
    dirname $fullpath
}

export PYCHESS_DATA_PATH="`FindPath $0`"

################################################################################
# Workaround for problem with ubuntu/debian kernels and gobject/gtk threading  #
################################################################################

# Shouldn't be needed any more

#if [ -f /etc/debian-release ] ; then
#    export LD_ASSUME_KERNEL=2.4.1
#elif [ -f /etc/debian_version ] ; then
#    export LD_ASSUME_KERNEL=2.4.1
#fi

################################################################################
# Try to import pychess modules and set up gettext                             #
################################################################################

python -c '
import pygtk
pygtk.require("2.0")

try:
    from pychess.Utils.const import prefix
except ImportError:
    print "ERROR: Could not import modules."
    print "Please try to run pychess as stated in the INSTALL file"
    import sys
    sys.exit(1)

import gettext, gtk.glade, os
if "/bin/" in os.environ["PYCHESS_DATA_PATH"]:
    gettext.install("pychess", unicode=1)
    gtk.glade.bindtextdomain("pychess")
else:
    gettext.install("pychess", localedir=prefix("lang"), unicode=1)
    gtk.glade.bindtextdomain("pychess", prefix("lang"))
gtk.glade.textdomain("pychess")

import pychess.Main
pychess.Main.run()
'
