FONTINST_SUBDIR=""
FONTINST_TN=""

# Check for FreeType...
KFI_FOUND_FREETYPE=0

KDE_FIND_PATH(freetype-config, FREETYPE_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin], [
              AC_MSG_WARN([Could not find libfreetype anywhere, check http://www.freetype.org/])
             ])

if test -n "$FREETYPE_CONFIG"; then
    vers=`$FREETYPE_CONFIG --version 2>/dev/null | sed -e 's/libfreetype //' | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
    if test -n "$vers" && test "$vers" -ge 9002003
    then
        LIBFREETYPE_LIBS="`$FREETYPE_CONFIG --libs`"
        LIBFREETYPE_RPATH=
        for args in $LIBFREETYPE_LIBS; do
            case $args in
                -L*)
                LIBFREETYPE_RPATH="$LIBFREETYPE_RPATH $args"
                ;;
            esac
        done
        LIBFREETYPE_RPATH=`echo $LIBFREETYPE_RPATH | sed -e "s/-L/-R/g"`
        LIBFREETYPE_CFLAGS="`$FREETYPE_CONFIG --cflags`"
        KFI_FOUND_FREETYPE=1
    else
        AC_MSG_WARN([You need at least FreeType 2.1.3])
    fi
fi

if test "$KFI_FOUND_FREETYPE" -eq 1; then
    AC_SUBST(LIBFREETYPE_LIBS)
    AC_SUBST(LIBFREETYPE_CFLAGS)
    AC_SUBST(LIBFREETYPE_RPATH)

    KFI_FOUND_FONTCONFIG=0
    # check for fontconfig...
    KDE_FIND_PATH(fontconfig-config, FONTCONFIG_CONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin], [
            KDE_FIND_PATH(pkg-config, PKGCONFIG, [${prefix}/bin ${exec_prefix}/bin /usr/bin /usr/local/bin /opt/local/bin], [
                AC_MSG_WARN([Could not find neither pkg-config nor fontconfig-config, check http://www.fontconfig.org/ ])
            ])
    ])

    if test -n "$PKGCONFIG"; then
        vers=`$PKGCONFIG fontconfig --modversion 2>/dev/null | sed -e 's/libfontconfig //' | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
        if test -n "$vers" && test "$vers" -ge 2000000 ; then
            LIBFONTCONFIG_LIBS="`$PKGCONFIG fontconfig --libs`"
            LIBFONTCONFIG_RPATH=
            for args in $LIBFONTCONFIG_LIBS; do
                case $args in
                    -L*)
                        LIBFONTCONFIG_RPATH="$LIBFONTCONFIG_RPATH $args"
                        ;;
                esac
            done
            LIBFONTCONFIG_RPATH=`echo $LIBFONTCONFIG_RPATH | sed -e "s/-L/-R/g"`
            LIBFONTCONFIG_CFLAGS="`$PKGCONFIG fontconfig --cflags`"
            KFI_FOUND_FONTCONFIG=1
        fi
    fi

    if test -n "$FONTCONFIG_CONFIG"; then
        vers=`$FONTCONFIG_CONFIG --version 2>/dev/null | sed -e 's/libfontconfig //' | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
        if test -n "$vers" && test "$vers" -ge 2000000 ; then
            LIBFONTCONFIG_LIBS="`$FONTCONFIG_CONFIG --libs`"
            LIBFONTCONFIG_RPATH=
            for args in $LIBFONTCONFIG_LIBS; do
                case $args in
                    -L*)
                        LIBFONTCONFIG_RPATH="$LIBFONTCONFIG_RPATH $args"
                        ;;
                esac
            done
            LIBFONTCONFIG_RPATH=`echo $LIBFONTCONFIG_RPATH | sed -e "s/-L/-R/g"`
            LIBFONTCONFIG_CFLAGS="`$FONTCONFIG_CONFIG --cflags`"
            KFI_FOUND_FONTCONFIG=1
        fi
    fi

    if test "$KFI_FOUND_FONTCONFIG" -eq 1; then
        AC_SUBST(LIBFONTCONFIG_LIBS)
        AC_SUBST(LIBFONTCONFIG_CFLAGS)
        AC_SUBST(LIBFONTCONFIG_RPATH)

        # Check OS...
        AC_MSG_CHECKING([OS])
        case "$host" in
            *-*-linux*) UNAME='Linux' ;;
            *-*-freebsd*) UNAME='FreeBSD' ;;
            *-*-kfreebsd-gnu*) UNAME='GNU/kFreeBSD' ;;
            *-*-netbsd*) UNAME='NetBSD' ;;
            *-*-solaris*) UNAME='Solaris' ;;
            *-*-irix*) UNAME='Irix' ;;
            *-*-aix*) UNAME='AIX' ;;
            *-*-hpux*) UNAME='HPUX' ;;
            *-*-darwin*) UNAME='Darwin' ;;
            *) UNAME='Unknown' ;;
            esac
                AC_MSG_RESULT($UNAME)
        AC_SUBST(UNAME)

        # Check for fontenc...
        ac_font_enc_headers=0
        KDE_CHECK_HEADER(X11/fonts/fontenc.h,[ac_font_enc_headers=1])
        if test "$ac_font_enc_headers" = "0"; then
            AC_MSG_WARN(FontEnc headers not found. FontEnc support disabled)
        else
            ac_LDFLAGS_save="$LDFLAGS"
            ld_shared_flag=
            KDE_CHECK_COMPILER_FLAG(shared, [ld_shared_flag="-shared"])
            LDFLAGS="$LDFLAGS $ld_shared_flag $all_libraries -lz"
            KDE_CHECK_LIB(fontenc,FontEncDirectory,ac_font_enc_lib=1,ac_font_enc_lib=0)
            if test "$ac_font_enc_lib" = "1"; then
                LIB_FONT_ENC="-lfontenc"
                AC_DEFINE(HAVE_FONT_ENC, 1, [Defines if you have FontEnc])
            fi
            LDFLAGS="$ac_LDFLAGS_save"
        fi
        AC_SUBST(LIB_FONT_ENC)

        FONTINST_SUBDIR="tdefontinst"
        # Check for Xft...
        ac_xft_headers=0
        ac_CXXFLAGS_save="$CXXFLAGS"
        ac_CFLAGS_save="$CFLAGS"
        ac_CPPFLAGS_save="$CPPFLAGS"
        CXXFLAGS="$CXXFLAGS $all_includes $USER_INCLUDES $LIBFREETYPE_CFLAGS"
        CFLAGS="$CXXFLAGS"
        CPPFLAGS="$CXXFLAGS"
        
        AC_CHECK_HEADER(X11/Xft/Xft.h, [ac_xft_headers=1])
        if test "$ac_xft_headers" -eq 1 ; then
            LIBXFT_LIB=-lXft
            AC_SUBST(LIBXFT_LIB)
        else
            AC_MSG_WARN([Missing Xft headers - disabling font previews!])
        fi
        CXXFLAGS="$ac_CXXFLAGS_save"
        CFLAGS="$ac_CFLAGS_save"
        CPPFLAGS="$ac_CPPFLAGS_save"
    else
        AC_MSG_WARN([Missing FontConfig - disabling tdefontinst!])
    fi
else
    AC_MSG_WARN([Missing FreeType2 - disabling tdefontinst!])
fi

AM_CONDITIONAL(include_fontinst_tn, test "$ac_xft_headers" -eq 1)

if test "$ac_xft_headers" -eq 1 ; then
    AC_DEFINE(HAVE_XFT, 1, [Defines if you have Xft])
fi

AM_CONDITIONAL(include_kcontrol_tdefontinst, test -n "$FONTINST_SUBDIR")
