#!/bin/sh

XFT=yes
XFT2=no
XFT2_HEADER=no
XCONFIG=$1/qmake.conf
VERBOSE=$2
SRCDIR=$3
OUTDIR=$4
shift 4
IN_LIBDIRS=""
IN_INCDIRS=""
PARAMS=$@
for PARAM in $PARAMS; do
    PREFIX=`echo $PARAM | sed 's/^\(..\).*/\1/'`
    case $PREFIX in
    -L)
	CLIBDIR=`echo $PARAM | sed -e 's/^-L//'`
	IN_LIBDIRS="$IN_LIBDIRS $CLIBDIR"
	;;
    -I)
        CINCDIR=`echo $PARAM | sed -e 's/^-I//'`
        IN_INCDIRS="$IN_INCDIRS $CINCDIR"
        ;;
    *) ;;
    esac
done

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

# cleanup...
rm -f $OUTDIR/config.tests/x11/xft.cfg
rm -f $OUTDIR/config.tests/x11/xft.inc
rm -f $OUTDIR/config.tests/x11/xft.lib

# check for the Xft library
XDIRS=`sed -n -e '/^QMAKE_LIBDIR_X11[	]*=/ { s/[^=]*=[	 ]*//; s/-L/ /g; p; }' $XCONFIG`
LIBDIRS="$IN_LIBDIRS $XDIRS /usr/shlib /usr/lib /lib"
F=
LIBS="Xft2 Xft"
for LIB in $LIBS; do
    for LIBDIR in $LIBDIRS; do
	EXTENSIONS="a so sl"
	for E in $EXTENSIONS; do
	    if [ -f "$LIBDIR/lib$LIB.$E" ]; then
		F="$LIB"
		[ "$VERBOSE" = "yes" ] && echo "  Found lib$LIB.$E in $LIBDIR"
		break
	    fi
        done
    done
    [ -n "$F" ] && break
done
if [ -z "$F" ]; then
    XFT=no
    [ "$VERBOSE" = "yes" ] && echo "  Could not find Xft lib anywhere in $LIBDIRS"
fi
LIBXFT="-l$F -lfreetype"

# check for X11/Xft/Xft.h
XFT_H=
if [ "$XFT" = "yes" ]; then
    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
	INCS="X11/Xft/Xft2.h X11/Xft/Xft.h"
	for INC in $INCS; do
	    if [ -f $INCDIR/$INC ]; then
		F=yes
		[ "$INC" = "X11/Xft/Xft2.h" ] && XFT2_HEADER=yes
		XFT_H=$INCDIR/$INC
		[ "$VERBOSE" = "yes" ] && echo "  Found $INC in $INCDIR"
		break
	    fi
	done
    done
    if [ -z "$F" ]; then
	XFT=no
	[ "$VERBOSE" = "yes" ] && echo "  Could not find $INC anywhere in $INCDIRS"
    fi
fi

# detect major version of Xft
if [ "$XFT" = "yes" ]; then
    XFT_MAJOR=`grep XFT_MAJOR $XFT_H | head -n 1 | awk '{ print \$3 }'`
    XFT_MINOR=`grep XFT_MINOR $XFT_H | head -n 1 | awk '{ print \$3 }'`
    XFT_REVISION=`grep XFT_REVISION $XFT_H | head -n 1 | awk '{ print \$3 }'`
    [ "$VERBOSE" = "yes" ] && echo "  Found Xft version $XFT_MAJOR.$XFT_MINOR.$XFT_REVISION"
    if [ "$XFT_MAJOR" = "2" ]; then
        XFT2=yes
	LIBXFT="$LIBXFT -lfontconfig"
    fi
fi

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

# check for freetype2 headers
FREETYPE2_INCDIR=
if [ "$XFT" = "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="$IN_INCDIRS $XDIRS $LDIRS /usr/include /include"
    F=
    for INCDIR in $INCDIRS; do
	if [ -f $INCDIR/$INC ]; then
	    # detect major version of freetype2
	    FREETYPE_MAJOR=`grep "#define FREETYPE_MAJOR" $INCDIR/$INC | head -n 1 | awk '{ print \$3 }'`
	    FREETYPE_MINOR=`grep "#define FREETYPE_MINOR" $INCDIR/$INC | head -n 1 | awk '{ print \$3 }'`
	    FREETYPE_PATCH=`grep "#define FREETYPE_PATCH" $INCDIR/$INC | head -n 1 | awk '{ print \$3 }'`
	    test -z "$FREETYPE_PATCH" && FREETYPE_PATCH="0"
	    [ "$VERBOSE" = "yes" ] && \
		echo "  Found Freetype version $FREETYPE_MAJOR.$FREETYPE_MINOR.$FREETYPE_PATCH"
	    if [ "$FREETYPE_MAJOR" -eq "2" ] \
               && [ "$FREETYPE_MINOR" -ge "0" -a "$FREETYPE_PATCH" -ge "9" ] \
               || [ "$FREETYPE_MINOR" -ge "1" ]; then
		F=yes
		FREETYPE2_INCDIR=$INCDIR/freetype2
		[ "$VERBOSE" = "yes" ] && echo "  Found $INC in $INCDIR"
		break
	    fi
	fi
    done
    if [ -z "$F" ]; then
	XFT=no
	[ "$VERBOSE" = "yes" ] && echo "  Could not find $INC anywhere in $INCDIRS"
    fi
fi

# check for fontconfig headers when using Xft2
if [ "$XFT" = "yes" ] && [ "$XFT2" = "yes" ]; then
    INC="fontconfig/fontconfig.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="$IN_INCDIRS $XDIRS $LDIRS /usr/include /include"
    F=
    for INCDIR in $INCDIRS; do
	if [ -f $INCDIR/$INC ]; then
	    F=yes
	    [ "$VERBOSE" = "yes" ] && echo "  Found $INC in $INCDIR"
	    break
	fi
    done
    if [ -z "$F" ]; then
	XFT=no
	[ "$VERBOSE" = "yes" ] && echo "  Could not find $INC anywhere in $INCDIRS"
    fi
fi

# done
if [ "$XFT" != "yes" ]; then
    [ "$VERBOSE" = "yes" ] && echo "Xft disabled."
    exit 0
else
    [ "$VERBOSE" = "yes" ] && echo "Xft enabled."
    [ "$XFT2_HEADER" = "yes" ] && \
	echo "xft2header" > $OUTDIR/config.tests/x11/xft.cfg
    echo "$FREETYPE2_INCDIR" > $OUTDIR/config.tests/x11/xft.inc
    echo "$LIBXFT" > $OUTDIR/config.tests/x11/xft.lib
    exit 1
fi
