#!/usr/bin/env bash
#This recipe was adapted from the default and conda-forge recipes

if [ "$(uname)" = "Darwin" ] ; then
    LIBICONV=yes
    # Need to get appropriate response to g_get_system_data_dirs()
    # See the hardcoded-paths.patch file
    CONDA_BUILD_SYSROOT=/opt/MacOSX10.9.sdk
    export CFLAGS="$CFLAGS -DCONDA_PREFIX=\\\"\${PREFIX}\\\""
elif [ "$(uname)" = "Linux" ] ; then
    # So the system (builtin to glibc) iconv gets found and used.
    LIBICONV=maybe
    export PATH="$PATH:$PREFIX/$HOST/sysroot/usr/bin"
fi

autoreconf -vfi

# A full path to PYTHON causes overly long shebang in gobject/glib-genmarshal
./configure --prefix=${PREFIX} \
            --host=$HOST \
            --with-python=${PYTHON} \
            --with-libiconv=${LIBICONV} \
            --disable-libmount \
                || { cat config.log; exit 1; }


make -j${CPU_COUNT} ${VERBOSE_AT}
make install

#Grab missing gio header files
cp ${SRC_DIR}/gio/*.h ${PREFIX}/include/gio-unix-2.0/gio/

# gdb folder has a nested folder structure similar to our host prefix (255 chars) which causes installation issues
#    so kill it.
rm -rf $PREFIX/share/gdb

# Steps from CIAO's build-all.sh
cd $PREFIX/include
cp ${SRC_DIR}/glib/glibconfig.h ${PREFIX}/include/
ln -s glib-2.0/glib glib
ln -s glib-2.0/glib.h glib.h
ln -s glib-2.0/glib-object.h glib-object.h
ln -s glib-2.0/gmodule.h gmodule.h
ln -s glib-2.0/gobject gobject
ln -s gio-unix-2.0/gio gio
