#!/bin/sh


# initialization

XFREETYPE=yes
XCONFIG=$1/qmake.conf
VERBOSE=$2
x11tests=$3

rm -f $x11tests/xfreetype.cfg
rm -f $x11tests/xfreetype.inc


# debuggery

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


# check for lib

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/libXft.* 2>/dev/null`
    if [ ! -z "$FOUND_LIB" ]
    then
	F=yes
	if [ "$VERBOSE" = "yes" ]
	then
	    echo "  Found XftFreeType lib in $LIBDIR"
	fi
	break
    fi
done
if [ -z "$F" ]
then
    XFREETYPE=no
    if [ "$VERBOSE" = "yes" ]
    then
	echo "  Could not find XftFreeType lib anywhere in $LIBDIRS"
    fi
fi
    

# check for headers

XFT_H=
if [ "$XFREETYPE" = "yes" ]
then
    INC="X11/Xft/Xft.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
	    XFT_H=$INCDIR/$INC
	    if [ "$VERBOSE" = "yes" ]
	    then
		echo "  Found $INC in $INCDIR"
	    fi
	    break
	fi
    done
    if [ -z "$F" ]
    then
	XFREETYPE=no
	if [ "$VERBOSE" = "yes" ]
	then
	    echo "  Could not find $INC anywhere in $INCDIRS"
	fi
    fi
fi

if [ "$XFREETYPE" = "yes" ]
then
    INC="X11/Xft/XftFreetype.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
	    if [ "$VERBOSE" = "yes" ]
	    then
		echo "  Found $INC in $INCDIR"
	    fi
	    break
	fi
    done
    if [ -z "$F" ]
    then
	XFREETYPE=no
	if [ "$VERBOSE" = "yes" ]
	then
	    echo "  Could not find $INC anywhere in $INCDIRS"
	fi
    fi
fi


# check for XftNameUnparse in X11/Xft/Xft.h
XFTNAMEUNPARSE=yes
if [ "$XFREETYPE" = "yes" ] && [ -f "$XFT_H" ]
then
    grep XftNameUnparse $XFT_H >/dev/null || XFTNAMEUNPARSE=no
    if [ "$XFTNAMEUNPARSE" = "no" ]
    then
	echo "xftnameunparse" > $x11tests/xfreetype.cfg
    fi
fi


#check for freetype2 headers
FREETYPE2_INCDIR=
if [ "$XFREETYPE" = "yes" ]
then
    INC="freetype2/freetype/freetype.h"
    XDIRS=`sed -n -e '/^QMAKE_INCDIR_X11[	]*=/ { s/[^=]*=[	 ]*//; s/-I/ /g; p; }' $XCONFIG`
    LDIRS=`sed -n -e '/^QMAKE_INCDIR[	]*=/ { s/[^=]*=[	 ]*//; s/-I/ /g; p; }' $XCONFIG`
    INCDIRS="$XDIRS $LDIRS /usr/include /include"
    F=
    for INCDIR in $INCDIRS
    do
	if [ -f $INCDIR/$INC ]
	then
	    F=yes
	    FREETYPE2_INCDIR=$INCDIR/freetype2
	    if [ "$VERBOSE" = "yes" ]
	    then
		echo "  Found $INC in $INCDIR"
	    fi
	    break
	fi
    done
    if [ -z "$F" ]
    then
	XFREETYPE=no
	if [ "$VERBOSE" = "yes" ]
	then
	    echo "  Could not find $INC anywhere in $INCDIRS"
	fi
    fi    
fi


# done

if [ "$XFREETYPE" != "yes" ]
then
    if [ "$VERBOSE" = "yes" ]
    then
	echo "XftFreeType disabled."
    fi
    exit 0
else
    if [ "$VERBOSE" = "yes" ]
    then
	echo "XftFreeType enabled."
    fi
    echo "$FREETYPE2_INCDIR" > $x11tests/xfreetype.inc
    exit 1
fi
