#!/bin/sh

XINPUT=yes
XCONFIG=$1/qmake.conf
VERBOSE=$2

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

#check for the XInput library
XDIRS=`sed -n -e '/^QMAKE_LIBDIR_X11[	]*=/ { s/[^=]*=[	 ]*//; s/-L/ /g; p; }' $XCONFIG`
LIBDIRS="$XDIRS /usr/lib /lib"
F=
for LIBDIR in $LIBDIRS
do
    FOUND_LIB=`ls $LIBDIR/libXi.* 2>/dev/null`
    if [ ! -z "$FOUND_LIB" ]
    then
	F=yes
	if [ "$VERBOSE" = "yes" ]
	then
	    echo "  Found XInput lib in $LIBDIR"
	fi
	break
     fi
done
if [ -z "$F" ]
then
    XINPUT=no
    if [ "VERBOSE" = "yes" ]
    then
	echo "  Could not find XInput lib anywhere in $LIBDIRS"
    fi
fi

#check for XInput.h and the IRIX wacom.h

if [ "$XINPUT" = "yes" ]
then
    INCS="X11/extensions/XInput.h wacom.h"
    XDIRS=`sed -n -e '/^QMAKE_INCDIR_X11[	]*=/ { s/[^=]*=[	 ]*//; s/-I/ /g; p; }' $XCONFIG`
    INCDIRS="XDIRS /usr/include /include"

    for I in $INCS
    do
	F=
	for INCDIR in $INCDIRS
	do
	    if [ -f $INCDIR/$I ]
	    then
		F=yes
		if [ "$VERBOSE" = "yes" ]
		then
		    echo "  Found $I in $INCDIR"
		fi
		break
	    fi
	done
	if [ -z "$F" ]
	then
	    XINPUT=no
	    if [ "$VERBOSE" = "yes" ]
	    then
		echo "  Could not find $INC anywhere in $INCDIRS"
	    fi
	fi
    done
fi

#done
if [ "$XINPUT" != "yes" ]
then
    if [ "$VERBOSE" = "yes" ]
    then
	echo "XInput disabled."
    fi
    exit 0
else
    if [ "$VERBOSE" = "yes" ]
    then
	echo "XInput enabled."
    fi
    exit 1
fi
