#!/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"

# Make ruby aware of our CA bundle
export SSL_CERT_FILE="${EMBEDDED_DIR}/cacert.pem"

# 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}"
export VAGRANT_INSTALLER_VERSION="2"

# Setup CPPFLAGS/LDFLAGS so that any gem installs (for plugins) will
# be able to link against our tools. First, append a space to any
# previously set CPPFLAGS/LDFLAGS.
if [ "${CPPFLAGS}x" != "x" ]; then
  CPPFLAGS="$CPPFLAGS "
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 CPPFLAGS="${CPPFLAGS}-I${EMBEDDED_DIR}/include"
export LDFLAGS="${LDFLAGS}-L${EMBEDDED_DIR}/lib"

# Determine the OS that we're on, which is used in some later checks.
# It is very important we do this _before_ setting the PATH below
# because uname dependencies can conflict on some platforms.
OS=$(uname -s 2>/dev/null)

# Export the OS as an environmental variable that Vagrant can access
# so that it can behave better.
export VAGRANT_DETECTED_OS="${OS}"

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

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

# Set the path to the Ruby executable
RUBY_EXECUTABLE="${EMBEDDED_DIR}/bin/ruby"

# Find the Vagrant executable
files=(${GEM_PATH}/gems/vagrant-*)
for needle in "${files[@]}"; do
  if [ -f "${needle}/bin/vagrant" ]; then
    VAGRANT_GEM_PATH="${needle}"
    break
  fi
done

VAGRANT_EXECUTABLE="${VAGRANT_GEM_PATH}/bin/vagrant"
VAGRANT_LAUNCHER="${VAGRANT_GEM_PATH}/lib/vagrant/pre-rubygems.rb"

# Do some things depending on the OS that we're running on.
case $OS in
	CYGWIN* | MINGW32*)
		# In Linux-like environments on Windows, we want to add the
		# Windows path to the PATH so that bsdtar and friends can
		# be found.
		export PATH="${EMBEDDED_DIR}/gnuwin32/bin:${PATH}"
		;;
esac

# Additional things to do that is OS-specific. We put this in a separate
# case clause because we're matching specific OS's here.
case $OS in
	CYGWIN*)
		# If we're in Cygwin, we want to convert the path to a Windows path, rather
		# than a Cygwin path, which Ruby does not work with.
		VAGRANT_EXECUTABLE=`cygpath -wla "${VAGRANT_EXECUTABLE}"`
		VAGRANT_INSTALLER_EMBEDDED_DIR=`cygpath -wla "${EMBEDDED_DIR}"`
		VAGRANT_LAUNCHER=`cygpath -wla "${VAGRANT_LAUNCHER}"`
		GEM_HOME=`cygpath -wla "${GEM_HOME}"`
		GEM_PATH=`cygpath -wla "${GEM_PATH}"`
		;;
esac

# Export the VAGRANT_EXECUTABLE so that pre-rubygems can optimize a bit
export VAGRANT_EXECUTABLE

# Call the actual Vagrant bin with our arguments
"${RUBY_EXECUTABLE}" "${VAGRANT_LAUNCHER}" "$@"
