tar -xzvf ${PKG_NAME}-${PKG_VERSION}.tar.gz
cd ${PKG_NAME}-${PKG_VERSION}

if [ "`uname -s`" == "Linux" ] ; then
  export CFLAGS="$CFLAGS -fPIC"
  export CONFIG_FLAGS="--with-gtk --with-tcl=$PREFIX/lib --with-x"
  sed -i.orig 's|gtk+-2.0|gtk+-3.0|g' configure
else
  #Set the SYSROOT
  CONDA_BUILD_SYSROOT=/opt/MacOSX10.9.sdk

  #--with-gtk has to point to the directory containing gtk.h
  export CONFIG_FLAGS="--with-gtk=$PREFIX/include/gtk --with-tcl=$PREFIX/lib --x-libraries=/opt/X11/lib --x-includes=/opt/X11/include"
  sed -i.orig 's|gtk+-2.0|gtk+-3.0|g' configure

  PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$CONDA_PREFIX/lib/pkgconfig:/opt/X11/lib/pkgconfig"

  #tcltk issue with conflicting X11 includes
  rm -rf $PREFIX/include/X11
  rm -rf $CONDA_PREFIX/include/X11
fi

./configure --prefix=${PREFIX} --enable-threaded-xpans ${CONFIG_FLAGS}

# We could not find a way to pass this through
# configure or any other standard means.
if [ "`uname -s`" == "Linux" ] ; then
  sed -i.orig "s|./mklib|./mklib -linker $CC|g" Makefile

  # We could not find a way to both make the
  # tclxpa shared library and install it.
  # The following is the closest we could get.
  make shlib
  make tclxpa
  make install
  make shlib_install
else
  sed -i.orig 's|LINK="ar"|LINK="$\{AR\}"|g' mklib
  sed -i.ar 's|LINK="cc"|LINK="$\{CC\}"|g' mklib
  sed -i.orig "s|ar x |$\(AR\) x |g" Makefile
  sed -i.arx "s|ar crv|$\(AR\) crv|g" Makefile

  #Make just the xpa static lib. Skip tclxpa and shlib
  make install
fi
