#!/bin/sh


XKB=yes
XPLATFORM=`basename $1`
XCONFIG=$1/qmake.conf
VERBOSE=$2


# debuggery

if [ "$VERBOSE" = "yes" ]
then
    echo "XKB auto-detection..."
fi


# some platforms are known to be broken
#   Solaris 7 + Xsun
#   	Patches 107648-01 and 107649-01 upgrade the Xsun package
#   	of Solaris 7 to X Xindows 6.4 on SPARC and Intel platforms
#   	respectively, introducing XKB support. However most headers
#   	are missing and there's no patch to fix that. Our solution
#   	is to disable XKB support on Solaris 7.
#   	Of course XFree86 is not affected by this Xsun bug, so ideally
#   	we should disable XKB support on Solaris + Xsun only, not on
#   	Solaris + XFree86. But then how to detect Xsun vs. XFree86?
#   Tru64
#   	Link-time problems.

case "$XPLATFORM" in
solaris-*)
    UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown
    case "$UNAME_RELEASE" in
    5.7)
	if [ "x$VERBOSE" = "xyes" ]
	then
		echo "XKB extension known to be broken on this platform."
	fi
	XKB=no
	;;
    esac
    ;;
tru64-*)
    UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown
    if [ "x$VERBOSE" = "xyes" ]
    then
	echo "XKB extension not supported on this platform."
    fi
    XKB=no
    ;;
esac
    
# check for headers

XKBLIB_H=
if [ "$XKB" = "yes" ]
then
    INC="X11/XKBlib.h"
    XDIRS=`sed -n -e '/^QMAKE_INCDIR_X11[	]*=/ { s/[^=]*=[	 ]*//; s/-I/ /g; p; }' $XCONFIG`
    INCDIRS="$XDIRS /usr/include /include"
    F=
    for INCDIR in $INCDIRS
    do
	if [ -f $INCDIR/$INC ]
	then
	    F=yes
	    XKBLIB_H=$INCDIR/$INC
	    if [ "$VERBOSE" = "yes" ]
	    then
		echo "  Found $INC in $INCDIR"
	    fi
	    break
	fi
    done
    if [ -z "$F" ]
    then
	XKB=no
	if [ "$VERBOSE" = "yes" ]
	then
	    echo "  Could not find $INC anywhere in $INCDIRS"
	fi
    fi
fi

# check for XkbSetPerClientControls in X11/XKBlib.h
# if it is not found, we disable xkb support

if [ "$XKB" = "yes" ] && [ -f "$XKBLIB_H" ]
then
    grep XkbSetPerClientControls $XKBLIB_H >/dev/null || XKB=no
    
    if [ "$VERBOSE" = "yes" ]
    then
	if [ "$XKB" = "yes" ]
	then
	    echo "  XkbSetPerClientControls found."
	else
	    echo "  XkbSetPerClientControls not found."
	fi
    fi
fi


# done

if [ "$XKB" != "yes" ]
then
    if [ "$VERBOSE" = "yes" ]
    then
	echo "XKB disabled."
    fi
    exit 0
else
    if [ "$VERBOSE" = "yes" ]
    then
	echo "XKB enabled."
    fi
    exit 1
fi
