#!/bin bash
#This recipe was adapted from the conda-forge recipe

GDKTARGET=""
if [ "$(uname)" == "Darwin" ] ; then
  CONDA_BUILD_SYSROOT=/opt/MacOSX10.9.sdk

  #FOR QUARTZ BACKEND
  #export GDKTARGET="quartz"
  #XWIN_ARGS=""
  #END

  #FOR x11 BACKEND
  export GDKTARGET="x11"
  XWIN_ARGS="--with-x --x-libraries=/opt/X11/lib --x-includes=/opt/X11/include --enable-xdamage=no"
  #Issue with competing X11 libs (from tk)    
  rm -rf $PREFIX/include/X11
  rm -rf $CONDA_PREFIX/include/X11
  #END

  export CFLAGS="$CFLAGS -I$CONDA_PREFIX/include -I$PREFIX/include -I/opt/X11/include/X11/extensions"
  export CXXFLAGS="$CXXFLAGS -I$CONDA_PREFIX/include -I$PREFIX/include -I/opt/X11/include/X11/extensions"
  export CPPFLAGS="$CPPFLAGS -I$CONDA_PREFIX/include -I$PREFIX/include -I/opt/X11/include/X11/extensions"
  export PKG_CONFIG_PATH="$CONDA_PREFIX/lib/pkgconfig:$PREFIX/lib/pkgconfig:/opt/X11/lib/pkgconfig"
  
  # Disable cups for macOS
  XWIN_ARGS="$XWIN_ARGS --disable-cups"

elif [ "$(uname)" == "Linux" ] ; then
  export GDKTARGET="x11"
  XWIN_ARGS="--with-x"
  export LD_LIBRARY_PATH="${PREFIX}/lib:${CONDA_PREFIX}/lib:$CONDA_PREFIX/$HOST/sysroot/usr/lib64"
  ln -sf $GCC $BUILD_PREFIX/bin/gcc
  export PATH="$PATH:$CONDA_PREFIX/$HOST/sysroot/usr/bin"
fi

#In case any of the GTK dependencies contain libtool files
# conda-build #3102 should fix this, but the dependency might have been built without this.
find $PREFIX -name '*.la' -delete
find $CONDA_PREFIX -name '*.la' -delete

./configure \
    --host=$HOST \
    --prefix="${PREFIX}" \
    --disable-dependency-tracking \
    --disable-silent-rules \
    --disable-glibtest \
    --enable-introspection=no \
    --enable-${GDKTARGET}-backend \
    --disable-visibility \
    --with-html-dir="${SRC_DIR}/html" \
    ${XWIN_ARGS} \
    || { cat config.log ; exit 1 ; }

make V=0 -j$CPU_COUNT
# make check -j$CPU_COUNT
make install -j$CPU_COUNT

cd $PREFIX/include
cp $PREFIX/include/gtk-3.0/gdk/gdkconfig.h $PREFIX/include
ln -s gtk-3.0/gdk gdk
ln -s gtk-3.0/gtk gtk

#Clean up created libtool files
cd $PREFIX
find . -name '*.la' -delete
