#!/bin/sh

XKB=yes
XPLATFORM=`basename $1`
XCONFIG=$1/qmake.conf
VERBOSE=$2
shift 2
IN_INCDIRS=""
PARAMS=$@
for PARAM in $PARAMS; do
    PREFIX=`echo $PARAM | sed 's/^\(..\).*/\1/'`
    case $PREFIX in
    -I)
        CINCDIR=`echo $PARAM | sed -e 's/^-I//'`
        IN_INCDIRS="$IN_INCDIRS $CINCDIR"
        ;;
    *) ;;
    esac 
done

# debuggery
[ "$VERBOSE" = "yes" ] && echo "XKB auto-detection... ($*)"

# 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 Window 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)
	[ "x$VERBOSE" = "xyes" ] && echo "XKB extension known to be broken on this platform."
	XKB=no
	;;
    esac
    ;;
tru64-*)
    UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown
    [ "x$VERBOSE" = "xyes" ] && echo "XKB extension not supported on this platform."
    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="$IN_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
    [ "$VERBOSE" = "yes" ] && echo "XKB disabled."
    exit 0
else
    [ "$VERBOSE" = "yes" ] && echo "XKB enabled."
    exit 1
fi
