#!/bin/bash
#
razor='/opt/puppetlabs/server/apps/razor-server/share/razor-server'
torquebox='/opt/puppetlabs/server/apps/razor-server/share/torquebox'
jruby="${torquebox}/jruby"

# Make sure our Gemfile is found by the tool.  This is needed because Bundler
# checks for the Gemfile relative to the current working directory, not the
# code you are running -- so fails for anything, say, in the path. :/
export BUNDLE_GEMFILE="${razor}/Gemfile"

# Make sure our path points to the right general location.
export PATH="${razor}/bin:${jruby}/bin:${PATH}"

# Some other environment variables we might need.
export TORQUEBOX_HOME="${torquebox}"
export JBOSS_HOME="${torquebox}/jboss"
export JRUBY_HOME="${torquebox}/jruby"

# Load the sysconfig file for pe-razor-server,
# to get the RAZOR_CONFIG setting.
if [ -e /etc/sysconfig/pe-razor-server ]; then
  # Enable variable auto-exporting when we source the
  # sysconfig file so that the exec below doesn't drop
  # the variables set here. We can't export the vars
  # inside the sysconfig file due to the way systemd
  # parses the file.
  set -a
  . /etc/sysconfig/pe-razor-server
  set +a
fi

# Figure out what we were asked to execute.
exe="$(basename $0)"

# Find the executable, and run it directly, or fail out gracefully.
if test -f "${razor}/bin/${exe}"; then
    pushd $razor &> /dev/null
        exec "${razor}/bin/${exe}" "$@"
    popd &> /dev/null
elif test -f "${jruby}/bin/${exe}"; then
    exec "${jruby}/bin/${exe}"
else
    echo "unable to find the ${exe} command in razor!"
    exit 1
fi
