#!/usr/bin/env bash
#
# This is a wrapper to properly execute Vagrant within the embedded
# Vagrant installation directory. This sets up proper environmental variables
# so that everything loads and compiles to proper directories.

# Get the directory where this script is. This will also resolve
# any symlinks in the directory/script, so it will be the fully
# resolved path.
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

# Useful variables
EMBEDDED_DIR="${DIR}/../embedded"

# Export gem paths so that we use the isolated gems.
export GEM_PATH="${EMBEDDED_DIR}/gems"
export GEM_HOME="${GEM_PATH}"
export GEMRC="${EMBEDDED_DIR}/etc/gemrc"

# Export an enviromental variable to say we're in a Vagrant
# installer created environment.
export VAGRANT_INSTALLER_ENV=1
export VAGRANT_INSTALLER_EMBEDDED_DIR="${EMBEDDED_DIR}"

# Setup CFLAGS/LDFLAGS so that any gem installs (for plugins) will
# be able to link against our tools. First, append a space to any
# previously set CFLAGS/LDFLAGS.
if [ "${CFLAGS}x" != "x" ]; then
	CFLAGS="$CFLAGS "
fi

if [ "${LDFLAGS}x" != "x" ]; then
	LDFLAGS="$LDFLAGS "
fi

# Append our include/lib search path to the flags and make sure they're
# exported so that any child processes have access to them.
export CFLAGS="${CFLAGS}-I${EMBEDDED_DIR}/include -L${EMBEDDED_DIR}/lib}"
export LDFLAGS="${LDFLAGS}-I${EMBEDDED_DIR}/include -L${EMBEDDED_DIR}/lib}"

# Prepend our embedded dir to the PATH so that we call that preferably
export PATH="${EMBEDDED_DIR}/bin:${PATH}"

# Unset any RUBYOPT, we don't want this bleeding into our runtime.
unset RUBYOPT

# The path to the Vagrant executable
VAGRANT_EXECUTABLE=${GEM_PATH}/bin/vagrant

# Call the actual Vagrant bin with our arguments
${EMBEDDED_DIR}/bin/ruby ${VAGRANT_EXECUTABLE} "$@"
