#!/usr/bin/env bash

if [ -f /usr/local/bin/python ]
then
  export PATH="/usr/local/bin:$PATH"
fi

target=all
if [ -z "${CC}" ]; then
  CC="cc"
fi
if [ -z "${CXX}" ]; then
  CXX="c++"
fi
while [ $# -gt 0 ]
do
  eval $1
  shift
done
wxdefs=""
qt4defs=""
qt5defs=""
qt6defs=""
if [ -n "${QT_ARCH}" ]; then
  qt_arch="${QT_ARCH}"
  qt_lib_suffix=""
elif [ "`uname -m`" = "x86_64" ]; then
  qt_lib_suffix="64"
  qt_arch="x86_64"
else
  qt_lib_suffix=""
  qt_arch="i386"   # using 'uname -p' does not work in that case; for ubuntu it returns i686, but i386 is set as path prefix
fi
qt4_qmake_names=("qmake-qt4" "qmake")
qt4_qmake_dirs=("/usr/lib/${qt_arch}-linux-gnu/qt4/bin" "/usr/bin" "/usr/lib${qt_lib_suffix}/qt4/bin")
qt5_qmake_names=("qmake-qt5" "qmake")
qt5_qmake_dirs=("/usr/lib/${qt_arch}-linux-gnu/qt5/bin" "/usr/bin" "/usr/lib${qt_lib_suffix}/qt5/bin")
if [ -d /usr/lib/arm-linux-gnueabihf/qt5 ]; then
  qt5_qmake_names=("qmake")
  qt5_qmake_dirs=("/usr/lib/arm-linux-gnueabihf/qt5/bin" "/usr/bin")
fi
qt6_qmake_names=("qmake-qt6" "qmake6" "qmake")
qt6_qmake_dirs=("/usr/lib/${qt_arch}-linux-gnu/qt6/bin" "/usr/bin" "/usr/lib${qt_lib_suffix}/qt6/bin")
gtkdefs=""
x11defs=""
xftdefs=""
gsdefs=""
glfwdefs=""
zmqdefs=""
avdefs=""
cairodefs=""
extradefs=""
if [ `uname` = "Darwin" ]; then
  if [ `arch` = "arm64" ]; then
    extra_root="/opt"
  else
    extra_root="/usr/local"
  fi
  if [ -z "${EXTRA_CFLAGS}" ]; then
    EXTRA_CFLAGS="-I${extra_root}/include"
  fi
  if [ -z "${EXTRA_CXXFLAGS}" ]; then
    EXTRA_CXXFLAGS="-I${extra_root}/include"
  fi
  if [ -z "${EXTRA_LDFLAGS}" ]; then
    EXTRA_LDFLAGS="-L${extra_root}/lib"
  fi
fi

create_extradef () {
    local var_name value extradef

    var_name="$1"
    shift
    extradef=""
    for value in "$@"; do
        if [ -z "${extradef}" ]; then
            extradef="${var_name}=${value}"
        else
            extradef="${extradef} ${var_name}+=${value}"
        fi
    done
    echo "${extradef}"
}

if [ ! -z "${EXTRA_CFLAGS}" ]; then
  extradefs="$(create_extradef EXTRA_CFLAGS $EXTRA_CFLAGS)"
fi
if [ ! -z "${EXTRA_CXXFLAGS}" ]; then
  extradefs="$extradefs $(create_extradef EXTRA_CXXFLAGS $EXTRA_CXXFLAGS)"
fi
if [ ! -z "${EXTRA_LDFLAGS}" ]; then
  extradefs="$extradefs $(create_extradef EXTRA_LDFLAGS $EXTRA_LDFLAGS)"
fi
ret=0

red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
normal=$(tput sgr0)

printf "\nBuilding GR Framework\n---------------------\n" >&2

if [ "`which ${CC} 2>/dev/null`" = "" ]
then
  info="${red} no${normal} [not found]"
else
  info="${green}yes${normal} [`${CC} --version | sed 's/) (/ /' | head -1`]"
fi
printf "%12s: %s\n" "C" "$info" >&2

if [ "`which ${CXX} 2>/dev/null`" = "" ]
then
  info="${red} no${normal} [not found]"
else
  info="${green}yes${normal} [`${CXX} --version | sed 's/) (/ /' | head -1`]"
fi
printf "%12s: %s\n" "C++" "$info" >&2

if [ "`which python 2>/dev/null`" = "" ]
then
  info="${red} no${normal} [not found]"
else
  info="${green}yes${normal} [version `python -c 'import sys;print(sys.version)' | head -1`]"
fi
printf "%12s: %s\n" "Python" "$info" >&2

if [ "`which latex 2>/dev/null`" = "" ]
then
  info="${red} no${normal} [not found]"
else
  info="${green}yes${normal} [version `latex --version|grep ^pdfTeX|awk '{print $2}'`]"
fi
printf "%12s: %s\n" "LaTeX" "$info" >&2

if [ "`which dvipng 2>/dev/null`" = "" ]
then
  info="${red} no${normal} [not found]"
else
  info="${green}yes${normal} [version `dvipng --version|grep ^dvipng|awk '{print $NF}'`]"
fi
printf "%12s: %s\n" "dvipng" "$info" >&2

function version {
  echo "$@" | awk -F. '{ printf("%d\n", $1); }';
}

if [ "$qt" != "no" ]
then
  qt_versions="4 5 6"
  if [ `uname` == "Darwin" ]; then
    if [ $(version "`sw_vers --productVersion`") -ge "14" ]; then
      qt_versions="5 6"
      qt4defs="QT4DEFS=-DNO_QT4 QT4_QMAKE=false"
    fi
  fi
  for qt_major_version in ${qt_versions}
  do
    eval current_qt="$qt${qt_major_version}"
    if [ "${current_qt}" != "no" ]
    then
      found_qmake=false
      eval qmake_path="\${QT${qt_major_version}_QMAKE}"
      if [ "${qmake_path}" != "" ]
      then
        if [ -x "${qmake_path}" ] || [ "`which ${qmake_path} 2>/dev/null`" != "" ]
        then
          qmake_qt_version="`${qmake_path} -query QT_VERSION 2>/dev/null`"
          if [ "${qmake_qt_version%%.*}" = "${qt_major_version}" ]
          then
            found_qmake=true
          fi
        fi
      else
        eval current_qmake_names=( "\${qt${qt_major_version}_qmake_names[@]}" )
        eval current_qmake_dirs=( "\${qt${qt_major_version}_qmake_dirs[@]}" )
        for qmake_name in "${current_qmake_names[@]}"
        do
          if [ "`which ${qmake_name} 2>/dev/null`" != "" ]
          then
            qmake_qt_version="`${qmake_name} -query QT_VERSION 2>/dev/null`"
            if [ "${qmake_qt_version%%.*}" = "${qt_major_version}" ]
            then
              found_qmake=true
              qmake_path="${qmake_name}"
              break
            fi
          fi
        done
        if [ "${found_qmake}" = "false" ]
        then
          for qmake_dir in "${current_qmake_dirs[@]}"
          do
            for qmake_name in "${current_qmake_names[@]}"
            do
              qmake_path="${qmake_dir}/${qmake_name}"
              if [ -x "${qmake_path}" ]
              then
                qmake_qt_version="`${qmake_path} -query QT_VERSION 2>/dev/null`"
                if [ "${qmake_qt_version%%.*}" = "${qt_major_version}" ]
                then
                  found_qmake=true
                  break
                fi
              fi
            done
            if [ "${found_qmake}" = "true" ]
            then
              break
            fi
          done
        fi
      fi
      if [ "${found_qmake}" = "false" ]
      then
        info="${red} no${normal} [Qt${qt_major_version} API not found]"
        eval qt${qt_major_version}defs="QT${qt_major_version}_QMAKE=false\ QT${qt_major_version}DEFS=-DNO_QT${qt_major_version}"
        ret=1
      else
        info="${green}yes${normal} [version ${qmake_qt_version}]"
        eval qt${qt_major_version}defs="QT${qt_major_version}_QMAKE=${qmake_path}"
      fi
    else
      eval qt${qt_major_version}defs="QT${qt_major_version}_QMAKE=false\ QT${qt_major_version}DEFS=-DNO_QT${qt_major_version}"
      info="${yellow} no${normal} [disabled]"
      ret=1
    fi
    printf "%12s: %s\n" "Qt${qt_major_version}" "$info" >&2
  done
fi

if [ "$wx" != "no" ]
then
  wxconfig=wx-config
  if [ "$WX_CONFIG" != "" ]
  then
    wxconfig=$WX_CONFIG
  fi
  if [ "`which $wxconfig 2>/dev/null`" = "" ]
  then
    wxdefs="WX_CONFIG=false WXDEFS=-DNO_WX WXINC= WXLIBS="
    info="${red} no${normal} [wx-config not found]"
    ret=1
  else
    wxdefs="WX_CONFIG=$wxconfig"
    info="${green}yes${normal} [version `wx-config --version`]"
  fi
else
  wxdefs="WX_CONFIG=false WXDEFS=-DNO_WX WXINC= WXLIBS="
  info="${yellow} no${normal} [disabled]"
  ret=1
fi
printf "%12s: %s\n" "wxWidgets" "$info" >&2

if [ "$gtk" != "no" ]
then
  if [ "`pkg-config gtk+-2.0 --cflags 2>/dev/null`" = "" ]
  then
    gtkdefs="GTK_CONFIG=false GTKDEFS=-DNO_GTK GTKINC= GTKLIBS="
    info="${red} no${normal} [gtk+-2.0 not found]"
    ret=1
  else
    gtkdefs="GTK_CONFIG=pkg-config"
    info="${green}yes${normal} [version `pkg-config gtk+-2.0 --modversion`]"
  fi
else
  gtkdefs="GTK_CONFIG=false GTKDEFS=-DNO_GTK GTKINC= GTKLIBS="
  info="${yellow} no${normal} [disabled]"
  ret=1
fi
printf "%12s: %s\n" "GTK+" "$info" >&2

if [ "$x11" != "no" ]
then
  tmpout=`mktemp /tmp/a.out.XXXXX`
  tmpsrc=`mktemp /tmp/a$$XXXXX.c`
  cat >$tmpsrc << eof
#include <X11/Intrinsic.h>

int main(int argc, char **argv)
{
    Widget toplevel;
    toplevel = XtInitialize(argv[0], "simple", NULL, 0, &argc, argv);
    XtMainLoop();
    return 0;
}
eof
  if [ `uname` = "Darwin" ]; then
    x11path="/opt/X11"
  elif [ -d /usr/X11R6 ]; then
    x11path="/usr/X11R6"
  else
    x11path="/usr/X11"
  fi
  cmd="${CC} ${EXTRA_CFLAGS} ${EXTRA_LDFLAGS} -o $tmpout $tmpsrc -I$x11path/include -L$x11path/lib -lXt -lX11"
  $cmd >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    x11defs="X11DEFS=-DNO_X11 X11INC= X11LIBS="
    info="${red} no${normal} [X11 API not found]"
    ret=1
  else
    x11defs="X11PATH=-L$x11path/lib"
    info="${green}yes${normal} [$x11path]"
  fi
else
  x11defs="X11DEFS=-DNO_X11 X11INC= X11LIBS="
  info="${yellow} no${normal} [disabled]"
  xft="no"
  gs="no"
  ret=1
fi
printf "%12s: %s\n" "X11" "$info" >&2
rm -f $tmpout $tmpsrc
if [ `uname` = "Darwin" ]; then
  dir=`dirname $0`
  if [ $ret -eq 0 ]; then
    cp -p $dir/gks/quartz/project.pbxproj.X11 lib/gks/quartz/GKSTerm.xcodeproj/project.pbxproj
  else
    cp -p $dir/gks/quartz/project.pbxproj lib/gks/quartz/GKSTerm.xcodeproj/project.pbxproj
  fi
fi

if [ "$xft" != "no" ]
then
  tmpout=`mktemp /tmp/a.out.XXXXX`
  tmpsrc=`mktemp /tmp/a$$XXXXX.c`
  tmpver=`mktemp /tmp/a$$XXXXX.txt`
  cat >$tmpsrc << eof
#include <stdio.h>
#include <X11/Xft/Xft.h>
int main(void)
{
    printf("%d.%d.%d\n", XFT_MAJOR, XFT_MINOR, XFT_REVISION);
    return 0;
}
eof
  if [ "`which freetype-config 2>/dev/null`" = "" ]; then
    freetype_cflags="`pkg-config freetype2 --cflags 2>/dev/null`"
  else
    freetype_cflags="`freetype-config --cflags 2>/dev/null`"
  fi
  cmd="${CC} ${EXTRA_CFLAGS} ${EXTRA_LDFLAGS} -o $tmpout $tmpsrc -I$x11path/include $freetype_cflags"
  $cmd >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    xftdefs="XFTDEFS=-DNO_XFT XFTLIBS="
    info="${red} no${normal} [Xft API not found]"
    ret=1
  else
    $tmpout >$tmpver 2>&1
    info="${green}yes${normal} [version `cat $tmpver`]"
  fi
else
  xftdefs="XFTDEFS=-DNO_XFT XFTLIBS="
  info="${yellow} no${normal} [disabled]"
  ret=1
fi
printf "%12s: %s\n" "Xft" "$info" >&2
rm -f $tmpout $tmpsrc $tmpver

if [ "$gs" != "no" ]
then
  tmpout=`mktemp /tmp/a.out.XXXXX`
  tmpsrc=`mktemp /tmp/a$$XXXXX.c`
  tmprev=`mktemp /tmp/a$$XXXXX.txt`
  cat >$tmpsrc << eof
#include <stdio.h>
#include <stdlib.h>
#include <ghostscript/iapi.h>

int main()
{
    gsapi_revision_t r;
    if (gsapi_revision(&r, sizeof(gsapi_revision_t)) == 0)
        fprintf(stderr, "%ld\n", r.revision);
    exit(0);
}
eof
  cmd="${CC} ${EXTRA_CFLAGS} ${EXTRA_LDFLAGS} -o $tmpout $tmpsrc -lgs"
  if [ `uname` = "Darwin" ]; then
    cmd="$cmd -L/usr/X11/lib -lXt -lX11 -liconv"
  fi
  $cmd >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    gsdefs="GSDEFS=-DNO_GS GSINC= GSLIBS="
    info="${red} no${normal} [GS API not found]"
    ret=1
  else
    $tmpout >$tmprev 2>&1
    info="${green}yes${normal} [revision `cat $tmprev`]"
  fi
else
  gsdefs="GSDEFS=-DNO_GS GSINC= GSLIBS="
  info="${yellow} no${normal} [disabled]"
  ret=1
fi
printf "%12s: %s\n" "Ghostscript" "$info" >&2
rm -f $tmpout $tmpsrc $tmprev

if [ "$glfw" != "no" ]
then
  tmpout=`mktemp /tmp/a.out.XXXXX`
  tmpsrc=`mktemp /tmp/a$$XXXXX.c`
  tmpver=`mktemp /tmp/a$$XXXXX.txt`
  cat >$tmpsrc << eof
#include <stdio.h>
#include <GLFW/glfw3.h>

int main(void)
{
    if (!glfwInit())
        return -1;
    fprintf(stderr, "%d.%d.%d\n", GLFW_VERSION_MAJOR, GLFW_VERSION_MINOR,
        GLFW_VERSION_REVISION);
    return 0;
}
eof
  cmd="${CC} ${EXTRA_CFLAGS} ${EXTRA_LDFLAGS} -o $tmpout $tmpsrc"
  if [ `uname` = "Darwin" ]; then
    libs="-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo"
    ret=1
  else
    libs="-lGL -lXrandr -lXxf86vm -lXinerama -lXcursor -lXi -lX11 -lrt -lpthread -lm"
  fi
  glfwlib="glfw3"
  $cmd -l$glfwlib $libs >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    glfwlib="glfw"
    $cmd -l$glfwlib $libs >/dev/null 2>&1
  fi
  if [ $? -ne 0 ]; then
    glfwdefs="GLFWDEFS=-DNO_GLFW GLFWLIBS="
    info="${red} no${normal} [GLFW 3.x API not found]"
    ret=1
  else
    glfwdefs="GLFWLIB=$glfwlib"
    $tmpout >$tmpver 2>&1
    info="${green}yes${normal} [version `cat $tmpver`]"
  fi
else
  glfwdefs="GLFWDEFS=-DNO_GLFW GLFWLIBS="
  info="${yellow} no${normal} [disabled]"
  ret=1
fi
printf "%12s: %s\n" "GLFW" "$info" >&2
rm -f $tmpout $tmpsrc $tmpver

if [ "$gl" != "no" ]
then
  tmpout=`mktemp /tmp/a.out.XXXXX`
  tmpsrc=`mktemp /tmp/a$$XXXXX.c`
  if [ `uname` = "Darwin" ]; then
    cat >$tmpsrc << eof
#include <OpenGL/OpenGL.h>
int main(){
  return 0;
}
eof
    libs="-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo"
    ret=1
  else
    cat >$tmpsrc << eof
#include <GL/gl.h>
int main(){
  return 0;
}
eof
    libs="-lGL -lX11"
  fi
  cmd="${CC} ${EXTRA_CFLAGS} ${EXTRA_LDFLAGS} -o $tmpout $tmpsrc"
  $cmd $libs >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    gldefs="GLDEFS=-DNO_GL PLATFORMLIBS="
    info="${red} no${normal} [OpenGL API not found]"
    ret=1
  else
    $tmpout >$tmpver 2>&1
    info="${green}yes${normal}"
  fi
else
  gldefs="GLDEFS=-DNO_GL PLATFORMLIBS="
  info="${yellow} no${normal} [disabled]"
  ret=1
fi
printf "%12s: %s\n" "OpenGL" "$info" >&2
rm -f $tmpout $tmpsrc $tmpver

if [ "$zmq" != "no" ]
then
  tmpout=`mktemp /tmp/a.out.XXXXX`
  tmpsrc=`mktemp /tmp/a$$XXXXX.c`
  tmpver=`mktemp /tmp/a$$XXXXX.txt`
  cat >$tmpsrc << eof
#include <stdio.h>
#include <zmq.h>

int main(void)
{
    void *context = zmq_ctx_new();
    void *publisher = zmq_socket(context, ZMQ_PUSH);
    if (0) {
        zmq_bind(publisher, "tcp://*:5556");
        zmq_send(publisher, "Hello", 5, 0);
    }
    zmq_close(publisher);
    zmq_ctx_destroy(context);
    fprintf(stderr, "%d.%d.%d\n", ZMQ_VERSION_MAJOR, ZMQ_VERSION_MINOR,
        ZMQ_VERSION_PATCH);
    return 0;
}
eof
  cmd="${CXX} ${EXTRA_CFLAGS} ${EXTRA_LDFLAGS} -o $tmpout $tmpsrc -lzmq -lpthread"
  if [ `uname` != "Darwin" ]; then
    cmd="${cmd} -lrt"
  fi
  $cmd >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    zmqdefs="ZMQDEFS=-DNO_ZMQ ZMQLIBS="
    info="${red} no${normal} [0MQ 3.x API not found]"
    ret=1
  else
    $tmpout >$tmpver 2>&1
    info="${green}yes${normal} [version `cat $tmpver`]"
  fi
else
  info="${yellow} no${normal} [disabled]"
  zmqdefs="ZMQDEFS=-DNO_ZMQ ZMQLIBS="
  ret=1
fi
printf "%12s: %s\n" "0MQ" "$info" >&2
rm -f $tmpout $tmpsrc $tmpver

if [ "$av" != "no" ]
then
  tmpout=`mktemp /tmp/a.out.XXXXX`
  tmpsrc=`mktemp /tmp/a$$XXXXX.c`
  tmpver=`mktemp /tmp/a$$XXXXX.txt`
  cat >$tmpsrc << eof
#ifdef __cplusplus
extern "C"
{
#endif
#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/mathematics.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#ifdef __cplusplus
}
#endif
int main(void)
{
  int minimum_version = (57 << 16) + (48 << 8) + 100;
  if (avcodec_version() < minimum_version) {
    /* ffmpeg version is lower than 3.1 */
    return 1;
  }

  const AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_VP8);
  AVCodecContext *ctx = avcodec_alloc_context3(codec);
  avcodec_free_context(&ctx);

  /* av_version_info() is the 'informative version string' but should not be parsed */
  fprintf(stderr, "%s\n", av_version_info());
  return 0;
}
eof
  cmd="${CXX} ${EXTRA_CFLAGS} ${EXTRA_LDFLAGS} -o $tmpout $tmpsrc -lavformat -lavcodec -lswscale -lavutil -lz"
  cmd_link_encoder="${CXX} ${EXTRA_CFLAGS} ${EXTRA_LDFLAGS} -o $tmpout $tmpsrc -lavformat -lavcodec -lswscale -lavutil -lz -lvpx -ltheora -logg -lopenh264"
  if [ `uname` = "Linux" ]
  then
    cmd="$cmd -lpthread -lm"
    cmd_link_encoder="$cmd_link_encoder -lpthread -lm"
  elif [ `uname` = "Darwin" ]
  then
    cmd="$cmd -liconv -framework VideoToolbox -framework CoreVideo -framework CoreFoundation -framework CoreServices -framework CoreMedia"
    cmd_link_encoder="$cmd_link_encoder -liconv -framework VideoToolbox -framework CoreVideo -framework CoreFoundation -framework CoreServices -framework CoreMedia"
  fi
  if $cmd >/dev/null 2>&1 && $tmpout >$tmpver 2>&1; then
    info="${green}yes${normal} [version `cat $tmpver | head -1`]"
    avdefs="AVDEFS= AVLIBS=-lavformat AVLIBS+=-lavcodec AVLIBS+=-lswscale AVLIBS+=-lavutil"
  elif $cmd_link_encoder >/dev/null 2>&1 && $tmpout >$tmpver 2>&1; then
    info="${green}yes${normal} [version `cat $tmpver | head -1`, linking encoders]"
    avdefs="AVDEFS= AVLIBS=-lavformat AVLIBS+=-lavcodec AVLIBS+=-lswscale AVLIBS+=-lavutil AVLIBS+=-lvpx AVLIBS+=-ltheora AVLIBS+=-logg AVLIBS+=-lopenh264"
  else
    avdefs="AVDEFS=-DNO_AV AVLIBS="
    info="${red} no${normal} [required APIs not found]"
    ret=1
  fi
else
  avdefs="AVDEFS=-DNO_AV AVLIBS="
  info="${yellow} no${normal} [disabled]"
  ret=1
fi
printf "%12s: %s\n" "ffmpeg" "$info" >&2
rm -f $tmpsrc $tmpout $tmpver

if [ "$cairo" != "no" ]
then
  tmpout=`mktemp /tmp/a.out.XXXXX`
  tmpsrc=`mktemp /tmp/a$$XXXXX.c`
  tmpver=`mktemp /tmp/a$$XXXXX.txt`
  cat >$tmpsrc << eof
#include <stdio.h>
#include <cairo/cairo.h>

int main(int argc, char **argv) {
    cairo_t *cr;
    cairo_surface_t *surface;

    surface = (cairo_surface_t *) cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 500, 500);
    cr = cairo_create(surface);

    cairo_move_to(cr, 0, 0);
    cairo_line_to(cr, 500, 500);
    cairo_stroke(cr);

    cairo_destroy(cr);
    cairo_surface_destroy(surface);

    puts(CAIRO_VERSION_STRING);

    return 0;
}
eof
  libs="-lm"
  cmd="${CC} ${EXTRA_CFLAGS} ${EXTRA_LDFLAGS} -o $tmpout $tmpsrc -lcairo -lpixman-1 -lpthread -lfreetype -lz"
  $cmd $libs >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    cairodefs="CAIRODEFS=-DNO_CAIRO CAIROLIBS="
    info="${red} no${normal} [Cairo not found]"
    ret=1
  else
    $tmpout >$tmpver 2>&1
    info="${green}yes${normal} [version `cat $tmpver | head -1`]"
  fi
else
  info="${yellow} no${normal} [disabled]"
  cairodefs="CAIRODEFS=-DNO_CAIRO CAIROLIBS="
  ret=1
fi
printf "%12s: %s\n" "Cairo" "$info" >&2
rm -f $tmpsrc $tmpout $tmpver


if [ "$tiff" != "no" ]
then
  tmpout=`mktemp /tmp/a.out.XXXXX`
  tmpsrc=`mktemp /tmp/a$$XXXXX.c`
  tmpver=`mktemp /tmp/a$$XXXXX.txt`
  cat >$tmpsrc << eof
#include <stdio.h>
#include <tiffio.h>

int main() {
    puts(TIFFGetVersion());
    return 0;
}
eof
  libs="-lm"
  cmd="${CC} ${EXTRA_CFLAGS} ${EXTRA_LDFLAGS} -o $tmpout $tmpsrc -ltiff"
  $cmd $libs >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    tiffdefs="TIFFDEFS=-DNO_TIFF TIFFLIBS="
    info="${red} no${normal} [libtiff not found]"
    ret=1
  else
    $tmpout >$tmpver 2>&1
    info="${green}yes${normal} [`cat $tmpver | head -1 | sed s/LIBTIFF,\ Version/version/`]"
  fi
else
  info="${yellow} no${normal} [disabled]"
  tiffdefs="TIFFDEFS=-DNO_TIFF TIFFLIBS="
  ret=1
fi
printf "%12s: %s\n" "libtiff" "$info" >&2
rm -f $tmpsrc $tmpout $tmpver

if [ "$agg" != "no" ]
then
  tmpout=`mktemp /tmp/a.out.XXXXX`
  tmpsrc=`mktemp /tmp/a$$XXXXX.c`
  tmpver=`mktemp /tmp/a$$XXXXX.txt`
  cat >$tmpsrc << eof
#include <stdio.h>
#include <agg_pixfmt_rgba.h>
#include <agg_renderer_base.h>

typedef agg::pixfmt_alpha_blend_rgba<agg::blender_rgba<agg::rgba8, agg::order_bgra>, agg::rendering_buffer> pix_fmt;
typedef agg::renderer_base<pix_fmt> renderer_base;

int main() {
    renderer_base renderer;
    return 0;
}
eof
  libs="-lm"
  cmd="${CXX} ${EXTRA_CFLAGS} ${EXTRA_LDFLAGS} -o $tmpout $tmpsrc -lagg"
  $cmd $libs >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    aggdefs="AGGDEFS=-DNO_AGG AGGLIBS="
    info="${red} no${normal} [agg not found]"
    ret=1
  else
    $tmpout >$tmpver 2>&1
    info="${green}yes${normal}"
  fi
else
  info="${yellow} no${normal} [disabled]"
  aggdefs="AGGDEFS=-DNO_AGG AGGLIBS="
  ret=1
fi
printf "%12s: %s\n" "agg" "$info" >&2
rm -f $tmpsrc $tmpout $tmpver

if [ "$xerces" != "no" ]
then
  tmpout=`mktemp /tmp/a.out.XXXXX`
  tmpsrc=`mktemp /tmp/a$$XXXXX.c`
  tmpver=`mktemp /tmp/a$$XXXXX.txt`
  cat >$tmpsrc << eof
#include <stdio.h>
#include <xercesc/util/XercesVersion.hpp>

int main(void) {
    const char *version = XERCES_FULLVERSIONDOT;
    printf("%s\n", version);
    return 0;
}
eof
  if [ "`which pkg-config 2>/dev/null`" != "" ]; then
    xercesc_cflags="`pkg-config xerces-c --cflags 2>/dev/null`"
    xercesc_libs="`pkg-config xerces-c --libs 2>/dev/null`"
  else
    xercesc_cflags=""
    xercesc_libs=""
  fi
  cmd="${CXX} ${EXTRA_CFLAGS} ${EXTRA_LDFLAGS} -o $tmpout $tmpsrc $xercesc_cflags"
  $cmd $xercesc_libs >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    xercescdefs="XERCESCDEFS=-DNO_XERCES_C XERCESCINC= XERCESCLIBS="
    info="${red} no${normal} [xercesc not found]"
    ret=1
  else
    $tmpout >$tmpver 2>&1
    xercescdefs=""
    if [ -n "$xercesc_cflags" ]; then
      xercescdefs="${xercescdefs} XERCESCINC=${xercesc_cflags}"
    fi
    if [ -n "$xercesc_libs" ]; then
      xercescdefs="${xercescdefs} XERCESCLIBS=${xercesc_libs}"
    fi
    # Remove a trailing space
    xercescdefs="${xercescdefs# }"
    info="${green}yes${normal} [version `cat $tmpver`]"
  fi
else
  info="${yellow} no${normal} [disabled]"
  xercescdefs="XERCESCDEFS=-DNO_XERCES_C XERCESCINC= XERCESCLIBS="
  ret=1
fi
printf "%12s: %s\n" "Xerces-C++" "$info" >&2
rm -f $tmpsrc $tmpout $tmpver

echo "" >&2
echo $target $wxdefs $qt4defs $qt5defs $qt6defs $gtkdefs $x11defs $xftdefs $gsdefs $glfwdefs $gldefs $zmqdefs $avdefs $cairodefs $tiffdefs $aggdefs $xercescdefs $extradefs
