set -x
set -eu
set -o pipefail

export PATH=/usr/local/c"$which_python"/bin:"$PATH"

# pygobject-3.10.2.tar.xz with cpython 3.x, tries to find pycairo - when
# what we actually have is py3cairo.  So we specify PYCAIRO_CFLAGS and
# PYCAIRO_LIBS to skip the pkg-config.

base_dir="/usr/local/c$which_python"
include_dir="$base_dir/include"

if ! [ -d "$base_dir" ]
then
    echo "$base_dir does not exist" 1>&2
    exit 1
fi

if ! [ -d "$include_dir" ]
then
    echo "$include_dir does not exist" 1>&2
    exit 1
fi

export CFLAGS="-I$include_dir -I$include_dir/cairo -I$include_dir/pycairo"
export LDFLAGS="-L$base_dir/lib -Wl,-rpath -Wl,$base_dir/lib -Wl,-rpath -Wl,$base_dir/lib/x86_64-linux-gnu"
# export LDFLAGS="-L$base_dir/lib -Wl,-rpath,$base_dir/lib,-rpath,$base_dir/lib/x86_64-linux-gnu"
export CAIRO_CFLAGS="$CFLAGS"
export CAIRO_LIBS="$LDFLAGS"
export PYCAIRO_CFLAGS="$CFLAGS"
export PYCAIRO_LIBS="$LDFLAGS"
export GLIB_CFLAGS="$CFLAGS"
export GLIB_LIBS="$LDFLAGS"
# Cairo, perhaps others, want these mixed-case environment variables.
export glib_CFLAGS="$CFLAGS"
export glib_LIBS="$LDFLAGS"

# We use Python 2.x to build this, even though we're sometimes building for Python 2.x, 
# and sometimes building for Python 3.x.  Building with Python 3.x does not yet work
# Sat Jun 28 11:47:56 PDT 2014
#export PYTHON=/usr/bin/python

case "$which_python" in
    python-2.*) suffix="" ;;
    python-3.*) suffix=3 ;;
    *) echo Not 2.x or 3.x; exit 1 ;;
esac

PKG_CONFIG_PATH="$(set -eu; build_pkg_config_path)"
export PKG_CONFIG_PATH

#./configure \
#  --prefix=/usr/local/c"$which_python" \
#  --with-python="/usr/local/c$which_python/bin/python$suffix" \
#  && make

# configure the build
meson _build --prefix /usr/local/c"$which_python"

ninja -C _build