# Source this shell script to get the same environment as the build script

if [ -z "$ENV_ROOT" ]; then
  if [ -f "./env.source" ]; then
    ENV_ROOT=`pwd`
    export ENV_ROOT
  fi
fi

if [ -z "$ENV_ROOT" ]; then
  echo "Missing ENV_ROOT variable" >&2
elif [ "${ENV_ROOT#/}" = "$ENV_ROOT" ]; then
  echo "ENV_ROOT must be an absolute path" >&2
else

  BUILD_DIR="${BUILD_DIR:-$ENV_ROOT/build}"
  TARGET_DIR="${TARGET_DIR:-$ENV_ROOT/target}"
  DOWNLOAD_DIR="${DOWNLOAD_DIR:-$ENV_ROOT/dl}"
  BIN_DIR="${BIN_DIR:-$ENV_ROOT/bin}"

  export LDFLAGS="-L${TARGET_DIR}/lib"
  # FIXME: detect OS somehow
  export DYLD_LIBRARY_PATH="${TARGET_DIR}/lib"
  export PKG_CONFIG_PATH="$TARGET_DIR/lib/pkgconfig"
  #export CFLAGS="-I${TARGET_DIR}/include $LDFLAGS -static-libgcc -Wl,-Bstatic -lc"
  export CFLAGS="-I${TARGET_DIR}/include $LDFLAGS"
  export PATH="${TARGET_DIR}/bin:${PATH}"
  # Force PATH cache clearing
  hash -r
fi
